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();
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.
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.
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.
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.
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();
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