Getting every offsetParent or the total offSetTop and total offSetLeft
-
14-04-2021 - |
Question
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?
Solution
using jQuery: $( node ).offset()
then .top
and .left
OTHER TIPS
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
.
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow