4

我正在使用 jquery transit 来移动和元素。我想暂停动画并单击按钮继续。但我不知道该怎么做。

JSFiddle:http: //jsfiddle.net/b4hwq/2/

我确实尝试过

stop().transition({});

但它抛出了一个错误。

4

3 回答 3

2

您可以使用clearQueue()停止动画

clearQueue() : 停止队列中剩余的函数

jsFiddle here

HTML

<div class="box"></div>
<button id="restartTransition">Click Me</button>

CSS

.box{
    opacity: 0.8;
position: absolute;
left: 0;
top: 5%;
background: #505060;
border-radius: 1px;
box-shadow: inset 0 0 0 2px rgba(0, 0, 0, 0.2);
margin: -16px 0 0 -16px;
width: 32px;
height: 32px;
z-index: 2;
}

jQuery

$(document).ready(function(){
    $("#restartTransition").on("click", function(){
        $('.box').clearQueue().transition({ x: '0px' },10);
        $('.box').transition({ x: '350px' },5000);
    });
});
于 2013-10-30T13:39:03.913 回答
1

我已经用运输库中的完整功能更新了小提琴:

$('.box').transition({
            x: '350px',
            duration: 5000,
            complete: function() {
                alert("complete!");
            }
        });

jsfiddle

即使使用 clearQueue() 或 stop() 仍会执行完整的函数

于 2015-04-30T07:43:00.280 回答
0

你大部分时间都在那里,.stop()我只是添加了一些逻辑来告诉它停止时该怎么做。

工作示例

$('button').click(function () {
    if (!$('.box').hasClass('stop')) { // if the box does not have class "stop"
        $('.box').stop().transition({  // stop the transition
            x: $('.box').offset().left + $('.box').width() / 2  // where the box should be when it stops
        }, 1).addClass('stop'); // simple add class for easy button control  
    } else { // if the box has class "stop"
        $('.box').transition({
            x: '350px' 
        }, 5000).removeClass('stop'); // continue with regularly scheduled programming then remove "stop"
    }
});
于 2013-10-30T14:15:08.483 回答