我想使用 jQuery 制作自己的幻灯片。在这里,我陷入了滑块图像的方向。我希望它只循环一个方向(从右到左)。请在下面查看我的代码:
HTML:
<div class="flash-banner">
<ul>
<li>
<img src="img/flash-banner.jpg" alt="Black glass banner " />
</li>
<li>
<img src="img/flash-banner-contact.jpg" alt="Black glass banner " />
</li>
<li>
<img src="img/flash-banner-our-work.jpg" alt="Black glass banner " />
</li>
</ul>
</div>
JavaScript:
$(document).ready( function ( e ) {
slideshow( 6000 );
});
function slideshow( speed ) {
// Get width from image
$pic = $('.flash-banner ul li img')[0];
$width = $pic.width;
// Count the total number of children of UL
$count = $('.flash-banner ul').children().length;
// Set width to UL
var $ul = $width * $count;
$('.flash-banner ul').css('width', $ul);
var i = 1;
setInterval( function ( e ) {
if ( i > $count - 1 ) {
// I think the problem is here, but I can't fix it
i = 0;
var b = 0;
} else {
var b = $width * i;
}
console.log( b );
$element = $('.flash-banner ul');
$element.animate(
{'margin-left': -b },
{ duration: speed },
{ delay: speed }
);
i++;
}, speed );
}