如何制作循环图像滑块? 下面是我的图像幻灯片放映代码。我想制作幻灯片循环,即当最后一个图像出现时,单击下一个按钮,第一个图像应该再次出现。 我的JS就像这样
function worksCarousel(){
var totalWorks = $(".wrapper-project ul li").length;
var ampleWork = $(".project-content").width();
var ampleTotalWorks = totalWorks*ampleWork;
$('.wrapper-project ul').css('width',ampleTotalWorks)
var fragment = document.createDocumentFragment(),
li = document.createElement('li');
while (totalWorks--) {
fragment.appendChild(li.cloneNode(true));
}
$('.controller-3 ul').append(fragment);
var index3 = 0;
var pos3 = 1;
$('.controller-3 ul li:first-child').addClass('selected');
$(".controller-3 ul li").click(function(){
index3 = $(this).index();
$(".wrapper-project").stop().animate({scrollLeft:ampleWork*index3},'slow');
$('.controller-3 ul li').removeClass('selected');
$(this).addClass('selected');
//alert(ampleitem);
});
$(".w-next").click(function(){
if( index3 != $(".controller-3 ul li").size()-1){
index3++;
$(".wrapper-project").stop().animate({scrollLeft:ampleWork*index3},'slow');
pos3++;
$('.controller-3 ul li.selected').removeClass('selected').next().addClass('selected');
}
});
$(".w-prev").click(function(){
if( index3!=0 ){
index3--;
$(".wrapper-project").stop().animate({scrollLeft:ampleWork*index3},'slow');
pos3--;
$('.controller-3 ul li.selected').removeClass('selected').prev().addClass('selected');
}
});
}
我的html代码是
<div class="nav-work">
<div class="w-prev">Next</div>
<div class="w-next">Prev</div>
</div>
<div class="wrapper-project">
<ul>
<li><img alt="" src="{tag_portfolio image 1_value}" /></li>
<li><img alt="" src="{tag_portfolio image 2_value}" /></li>
<li><img alt="" src="{tag_portfolio image 3_value}" /></li>
<li><img alt="" src="{tag_portfolio image 4_value}" /></li>
<li><img alt="" src="{tag_portfolio image 5_value}" /></li>
</ul>
</div>
</div>
</div>
答案 0 :(得分:0)
一个可以给出想法的简单解决方案如下:
var images_li = $(".wrapper-project ul li").toArray(), current_img = 0;
$(images_li[current_img]).show();
$(".btn-prev").click(function(){
$(images_li[current_img]).hide();
current_img = (current_img === 0) ? images_li.length - 1 : current_img - 1;
$(images_li[current_img]).show();
});
$(".btn-next").click(function(){
$(images_li[current_img]).hide();
current_img = (current_img === images_li.length-1) ? 0 : current_img + 1;
$(images_li[current_img]).show();
});