简体   繁体   English

在点击时停止/启动动画循环? jQuery的

[英]stop/start animation loop on click? jquery

I am making a slideshow that continuously loops through 5 images etc. I have it looping fine but the loop won't start and stop. 我正在制作一个连续循环播放5张图像等的幻灯片。我可以循环播放,但循环不会开始和停止。 I was using a pause = true/false variable and adding if statements throughout the code - not very good looking or efficient (took it out of an example). 我使用的是pause = true / false变量,并在整个代码中添加了if语句-看起来不太美观或效率不高(从示例中删除)。 It would spoil the animations and images would go missing if the loop paused after the statements etc. 如果在语句等之后暂停循环,则会破坏动画,并且图像将丢失。

Are there any standard methods for controlling loops like this? 是否有控制这种循环的标准方法?

function setupSlideShow(){
    // Setup SlideShow stuff here then start the loop
    slideBoxTransition()
}

var page = 1;
function slideBoxTransition(){
    switch (settings['transition']['effect']){
        case "slide" :
            next.css({left:settings['width']+"px",top:0,display:"block"});
            current.animate({left: "-="+width+"px"}, {duration : speed, easing : easing, queue : false});
            next.animate({left: "-="+width+"px"}, {duration : speed, easing : easing, queue : false, complete : function(){
                current.css("display","none")
                slideBoxGetImage();
            }});
            break;
    }
}

function slideBoxGetImage(){
    current = $self.eq($self.index(next))
    page += 1;
    if(page < $self.length){
        next = $self.eq( $self.index(current) + 1 );
    }else{
        page = 0;
        next = $self.first();
    }
    slideBoxTimeout();
}

function slideBoxTimeout(){
    setTimeout(function(){
        slideBoxTransition();
    }, delay);
}

Anything would be much appreciated! 任何事情将不胜感激!

Thanks. 谢谢。

var interval;
function slideBoxTimeout(){
    var interval = setInterval(function(){
        slideBoxTransition();
    }, delay);
}

$(".play").click(function() {
    slideBoxTimeout();
});
$(".pause").click(function() {
    clearInterval(interval);
});

taken from : jQuery play/pause jQuery function 摘自: jQuery播放/暂停jQuery函数

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM