Skip to content

Instantly share code, notes, and snippets.

@caryfuk
Last active June 25, 2021 16:24
Show Gist options
  • 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 };
}
@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