Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to get elements clientX and clientY of an element

Is it possible to translate pageX, pageY to clientX, clientY using DOM API?

I have an element of which I need to find clientX and clientY. I can easily get pageX and pageY using jQuery's offset method, but I am interested in getting clientX and clientY. There are no mouse events involved.

like image 868
chvs2000 Avatar asked May 13 '16 01:05

chvs2000


People also ask

How do I get clientX in Javascript?

The clientX property returns the horizontal coordinate (according to the client area) of the mouse pointer when a mouse event was triggered. The client area is the current window. Tip: To get the vertical coordinate (according to the client area) of the mouse pointer, use the clientY property.

What is 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.

How do I get getBoundingClientRect?

The getBoundingClientRect() method returns the size of an element and its position relative to the viewport. The getBoundingClientRect() method returns a DOMRect object with eight properties: left, top, right, bottom, x, y, width, height.

What is element getBoundingClientRect ()?

The Element. getBoundingClientRect() method returns a DOMRect object providing information about the size of an element and its position relative to the viewport.


1 Answers

var element = document.getElementById(...);
var clientRect = element.getBoundingClientRect();
var clientX = clientRect.left;
var clientY = clientRect.top;

The Element.getBoundingClientRect() method returns the size of an element and its position relative to the viewport.

Reference: https://developer.mozilla.org/en-US/docs/Web/API/Element/getBoundingClientRect

like image 55
Dinh Tran Avatar answered Sep 19 '22 07:09

Dinh Tran