Friday, May 10, 2024
 Popular · Latest · Hot · Upcoming
12
rated 0 times [  19] [ 7]  / answers: 1 / hits: 29618  / 12 Years ago, thu, august 9, 2012, 12:00:00

I have multiple divs (haveing same class names). I want to move the div (always same div, which has unique ID #pos1) to the div which has been clicked. So, for that purpose, I am using following code to find the position1 (of the div which I want to move) and pos2 (the div which is clicked).



However, I don't know know how can I move (animate etc) the div from one position to another. I will appriciate any help.



jQuery(.container).click(function() {

var pos1 = jQuery(#pos1).position();
alert(pos1.top + ', ' + pos1.left);

var pos2 = jQuery(this).position();
alert(pos2.top + ', ' + pos2.left);

});

More From » jquery

 Answers
32

First of all make sure that all your .container divs are position:absolute



Then you can use the following animate function of jQuery:



$('.container').click(function(){
var pos1 = $('#pos1').position();

$(this).animate({ 'top': pos1.top + 'px', 'left': pos1.left + 'px'}, 200, function(){
//end of animation.. if you want to add some code here
});
});

[#83742] Wednesday, August 8, 2012, 12 Years  [reply] [flag answer]
Only authorized users can answer the question. Please sign in first, or register a free account.
mckenna

Total Points: 445
Total Questions: 109
Total Answers: 109

Location: Virgin Islands (U.S.)
Member since Sun, May 16, 2021
3 Years ago
;