Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What is the difference between offsetX,offsetY and pageX,pageY?

Tags:

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?

like image 676
Karina Avatar asked Jul 11 '11 05:07

Karina


People also ask

What is offsetX and offsetY?

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.

What is the difference between clientX and pageX?

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.

What is pageX?

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.


2 Answers

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/

like image 190
AlienWebguy Avatar answered Oct 21 '22 05:10

AlienWebguy


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/

like image 41
Praveen Prasad Avatar answered Oct 21 '22 04:10

Praveen Prasad