tags:

views:

82

answers:

3

I am trying re-create the facebook tagging functionality, where you click on an image and the part of the image you click on displays an empty box (or a square).

Using jQuery, how do I make the box appear around my cursor on the click?

Thanks

+2  A: 

Something like this might be able to help you, where "image" is the class of image that is taggable and "box" is the ID of an absolutely position element:

$(".image").click(function(e){
    $("#box").css('top',e.pageY).css('left',e.pageY);   
});
Sam152
Thanks for this. If you don't mind me asking, where can I find info on this e.pageY element? What is that called? Is that a CSS element or jQuery element?Thanks.
marcamillion
@marcamillion It's called an event object, it's pretty useful and you can get more info on it here: http://api.jquery.com/category/events/event-object/
Sam152
+1  A: 
$(function(){
  $(document).click(function(e){
    $("<div>").
      text("Foo").
      css({ position: "absolute", top: e.pageY, left: e.pageX }).
      appendTo(document.body);
  });
});

You can find more information about jQuery event objects in their docs for events. In this example, I am using event.PageX and event.PageY.

Raul Agrait
Thanks Raul. It seems that Sam152 beat you to it, but this definitely helps me with my issue. Do you happen to know where I can go to learn more about the 'e.Page' elements that you used here? What are they called and how do I even Google them? I want to make sure I am using them correctly, and don't want to just copy/paste without understanding :)Thanks.
marcamillion
@marcamillion I update my answer to include references to the jQuery event documentation.
Raul Agrait
+1  A: 

Ugly code warning:

<script type="text/javascript">
  $(document).ready(function() {
    $("#image-wrapper").click(function(e){
      var ele = $("<div>");
      ele.css({width:"50px", height:"50px", border:"1px solid green", position:"absolute", left: e.pageX - 25, top: e.pageY -25});
      $("body").append(ele);      
   }); 
  });
</script>

<div id="image-wrapper" style="border: 1px solid red; width: 300px; height: 200px;">
</div>
Kevin Wiskia
This is awesome. It ALMOST does exactly what I want, if I combine it with the above two, I will be able to achieve exactly what I am trying to.Thanks...this is awesome!
marcamillion
Hey Kevin, This one has proved to be the most efficient for me. The issue is that with every box click, the box stays on the image. How do I adjust it so that the box comes off when I click elsewhere?i.e. if I click in 10 different places, the only place I see the box is in the last clicked position...I am not seeing 9 - 10 boxes.Thanks.
marcamillion
Modified it for you: http://dakman.pastebin.com/K1FsekqH
Kevin Wiskia