Created
October 11, 2017 18:24
-
-
Save Johannestegner/54612610bc33a09743ccda694fd3ac2c to your computer and use it in GitHub Desktop.
JavaScript scoller start code.
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
export default function(targetSelector, speed) { | |
let current = ( | |
(document.documentElement.scrollTop || document.body.scrollTop) | |
- | |
(window.innerHeight / 2) | |
); | |
let target = document.querySelector(targetSelector).getBoundingClientRect(); | |
let offset = (target.bottom - (target.height / 2)) + current; | |
let lastFrame = Date.now(); | |
let move = function() { | |
let delta = (Date.now()-lastFrame) / 1000; | |
lastFrame = Date.now(); | |
if (window.scrollY - offset >= 1) { | |
return; | |
} | |
window.scrollBy(0, (offset * (delta * speed))); | |
requestAnimationFrame(move); | |
}; | |
move(); | |
} | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment