我有一个简单的动画淡入和淡出无序列表中的每个列表项。HTML 标记如下:
<ul>
<li>first par</li>
<li>second par</li>
<li>third par</li>
</ul>
<div></div>
空 div 将包含通过 jquery 动态地导航控件的按钮。
脚本如下:
<script>
var ul = $('ul'), li = $('ul li'), li_length = li.length, div = $('div');
//hides all list items
li.hide();
function play (eq){
eq = (eq===undefined) ? 0 : eq;
for(i=eq, d=0; i<li_length; i++, d+=3000){
li.eq(i).delay(d).fadeIn(1000).delay(1000).fadeOut(1000);
}
}
//This dynamically appends the nav buttons in the div
li.each(function(i){
div.append('<button> '+ i + '</button>');
})
//the normal animation going through each list item, fading in and out
play();
var btn = $('div button');
//each button when clicked should animate the corresponding list item (fadein and out), then the next
li.each(function(i){
btn.eq(i).click(function(){
li.stop(true, true);
li.hide();
play(i);
})
})
</script>
运行时play()
,它完成了工作。但是,当您单击任何导航按钮时,例如正在播放第二个列表项,然后单击按钮 1(基本上调用play(1)
),play()
尽管我已经停止使用.stop()
. 结果是重叠的动画。我是一个菜鸟,对如何处理这个问题一无所知,这样动画就不会重叠。任何人都可以帮忙吗?