Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to find the element's x center coordinates and related window offset

i would like to retrieve the element offset starting from his own x center coordinates.

how can i do it?

Actually i can find the window offset of an element but it retrieves the coordinates from the border of the element like this:

var _position = $(this).offset(); 
like image 842
itsme Avatar asked Nov 06 '11 14:11

itsme


People also ask

How do you find the coordinates of an element?

jQuery . offset() will get the current coordinates of the first element, or set the coordinates of every element, in the set of matched elements, relative to the document.

How do I find the x and y coordinates of a website?

In order to find the coordinates of the top left corner of the HTML page, you can use the web browser's instance properties DisplayRectangleX and DisplayRectangleY. For example, after storing a browser's instance into the variable %Browser%, then %Browser. DisplayRectangleX% will return the X dimension and %Browser.

How do you find XY coordinates of a div?

If the element is in the main document you can get the DIV's coordinates with... var X=window. getComputedStyle(abc,null). getPropertyValue('left'); var Y=window.

What is element offset?

Definition and Usage. The offset() method set or returns the offset coordinates for the selected elements, relative to the document. When used to return the offset: This method returns the offset coordinates of the FIRST matched element. It returns an object with 2 properties; the top and left positions in pixels.


1 Answers

You have to use offset() to get the top and left position, then add half of the height() and width() values to them. That gives the center coordinates.

var $this = $(this); var offset = $this.offset(); var width = $this.width(); var height = $this.height();  var centerX = offset.left + width / 2; var centerY = offset.top + height / 2; 

If you need to consider the padding property in your calculations, use the following:

var width = $this.outerWidth(); var height = $this.outerHeight(); 
like image 112
Rob W Avatar answered Sep 20 '22 23:09

Rob W