Last active
February 23, 2021 15:06
-
-
Save moeamaya/6995c83651cba2cf7416 to your computer and use it in GitHub Desktop.
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
// Get element offset from top of page | |
function getOffset(el) { | |
el = el.getBoundingClientRect(); | |
return { | |
left: el.left + window.scrollX, | |
top: el.top + window.scrollY | |
}; | |
}; |
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
// Scroll element to a integer position | |
function scrollTo(element, to, duration) { | |
if (duration <= 0) return; | |
var difference = to - element.scrollTop; | |
var perTick = difference / duration * 10; | |
setTimeout(function() { | |
element.scrollTop = element.scrollTop + perTick; | |
if (element.scrollTop === to) return; | |
scrollTo(element, to, duration - 10); | |
}, 10); | |
}; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment