Last active
August 29, 2015 14:04
-
-
Save dhilipsiva/fcca211882fc4f47c3a7 to your computer and use it in GitHub Desktop.
A smooth scroll to script
This file contains hidden or 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
window.smoothScrollTo = (target_id, duration=1000) -> | |
step = -> | |
factor = (Date.now() - start) / duration # get interpolation factor | |
if factor >= 1 | |
clearInterval timer # stop animation | |
factor = 1 # clip to max 1.0 | |
y = factor * delta + offset | |
window.scrollBy 0, y - window.pageYOffset | |
target = document.getElementById(target_id).offsetTop | |
offset = window.pageYOffset | |
delta = target - window.pageYOffset | |
start = Date.now() | |
factor = 0 | |
clearInterval timer if timer | |
timer = setInterval(step, 10) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment