【问题标题】:How to pause and restart a animation within a setInterval?如何在 setInterval 内暂停和重新启动动画?
【发布时间】:2013-10-20 17:32:44
【问题描述】:

我编写了一个小代码,可以从屏幕顶部向下滑动图像。 我有一个暂停按钮来清除超时。但我无法停止 JS 动画并从停止的地方继续。

JSFiddle-Example

Code :
$.fn.pSlide = function(options) {
        var icount = 0,interval;
        var isPaused = false;
        documentHeight  = $('.image-container').height(),
        documentWidth   = $('.image-container').width(),
        imageHeight = 250;
        imageWidth = 250;
        defaults = {
            minSize     : 5,
            maxSize     : 15,
            newOn       : 8000, //controls duration of each element
        },
        options = $.extend({}, defaults, options);

        $('#control-btn').click(function() {

            window.clearInterval(interval);
        });

        interval = window.setInterval(function() {
            if(!isPaused) {
                var $elm = $('.image-container img').eq(icount);
                var startPositionLeft   = Math.random() * documentWidth - imageWidth ,
                startOpacity        = 0.5 + Math.random(),
                endPositionTop      = documentHeight - 40,
                endPositionLeft     = startPositionLeft - 100 + Math.random() * 200,
                durationFall        = documentHeight * 10 + (Math.random() * 15000)+5000,   //controls the speed of transition
                rotateValue = Math.random();

                if(startPositionLeft<imageWidth) {
                    startPositionLeft = imageWidth;
                }

                if(rotateValue<=0.5) {
                    rotateValue = 0;
                }else{
                    rotateValue *= 10;
                    rotateValue *= Math.cos(Math.PI * Math.round(Math.random()));   //cos(0) = 1; cos(PI) = -1
                }

                if(icount==20) 
                    icount=0; 
                else 
                    icount++;

                $elm
                        // .clone()
                        // .appendTo($('.image-container'))
                        .css(
                            {
                                left: startPositionLeft,
                                opacity: startOpacity,
                                '-webkit-transform': 'rotate(' + rotateValue +'deg)',
                                '-moz-transform': 'rotate(' + rotateValue +'deg)',
                                '-o-transform': 'rotate(' + rotateValue +'deg)',
                                '-ms-transform': 'rotate(' + rotateValue +'deg)',
                                'zoom' : 1
                            }
                        )
                        .animate(
                            {
                                top: endPositionTop,
                                left: endPositionLeft,
                                opacity: 0.2
                            },
                            durationFall,
                            'linear',
                            function() {

                                $(this).css({'top':'-250'+'px' , '-webkit-transform': 'rotate(0)'});
                            }
                        );
                }

                },options.newOn);
    };

【问题讨论】:

    标签: javascript jquery


    【解决方案1】:

    答案是您需要使用队列和出队,这是关于同一主题的问题。 jQuery Pause / Resume animate

    【讨论】:

    • 感谢您的回复。我已经更新了我的代码jsfiddle.net/utzqZ/3 现在我可以暂停动画,但由于它在 setInterval 内,所以我无法理解如何恢复它。
    • 对暂停/恢复有任何帮助吗? :(
    • 看来我们需要写一个插件,我有时间会做的
    • 顺便说一句,您是否考虑过使用 greensock 动画库,它内置了所有这些功能,并且说它比 JQuery 更快。 greensock.com/get-started-js
    猜你喜欢
    • 2012-01-22
    • 1970-01-01
    • 2015-06-18
    • 2020-04-26
    • 1970-01-01
    • 2019-09-21
    • 2013-10-25
    • 1970-01-01
    相关资源
    最近更新 更多