cho.sh Programmatically Scroll · cho.shProgrammatically Scroll
Warning
This post is more than a year old. Information may be outdated.
startDelay
=
5000
;
// Calculate the total distance to scroll
const totalScrollDistance =
document.documentElement.scrollHeight - window.innerHeight;
// Calculate the scroll duration based on the scroll speed and total distance
const scrollDuration = totalScrollDistance / scrollSpeed;
// Get the start time
let startTime = null;
// Function to handle the scrolling animation
function scrollAnimation(currentTime) {
if (startTime === null) {
startTime = currentTime;
}
const elapsedTime = currentTime - startTime;
const scrollPosition = (elapsedTime / scrollDuration) * totalScrollDistance;
window.scrollTo(0, scrollPosition);
if (elapsedTime < scrollDuration) {
window.requestAnimationFrame(scrollAnimation);
}
}
// Function to start the scrolling animation after the specified delay
function startScrolling() {
setTimeout(() => {
window.requestAnimationFrame(scrollAnimation);
}, startDelay);
}
// Call the startScrolling function to begin the scrolling animation after the delay
startScrolling();
No comments yet. Be the first to share your thoughts.