Created
September 8, 2017 14:01
-
-
Save allaniftrue/e73c1e4a049077f4431d52dc5dc044a7 to your computer and use it in GitHub Desktop.
Vanilla Javascript scrolling
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
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