Last active
June 25, 2021 16:24
-
-
Save caryfuk/3b7bb50831421f1873aa to your computer and use it in GitHub Desktop.
alternative to Element.getBoundingClientRect() by https://github.com/kirupa
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
function getPosition(elm) { | |
var xPos = 0, yPos = 0; | |
while(elm) { | |
xPos += (elm.offsetLeft - elm.scrollLeft + elm.clientLeft); | |
yPos += (elm.offsetTop - elm.scrollTop + elm.clientTop); | |
elm = elm.offsetParent; | |
} | |
return { x: xPos, y: yPos }; | |
} |
very useful for sticky feature.
var pos_t = getPosition(i_sticky_i);
alert(pos_t.x);
ok.
It's alternative, but what's the purpose? This will cause reflow several times instead of one using getBoundingClientRect
this is even worse than getBoundingClientRect
.
I was trying to use this 'getBoundingClientRect'. but every time i get an error of null. I used this and it works! Wow... Great work 💯
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
It was helpful for me. But how to pass parameter? I have added function call with parameter.
getPosition(window.frameElement);
Thanks.