如何在 jquery transit 中暂停和重新启动 css3 转换

How to pause and restart a css3 transition in jquery transit

本文关键字:重新启动 css3 转换 暂停 jquery transit      更新时间:2023-09-26

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

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

我确实试过

stop().transition({});

但它抛出了一个错误。

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

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

js小提琴在这里

.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);
    });
});

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

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

斯菲德尔

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

你大部分时间都在那里.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"
    }
});