Search code examples
javascriptjquerycsscss-transitionsjquery-transit

How to pause and restart a css3 transition in jquery transit


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.net/b4hwq/2/

I did try

stop().transition({});

but it is throwing an error.


Solution

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