Navigation Menu

Skip to content

Instantly share code, notes, and snippets.

@Orlandster
Last active February 23, 2018 19:35
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save Orlandster/792bfaebe550f1afe100b9c731bacf80 to your computer and use it in GitHub Desktop.
Save Orlandster/792bfaebe550f1afe100b9c731bacf80 to your computer and use it in GitHub Desktop.
requestAnimationFrame - Snippets
// smooth to top scroll
(function smoothscroll(){
var currentScroll = document.documentElement.scrollTop || document.body.scrollTop;
if (currentScroll > 0) {
window.requestAnimationFrame(smoothscroll);
window.scrollTo (0,currentScroll - (currentScroll/5));
}
})();
// scroll event
var scroll = window.requestAnimationFrame;
function loop(){
// Avoid calculations if not needed
if (lastPosition == window.pageYOffset) {
scroll(loop);
return false;
} else lastPosition = window.pageYOffset;
//... calculations
scroll( loop );
}
// Call the loop for the first time
loop();
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment