当前的代码问题
i
具有全局范围,因此下次循环尝试迭代i
= 5,这会导致循环不迭代。将声明移动i
到匿名函数中。
$('.right_trigger').click(function(event) {
var i=0;
event.preventDefault();
while (i<5) {
$('#skater_mulitivew li').eq(0).fadeOut('slow');
$('#skater_mulitivew li').eq(1).fadeOut('slow');
i++;
});
});
当前代码的优化
附带说明一下,我不确定您为什么将fadeOut()
函数称为 5x。您可以通过以下方式实现相同的效果:
$('.right_trigger').click(function(event) {
event.preventDefault();
//If your trying to fadeout all li use the proceeding commented code
//$('#skater_mulitivew li").fadeOut("slow");
//If your actually trying to target by index use the proceeding code
$('#skater_mulitivew li:eq(0),#skater_mulitivew li:eq(1) ').fadeOut('slow');
i++;
});
轮播实现
鉴于评论,您似乎想要制作类似轮播的功能。这是实现这一目标的 html、css 和 javascript。
HTML
<ul id="skater_mulitivew">
<li class="first_gallery">Gallery 1</li>
<li class="second_gallery">Gallery 2</li>
<li class="third_gallery">Gallery 3</li>
<li class="fourth_gallery">Gallery 4</li>
</ul>
<div class="right_trigger">Trigger</div>
CSS
#skater_mulitivew li{
display:none;
}
#skater_mulitivew li:first-child{
display: block;
}
Javascript
$('.right_trigger').click(function(event) {
var items = $("#skater_mulitivew li");
toggle(items,0);
});
function toggle(items, index){
items.eq(index).fadeOut('slow', function(){
index = (index == items.length -1) ? -1:index;
items.eq(index + 1).fadeIn('slow', function(){
toggle(items, ++index);
});
});
}
工作示例:http: //jsfiddle.net/njxnN/1/