tags:

views:

83

answers:

3

I have a number of small images that I display that are the size of an icon (32x32). I want to animate them from their current position on the page to the x,y of a DIV anytime someone clicks on any of the images. What is the best route to do this using jQuery?

+1  A: 

If I understand the question correctly, then you can change the css properties to that of the image. I.e. contain the images within the div and move the div to the location of the clicked icon.

$("#divname").css({'width' : '32', 'height' : '32' , 'left' : 'IMAGELEFT', 'top' : 'IMAGETOP'});

Assuming I understand the question.

EDIT:

To move the image to the div:

$("#image").click(function(){
  $(".block").animate({'left': 'DIVPOSITION', 'top', 'DIVPOSITION'}, 'slow', function() {
 $("#div").html("<img src=\"THEIMAGE\">");
   });
 );
});
Gazler
Sorry I wasn't very clear originally. I've revised to bring better clarity. I want to use the animate feature to make it move from where the icon is to the x,y of a particular DIV.
Nissan Fan
You want to move the image when clicked to inside the DIV, correct?
Gazler
+1  A: 

It looks like there are already some jquery plugins that do what you're looking for.

From the link, CJ Object Scaler should be able to make sure that your images fit within the specified size. JQUERY IMAGE MAGNIFY handles animation and transparency with images.

smencer
Sorry I wasn't very clear originally. I've revised to bring better clarity. I want to use the animate feature to make it move from where the icon is to the x,y of a particular DIV.
Nissan Fan
See here: http://api.jquery.com/animate/Basically you assign a set of CSS properties for the image that will be the end state, a duration and a callback function. The CJ Object Scaler can still help to make sure the image will fit in the DIV properly.
smencer
+1  A: 

Ok just from the top of my head, you can use the offset() function to get the x and y coordinates of both the image and the div you want the image to move to. offset().left and offset().right.

Then I also think you'll need to make sure the image is positioned absolutely for this to work correctly. Then just use the animate() function on the image element and add the new x and y position in the animate() function, to make the image go there.

Go to docs.jquery.com and read up on the parameters the animate() function need.

I'll try and come up with code examples if you're struggling.

Ettiene