Created
June 20, 2017 11:43
-
-
Save PatrickDinh/06d42322f9ba5e86af0c416c2c84dcfa to your computer and use it in GitHub Desktop.
get element position JS
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 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