Jquery () position includes margin

If I want to enable margin when measuring the width of an element, I can call element.outerWidth(true); However, I cannot find a similar way to get the left offset of an element in the container where the margin is on, element.position().left does not contain a field.

I tried element[0].getBoundingClientRect().left and this works, but is there a similar jquery call?

EDIT: It seems that the native javascript call above does not give me a field.

+7
javascript jquery position
source share
2 answers

This is the jQuery.position () constraint that has this limitation:

Note. jQuery does not support getting the position coordinates of hidden elements or taking into account the borders, margins or indents set on the body element.

Recommended Solution:

 var position = $element.position(); x = position.left + parseInt($element.css('marginLeft'), 10); y = position.top + parseInt($element.css('marginTop'), 10); 
+15
source share

Use jquery () offset function

 <!doctype html> <html lang="en"> <head> <meta charset="utf-8"> <title>offset demo</title> <style> p { margin-left: 10px; } </style> <script src="https://code.jquery.com/jquery-1.10.2.js"></script> </head> <body> <p>Hello</p><p>2nd Paragraph</p> <script> var p = $( "p:last" ); var offset = p.offset(); p.html( "left: " + offset.left + ", top: " + offset.top ); </script> </body> </html> 
-one
source share