user1519225
user1519225

Reputation: 41

How to animate to a specific position using jQuery

I've searched high and low and cannot seem to find a solution.

I want to be able to click on a div and have it animate to specific location (say, x:200 y:200) regardless of where it already lives on the page. For example, if I have 4 divs scattered on my page, I want to be able to have them animate to a unified coordinate. Does anyone know how I can accomplish this? It would be very much appreciated. I've tried various tricks using .position and .offset but none seem to work.

Thanks.

Upvotes: 1

Views: 2998

Answers (1)

HRÓÐÓLFR
HRÓÐÓLFR

Reputation: 5992

Use .animate(). look at this example:

$('#myDiv').animate({top: '50px', left: '50px'}, 2000, function () {
    console.log('done!');
});

it will animate the div with id "myDiv" to (50,50) over a period of 2 seconds.

Upvotes: 2

Related Questions