Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- /**
- * Smooth scroll to ...
- *
- * @param {integer} offset Vertical scroll offset (px)
- * @param {integer} speed Animation speed (ms)
- */
- function smoothScrollTo(offset = 0, speed = 500) {
- // Fallback for old browsers
- if (!performance || !performance.now || !requestAnimationFrame) {
- window.scrollTo(0, 0)
- return
- }
- const cosParameter = window.scrollY / 2
- let scrollCount = 0
- let oldTimestamp = performance.now()
- function step (newTimestamp) {
- scrollCount += Math.PI / (speed = 500 / (newTimestamp - oldTimestamp))
- if (scrollCount >= Math.PI) {
- window.scrollTo(0, 0)
- }
- if (window.scrollY <= offset) {
- return
- }
- window.scrollTo(0, Math.round(cosParameter + cosParameter * Math.cos(scrollCount)))
- oldTimestamp = newTimestamp
- requestAnimationFrame(step)
- }
- requestAnimationFrame(step)
- }
- export {smoothScrollTo as default}
Add Comment
Please, Sign In to add comment