I need a popup div that will appear right below the anchor that i clicked. Now, I want to determine the x and y coordinates onClick event of an anchor. What's the best practice of doing it? What are the advisable event properties to use?
The HTML DO MouseEvent offsetX property returns the horizontal (x) coordinate of the mouse pointer relative to the target element if a mouse event was triggered. Use with offsetY to get the vertical coordinate as well.
pageX/Y coordinates are relative to the top left corner of the whole rendered page (including parts hidden by scrolling), while clientX/Y coordinates are relative to the top left corner of the visible part of the page, "seen" through browser window.
Definition and Usage. The pageX property returns the horizontal coordinate (according to the document) of the mouse pointer when a mouse event was triggered. The document is the web page. Tip: To get the vertical coordinate (according to the document) of the mouse pointer, use the pageY property.
offsetX and offsetY are relative to the parent container, whereas pageX and pageY are relative to the document. Don't confuse this with .offset()
and .position()
, in which .offset()
is relative to the document and .position()
is relative to the parent container's .offset()
.
Something like this example should work (JQuery):
$('a').click(function(){ var offset = $(this).offset(); $('#popup_div').css('top',offset.top + 'px').css('left',offset.left + 'px').show(); });
http://api.jquery.com/offset/
http://api.jquery.com/position/
2 extracts from Jquery Documentation website
The .position() method allows us to retrieve the current position of an element relative to the offset parent
http://api.jquery.com/position/
The .offset() method allows us to retrieve the current position of an element relative to the document.
http://api.jquery.com/offset/
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With