Scroll element to top
As mentioned in the Scroll to an element post, we can scroll to given element smoothly by passing behavior: 'smooth': ele.scrollIntoView({ behavior: 'smooth' }); or applying the CSS property scroll-behavior to the target element: Both methods aren't supported in IE and Safari, and don't allow to customize the animation. This post introduces a smoothly scroll implementation which also allows us to customize the animation effect and duration. We'll demonstrate in a popular use case that user can jump between sections by clicking associated navigation button. The navigation consists of some a elements: ... ... Clicking the link will scroll the page to a particular element that can be determined by the href attribute. const triggers = [].slice.call(document.querySelectorAll('.trigger')); triggers.forEach(function (ele) { ele.addEventListener('click', clickHandler); The clickHandler function handles the click event of a navigation element. It determintes the target section based on the href attribute. Notice that we will scroll to the target section ourselves, hence the default action will be ignored: const clickHandler = function (e) { const href = e.target.getAttribute('href'); const target = document.getElementById(id); Don't worry if you haven't seen the scrollToTarget function. As the name implies, the function will scroll the page to given target. Scroll to given targetIt is the main part of the post. To scroll to given point, we can use window.scrollTo(0, y) where y indicates the distance from the top of the page to the target. It's also possible to set behavior: 'smooth': window.scrollTo({ top, left, behavior: 'smooth' }); What we're going to do is to move from the starting point to the ending point in given duration.
const scrollToTarget = function (target) { const top = target.getBoundingClientRect().top; const startPos = window.pageYOffset; const loop = function (currentTime) { const time = currentTime - startTime; const percent = Math.min(time / duration, 1); window.scrollTo(0, startPos + diff * percent); requestId = window.requestAnimationFrame(loop); window.cancelAnimationFrame(requestId); requestId = window.requestAnimationFrame(loop); As you see, we tell the browser to execute the loop function before the next paint happens. At the first time, startTime will be initialized as the current timestamp (currentTime). We then calculate how many milliseconds has gone: const time = currentTime - startTime; Based on the elapsed time and the duration, it's so easy to calculate the number of percentages we have been moving, and scroll to that position: const percent = Math.min(time / duration, 1); window.scrollTo(0, startPos + diff * percent); Finally, if there's remaining time, we continue looping. Otherwise, we cancel the last request: requestId = window.requestAnimationFrame(loop); window.cancelAnimationFrame(requestId); It's common to think of using setTimeout() or setInterval() when we need to move between given points in the given duration. But it's recommended to use requestAnimationFrame which gives better performance animation. Customize the animationCurrently, we move to the target equally per millisecond. We move the same distance every milliseconds. If you want, you can replace the current linear movement with other easing functions. Look at this website to imagine how each easing produces different animations. The code below uses the easeInQuad animation: const easeInQuad = function(t) { const loop = function(currentTime) { const percent = Math.min(time / duration, 1); window.scrollTo(0, startPos + diff * easeInQuad(percent)); In the following demo, try to move between sections by clicking an circle on the left. DemoScroll to an element smoothly See also |