views:

20

answers:

1

I have an image with a click event handler that captures the location where you clicked.

$("#image").click(function(e)
{
    var x = e.pageX - $(this).offset().left;
    var y = e.pageY - $(this).offset().top;
});

I want it so that when the image is clicked an image appears at that location on top of the image. How do I do this?

+1  A: 

Use absolute positioning and the top and left CSS attributes to place the image over the co-ordinates you calculate.

If the image you want to position is as below:

<img id="move-me" style="position:absolute;display:none;z-index:99" src="/somewhere.jpg"/>

Have the following procedure in your code:

$('#move-me').css({
   left: x-coord,
   top: y-coord
}).show();

The z-index property ensures the image is shown overlays all other elements on the page...

Matt
This is superb - thanks. How do I extend this so that I can have N images at N points?
Chris
You'd have to have a method to relate the image you want to display to the point you click on. You might do this by an image map (http://www.w3schools.com/TAGS/tag_map.asp). Capture the `click` event of the image map instead of the actual image, and then show the image you've assigned to that area; and position it using the same method as before.
Matt
Sounds a bit difficult, I might just have 10 images hidden then just remember how many I've put down. Thanks
Chris