Skip to content

Instantly share code, notes, and snippets.

@allaniftrue
Created September 8, 2017 14:01
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 allaniftrue/e73c1e4a049077f4431d52dc5dc044a7 to your computer and use it in GitHub Desktop.
Save allaniftrue/e73c1e4a049077f4431d52dc5dc044a7 to your computer and use it in GitHub Desktop.
Vanilla Javascript scrolling
scrollToTop = (scrollDuration) => {
if(navigator.userAgent.match(/Trident\/7\./)) { // if IE
window.scrollTo(0, 0);
} else {
const scrollHeight = window.scrollY,
scrollStep = Math.PI / (scrollDuration / 15),
cosParameter = scrollHeight / 2;
var scrollCount = 0,
scrollMargin,
scrollInterval = setInterval(function() {
if (window.scrollY !== 0) {
scrollCount = scrollCount + 1;
scrollMargin = cosParameter - cosParameter * Math.cos(scrollCount * scrollStep);
window.scrollTo(0, (scrollHeight - scrollMargin));
}
else clearInterval(scrollInterval);
}, 15);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment