Skip to content

Instantly share code, notes, and snippets.

@ActualKeith
Last active July 3, 2023 20:51
Show Gist options
  • Save ActualKeith/46b764156b14e62e1caf5d5314906b80 to your computer and use it in GitHub Desktop.
Save ActualKeith/46b764156b14e62e1caf5d5314906b80 to your computer and use it in GitHub Desktop.
Auto-scroller with ease and duration for screen recording and demo
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