Skip to content

Instantly share code, notes, and snippets.

@caryfuk
Last active June 25, 2021 16:24
Show Gist options
  • Star 5 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save caryfuk/3b7bb50831421f1873aa to your computer and use it in GitHub Desktop.
Save caryfuk/3b7bb50831421f1873aa to your computer and use it in GitHub Desktop.
alternative to Element.getBoundingClientRect() by https://github.com/kirupa
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 };
}
@swapon-ahamed
Copy link

It was helpful for me. But how to pass parameter? I have added function call with parameter.
getPosition(window.frameElement);
Thanks.

@ilgice
Copy link

ilgice commented Jan 26, 2018

very useful for sticky feature.

var pos_t = getPosition(i_sticky_i);
alert(pos_t.x);

ok.

@rootical
Copy link

It's alternative, but what's the purpose? This will cause reflow several times instead of one using getBoundingClientRect

@xinghul
Copy link

xinghul commented Jun 26, 2019

this is even worse than getBoundingClientRect.

@iamabhshk
Copy link

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