Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- // Scroll to top (Event Listenter)
- document.querySelector('#to-top').onclick = () => {
- scrollTo(document.documentElement, 0, 500);
- }
- // Scroll to function
- const scrollTo = (element, to, duration) => {
- let start = element.scrollTop,
- change = to - start,
- currentTime = 0,
- increment = 20;
- let animateScroll = () => {
- currentTime += increment;
- let val = Math.easeInOutQuad(currentTime, start, change, duration);
- element.scrollTop = val;
- if (currentTime < duration) {
- setTimeout(animateScroll, increment);
- }
- };
- animateScroll();
- }
- // Ease function
- Math.easeInOutQuad = (t, b, c, d) => {
- t /= d / 2;
- if (t < 1) return c / 2 * t * t + b;
- t--;
- return -c / 2 * (t * (t - 2) - 1) + b;
- };
Add Comment
Please, Sign In to add comment