I just created a simple, continuous bounce effect in jQuery, but I feel that the code is not all that optimized, and I look to improve it.
var $square = $("#square");
bounce();
function bounce() {
$square.animate({
top: "+=10"
}, 300, function() {
$square.animate({
top: "-=10"
}, 300, function() {
bounce();
})
});
}
$square.hover(function() {
jQuery.fx.off = true;
}, function() {
jQuery.fx.off = false;
});
All I did was basically create an animation that adds +10 to the top coordinate of the element, and as a callback function, I subtract 10 from the top coordinate.
This creates a (almost smooth) bounce effect, but I feel it can be improved.
In addition, I want to stop the animation on mouseenterand continue it on mouseleave.
stop(true, true)didn't work, didn't dequeue(), so I resorted to disabling all animation effects with jQuery.fx.off = true(stupid, no?)
I would be grateful for any feedback on how this can be optimized.
jsFiddle.
EDIT: , jQuery too much recursion .
,