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)
});
})
With jQuery, you can create custom animations.
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").
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 .
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
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With