最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - How to pause and restart a css3 transition in jquery transit - Stack Overflow

programmeradmin0浏览0评论

I'm using jquery transit to move and element. I want to pause the animation and continue it on click of a button. But i'm unable to figure out how to do it.

JSFiddle : /

I did try

stop().transition({});

but it is throwing an error.

I'm using jquery transit to move and element. I want to pause the animation and continue it on click of a button. But i'm unable to figure out how to do it.

JSFiddle : http://jsfiddle/b4hwq/2/

I did try

stop().transition({});

but it is throwing an error.

Share Improve this question edited Feb 16, 2015 at 20:06 Jeff 12.4k10 gold badges53 silver badges89 bronze badges asked Oct 30, 2013 at 13:26 user1184100user1184100 6,90430 gold badges84 silver badges122 bronze badges
Add a ment  | 

3 Answers 3

Reset to default 2

You can use clearQueue() to stop the animation

clearQueue() : Stop the remaining functions in the queue

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

I have update the fiddle with the plete function from the transit library:

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

jsfiddle

The plete function is still executed even when using clearQueue() or stop()

You were most of the way there with .stop() I just added a little logic to tell it what to do when it stops.

Working Example

$('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"
    }
});
发布评论

评论列表(0)

  1. 暂无评论