Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Save PatrickDinh/06d42322f9ba5e86af0c416c2c84dcfa to your computer and use it in GitHub Desktop.
Save PatrickDinh/06d42322f9ba5e86af0c416c2c84dcfa to your computer and use it in GitHub Desktop.
get element position JS
function getOffset(el) {
el = el.getBoundingClientRect();
return {
left: el.left + window.scrollX,
top: el.top + window.scrollY
}
}
var el = document.getElementById('my-beautiful-div');
var position = getOffset(el);
console.log(position);
window.scroll(0, position.top);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment