Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do you speed up an animation mid animation with jQuery?

Suppose you have an animation running with a certain time like this:

$('span').animate({opacity : 1}, 10000);

The animation is quite long so the user tries clicking the button again. The animation will be a certain amount of time through the animation already, which is probably going to be different every time.

On the second click is it possible to update the animation process keeping the opacity of the object when the user clicks, just changing the time it will take to finish?

Basically I want to update the animation process mid way through the animation.

like image 303
Johnny Avatar asked Sep 14 '12 19:09

Johnny


People also ask

How do you change the speed of an animation in CSS?

The animation-timing-function specifies the speed curve of an animation. The speed curve defines the TIME an animation uses to change from one set of CSS styles to another. The speed curve is used to make the changes smoothly.

What are speed options in jQuery?

speed − A string representing one of the three predefined speeds ("slow", "normal", or "fast") or the number of milliseconds to run the animation (e.g. 1000). callback − This is optional parameter representing a function to call once the animation is complete.

Can the jQuery animate () method be used to animate any CSS property?

The animate() method is typically used to animate numeric CSS properties, for example, width , height , margin , padding , opacity , top , left , etc. but the non-numeric properties such as color or background-color cannot be animated using the basic jQuery functionality. Note: Not all CSS properties are animatable.


2 Answers

You can use the step option of animate to keep track of how far along the animation is. Then with that information, you can calculate the time remaining in the animation. Then stop the current animation and start a new one with half the duration.

http://jsfiddle.net/MdD45/

EDIT

It looks like the 2nd parameter passed to step contains a property named pos which tells you what percentage along in the animation you are. That can simplify things further.

http://jsfiddle.net/MdD45/1/

var startVal = 0;
var endVal = 1;
var duration = 10000;

var howfar = 0;

$('span').css("opacity",startVal)
    .animate({
        opacity : endVal
    }, {
        duration: duration,
        step: function(now, fx){
            howfar = fx.pos;  // between 0 and 1, tells how far along %
        }        
    });

$("button").click(function(){
    // calculate the new duration as half of the remaining duration
    var timeRemaining = duration - (howfar * duration);
    duration = timeRemaining / 2;

    $('span').stop().animate({
        opacity : endVal
    }, {
        duration: duration,
        step: function(now, fx){
            howfar = fx.pos;  // between 0 and 1, tells how far along %
        }        
    });
});
​
like image 148
James Montagne Avatar answered Sep 24 '22 23:09

James Montagne


I put something together yesterday to skip in jQuery animations, here's the code, it should be pretty easy to modify for your use-case:

EDIT: Modified version:

Demo: http://jsfiddle.net/SO_AMK/fJyKM/

jQuery:

var time = 10000;
var opacity = 1;
var currentTime = 0;

$("#square").animate({
    opacity: opacity
}, {
    duration: time,
    step: function(now, fx) {

        currentTime = Math.round((now * time) / opacity);

    },
    easing: "linear"
});


$("#hurry").click(function() {
    $("#square").stop().animate({
        opacity: opacity
    }, {
        duration: ((time - currentTime) / 4), // Get remaining time, divide by 4
        step: function(now, fx) {

            currentTime = Math.round((now * time) / opacity);

        },
        easing: "linear"
    });
});​

It also works for other properties, like width. The only catch is that if it is a decreasing value than you need to use a different script.

like image 40
A.M.K Avatar answered Sep 26 '22 23:09

A.M.K