Getting every offsetParent or the total offSetTop and total offSetLeft
-
14-04-2021 - |
题
I want to get the total offSetTop and the total offSetLeft of a child element which have many level of parent element and may be adding up.
Is that any shorthand way, besides of adding one by one in manual ways?
解决方案
using jQuery: $( node ).offset()
then .top
and .left
其他提示
To provide an answer without jQuery:
var a = Element, b = 0, c = 0;
while (a) {
b += a.offsetLeft;
c += a.offsetTop;
a = a.offsetParent;
}
Where Element
is your Element node for which you need an offsetLeft
and offsetTop
.
不隶属于 StackOverflow