Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

jQuery: Slide element from a position to a position

I would like to use jQuery to move an object. I would like to animate this movement to move the element from its current vertical position to a new vertical position, as measured from the upper edge of its parent element.

To do this animation right now, I do the following:

var b = $(".another_object").position();
$(".object_to_move").animate({top:b.top},200);

This animates great, but the animation always has the object beginning at top of the parent (ie from top:0px;) to the desired location.

Does anyone know how I can alter this or use another function to slide from the current position to the desired one?

like image 253
jay Avatar asked Dec 27 '25 16:12

jay


1 Answers

var b = $(".another_object").position();
$(".object_to_move").animate({top:"+" + b.top},200);

Prefix it with a plus, and it will start where it currently is.

like image 96
Joseph at SwiftOtter Avatar answered Dec 31 '25 19:12

Joseph at SwiftOtter



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!