Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

jQuery animate += and latest version

Each time I click the div "blue" it moves 100px. It worked well and one day I realize it stopped working. After trying a lot of things I discovered that the problem is with the latest version of jQuery 1.10 Now it just moves 100px once. It is as it ignores the += . I could not find if it is deprecated? If so, what is the right way to do that now?

You could see it working here:http://jsfiddle.net/RB4eJ/1/
(This is working in jQuery 1.9.1. But it is not in 1.10)

$(function(){
    $(".blue").click(function() {
        $(".blue").animate({left: "+=100"}, 500)    
    });
})
like image 962
Nrc Avatar asked May 29 '13 16:05

Nrc


People also ask

Is jQuery used for animation?

With jQuery, you can create custom animations.

What is jQuery animate?

Definition and Usage. The animate() method performs a custom animation of a set of CSS properties. This method changes an element from one state to another with CSS styles. The CSS property value is changed gradually, to create an animated effect. Only numeric values can be animated (like "margin:30px").

What is the default easing used for jQuery animation?

An easing function specifies the speed at which the animation progresses at different points within the animation. The only easing implementations in the jQuery library are the default, called swing , and one that progresses at a constant pace, called linear .


1 Answers

If it is a bug I hope they fix it because it was useful. However for now you can do something like this:

$(".blue").click(function() {
    var new_left = +$(this).css("left").replace("px", "") + 100;
    $(".blue").animate({left: new_left + "px"}, 500)    
});

Or as @adeneo suggested:

$(".blue").click(function() {
    $(this).animate({left: $(this).position().left+100}, 500);  
});

See working demo with jQuery 2.x

Performance test

like image 132
letiagoalves Avatar answered Oct 13 '22 08:10

letiagoalves