use*_*145 13 javascript jquery position
如果我想在测量元素宽度时包含边距我可能会调用element.outerWidth(true);但是,我找不到类似的方法来获取容器中元素的左偏移量,其中包含边距.element.position().left不包括保证金.
我已经尝试过element[0].getBoundingClientRect().left,但是有效,但有一个类似的jquery调用吗?
编辑:似乎上面的本机JavaScript调用也没有给我保证金..
Phi*_*voy 20
这是jQuery的.position()的限制,它有这个限制:
注意: jQuery不支持获取隐藏元素的位置坐标或者考虑body元素上的边框,边距或填充集.
推荐解决方案
var position = $element.position();
x = position.left + parseInt($element.css('marginLeft'), 10);
y = position.top + parseInt($element.css('marginTop'), 10);
Run Code Online (Sandbox Code Playgroud)