admin管理员组文章数量:1406177
I've created a carousel using jQuery and I would like to add auto-play functionality to it.
Here's my existing 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')}
});
});
});
And here's a working fiddle.
Question: I have no idea where to start with auto-play. Any suggestions?
I've created a carousel using jQuery and I would like to add auto-play functionality to it.
Here's my existing 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')}
});
});
});
And here's a working fiddle.
Question: I have no idea where to start with auto-play. Any suggestions?
Share Improve this question edited Apr 13, 2012 at 15:14 Sampson 269k76 gold badges545 silver badges568 bronze badges asked Apr 13, 2012 at 14:25 LellyLelly 9683 gold badges15 silver badges29 bronze badges 04 Answers
Reset to default 2check this out:
http://jsfiddle/gy7LE/13/
$(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))});
}
This will work. See ments in code:
var slideInterval = null;
$(document).ready(function() {
$('#button a').click(function() {
//Clear slide interval to allow overriding of auto slide
if (slideInterval !== null) clearInterval(slideInterval);
var integer = $(this).attr('rel');
DoSlide(integer);
});
//Begin auto slide
slideInterval = setInterval(DoSlide , 2000);
});
function DoSlide(integer) {
integer = integer || parseInt($('.active').attr('rel')) + 1;
// Reset auto slide
if (integer == 5) integer = 1;
$('#myslide .cover').animate({
left: -705 * (parseInt(integer) - 1)
});
$('.active').removeClass('active');
$('a[rel="' + integer + '"]').addClass('active');
}
Here's a working fiddle to demonstrate.
A more elegant solution
$(document).ready(function() {
setTimeout(function () {
$('.carousel-control.right').trigger('click');
$('.carousel-inner').trigger('mouseenter');
$('.carousel-inner').trigger('mouseleave');
}, 3000); //set this time to what ever time you want it to take to start scrolling this is separate for the interval you set for the carousel though you can set it to be the same BooStrap default is 4000
});
you can try this plugin, it looks for images for you and creates auto carousel.
本文标签: javascriptAdd autoplay to this jQuery CarouselStack Overflow
版权声明:本文标题:javascript - Add autoplay to this jQuery Carousel - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1744972381a2635311.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论