我使用 jQuery 创建了一个旋转木马,我想为其添加自动播放功能。
这是我现有的 JS:
$(document).ready(function (){
$('#button a').click(function(){
var integer = $(this).attr('rel');
$('#myslide .cover').animate({left:-705*(parseInt(integer)-1)})
$('#button a').each(function(){
$(this).removeClass('active');
if($(this).hasClass('button'+integer)){
$(this).addClass('active')}
});
});
});
这里是 a working fiddle .
问题:我不知道从哪里开始自动播放。有什么建议吗?
最佳答案
检查一下:
$(document).ready(function (){
$('#button a').click(function(){
var integer = $(this).attr('rel');
$('#myslide .cover').animate({left:-705*(parseInt(integer)-1)})
$('#button a').each(function(){
$(this).removeClass('active');
if($(this).hasClass('button'+integer)){
$(this).addClass('active')}
});
});
setInterval ( function(){Next();}, 1000 );
});
function Next(){
var _next = false;
$('#button a').each(function(){
if(_next){
$(this).addClass('active');
_next = false;
}
else if($(this).hasClass('active')){
_next = true;
$(this).removeClass('active')
}
});
if(_next)
$("#button a:eq(0)").addClass("active");
var activeIndex = parseInt($(".active").attr("rel"));
$('#myslide .cover').animate({left:-705*(parseInt(activeIndex))});
}
关于javascript - 将自动播放添加到此 jQuery 轮播,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/10142662/