Last active
July 3, 2023 20:51
-
-
Save ActualKeith/46b764156b14e62e1caf5d5314906b80 to your computer and use it in GitHub Desktop.
Auto-scroller with ease and duration for screen recording and demo
This file contains hidden or 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
function scrollToBottom(duration) { | |
const currentPosition = window.pageYOffset; | |
const targetPosition = document.documentElement.scrollHeight - window.innerHeight; | |
const distance = targetPosition - currentPosition; | |
const startTime = performance.now(); | |
function scrollStep(timestamp) { | |
const elapsed = timestamp - startTime; | |
const progress = Math.min(elapsed / duration, 1); | |
const easing = quadraticEaseInOut(progress); | |
const newPosition = currentPosition + distance * easing; | |
window.scrollTo(0, newPosition); | |
if (elapsed < duration) { | |
window.requestAnimationFrame(scrollStep); | |
} | |
} | |
function quadraticEaseInOut(t) { | |
if (t < 0.5) { | |
return 2 * t * t; | |
} else { | |
return (-2 * t * t) + (4 * t) - 1; | |
} | |
} | |
window.requestAnimationFrame(scrollStep); | |
} | |
scrollToBottom(10000); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment