Forum Moderators: open
// Javascript
var x, dir;
var n = 0;
// this is actually defined in PHP
var end = 4;
function changeMain(n, dir) {
if (dir == 'next') x = n + 1;
else x = n - 1;
if (x < 0) x = end;
if (x > end) x = 0;
$('#slider-' + n).css('width', '0');
$('#slider-' + x).css('width', '294px');
return x;
}
// HTML
<div style="position: relative; overflow: hidden; width: 294px; height: 220px; background:#000">
<div id="slider-0"
style="float: left; overflow: hidden; width: 294px;
-webkit-transition: width .9s ease;
transition: width .9s ease"
onMouseOver="this.style.cursor= 'pointer';"
onClick="#">
<img src="https://www.example.com/photos/1128599_0.jpg" style="width: 294px; height: auto; border: 0">
</div>
<div id="slider-1"
style="float: left; overflow: hidden; width: 294px;
-webkit-transition: width .9s ease;
transition: width .9s ease"
onMouseOver="this.style.cursor= 'pointer';"
onClick="#">
<img src="https://www.example.com/photos/1128599_1.jpg" style="width: 294px; height: auto; border: 0">
</div>
<div style="clear: both"></div>
<div style="position: absolute; height: 50%; margin: auto; top: 0; bottom: 0; left: 0"
onClick="changeMain(n--)">
<
</div>
<div class="abs" style="height: 50%; margin: auto; top: 0; bottom: 0; right: 0"
onClick="changeMain(n++)">
>
</div>
</div>
// I don't know if I really need the CSS, but it was in the example
<link rel="stylesheet" href="//code.jquery.com/ui/1.12.1/themes/smoothness/jquery-ui.css">
<script src="//code.jquery.com/ui/1.12.1/jquery-ui.js"></script>
// Javascript
var x, dir;
var n = 0;
// this is actually defined in PHP
var end = 4;
function changeMain(n, dir) {
if (dir == 'next') x = n + 1;
else x = n - 1;
if (x < 0) x = end;
if (x > end) x = 0;
$('#slider-' + n).toggle('slide');
//// Eliminating these:
// $('#slider-' + n).css('width', '0');
// $('#slider-' + x).css('width', '294px');
return x;
}
// HTML
<div style="position: relative; overflow: hidden; width: 294px; height: 220px; background:#000">
<div id="slider-0"
style="float: left; overflow: hidden; width: 294px" // eliminating transition: width .9s ease
onMouseOver="this.style.cursor= 'pointer';"
onClick="#">
<img src="https://www.example.com/photos/1128599_0.jpg" style="width: 294px; height: auto; border: 0">
</div>
<div id="slider-1"
style="float: left; overflow: hidden; width: 294px"
onMouseOver="this.style.cursor= 'pointer';"
onClick="#">
<img src="https://www.example.com/photos/1128599_1.jpg" style="width: 294px; height: auto; border: 0">
</div>
<div style="clear: both"></div>
<div style="position: absolute; height: 50%; margin: auto; top: 0; bottom: 0; left: 0"
onClick="changeMain(n--)">
<
</div>
<div class="abs" style="height: 50%; margin: auto; top: 0; bottom: 0; right: 0"
onClick="changeMain(n++)">
>
</div>
</div>
// I'm defining the images in an array. In my case I actually loaded them
// from MySQL
$img = array(
'image_0.jpg',
'image_1.jpg',
'image_3.jpg'
);
// Behind the viewport, all of the images are going to float inline so the width
// of the child container is the sum of all image widths combined. So I'm calculating
// the width, or you could just use a number that's always going to be larger.
//
// In my case, the width of each image is 294px so I'm multiplying by 294
$container_width = (count($img) * 294) . 'px';
// Moving the last image to the front of the array
array_unshift($img, array_pop($img)); <script src="//code.jquery.com/jquery-2.2.4.min.js"></script>
// Optional, I use this for the mobile swipe options
<script src="//code.jquery.com/mobile/1.2.1/jquery.mobile-1.2.1.min.js"></script>
<script>
// These are all defined when you call the function, but I'll explain them here for you
//
// the name of the container that surrounds the image carousel
var slideID;
// the direction we're going with the slide: "back" or "next"
var slideDirection;
// the width of the images being scrolled
var imgWidth;
// when they're going "back". This will most likely be the negative value of
// imgWidth, so it's optional; leave it off and the default is - imgWidth
var negImgWidth;
// in milliseconds, so 1000 is 1 second; default for the script is 900, so this
// is optional, too
var slideSpeed;
function slideImg(slideID, slideDirection, imgWidth, negImgWidth, slideSpeed) {
// so you can leave the '#' off when you call the function
slideID = '#' + slideID;
// set defaults
if (imgWidth && !negImgWidth) negImgWidth = '-' + imgWidth;
if (!slideSpeed) slideSpeed = 900;
// when they click the left button
if (slideDirection == 'back') {
$(slideID + ' ul').animate({ marginLeft: imgWidth }, slideSpeed, function() {
// takes the image on the left and moves it to the far right
$(this).find('li:first').before($(this).find('li:last'));
$(this).css({ marginLeft: 0 });
})
}
// when they click the right button
else {
$(slideID + ' ul').animate({ marginLeft: negImgWidth }, slideSpeed, function() {
// takes the image on the right and moves it to the far left
$(this).find('li:last').after($(this).find('li:first'));
$(this).css({ marginLeft: 0 });
})
}
}
// Mobile options, slide when the user swipes; I'm just leaving my
// defined ID of "carousel" here
$(function() {
$('#carousel').on('swipeleft', function() {
slideImg('carousel', 'next', 294)
});
$('#carousel').on('swiperight', function() {
slideImg('carousel', 'back', 294)
});
});
</script> // I'm still gonna put comments in the HTML as if it were PHP or Javascript, just
// remember to take them out when you use it
echo <<<EOF
// this is the ID that becomes "slideID" in the Javascript
<div id="carousel" style="overflow: hidden">
// I use position relative so that I can overlay the < > button later
<div style="position: relative;
// the left margin is -300 because the width of my image is 294px, + 4px padding on
// left and right, plus 2px padding on left and right of the <li> later
margin-left: -300px; padding: 4px;
// remember, this DIV is going to be the sum of the width of all the images combined
width: $container_width;
// the height of my images; change this to whatever your height is
height: 220px">
<ul style="list-style: none; margin: 0; padding: 0">
EOF;
foreach ($img as $key) {
echo <<<EOF
<li style="float: left; padding: 0 2px">
<img src="$key" style="width: 294px; height: auto; border: 0">
</li>
EOF;
}
echo <<<EOF
</ul>
EOF;
// Here is where I create the < and > buttons; the left is based on the width of the
// first image that's hidden on the left side of the carousel
if ($img[1]) {
echo <<<EOF
<div style="position: absolute; height: 50%; margin: auto; top: 0; bottom: 0; left: 267px"
onClick="slideImg('carousel', 'back', 294)">
<
</div>
<div style="position: absolute; height: 50%; margin: auto; top: 0; bottom: 0; left: 544px"
onClick="slideImg('carousel', 'next', 294)">
>
</div>
EOF;
}
echo <<<EOF
</div>
EOF;
$(slideID + ' ul').animate({ marginLeft: imgWidth }, slideSpeed, function() {
$(this).find('li:first').before($(this).find('li:last'));
$(this).css({ marginLeft: 0 });
}) onClick="$('#carousel ul').css({ marginLeft: 0 });
slideImg('carousel', 'next', false, -600, 300);">