Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Select an option

  • Save tannerhallman/55bdf17a7823acd084e3efaa133f9459 to your computer and use it in GitHub Desktop.

Select an option

Save tannerhallman/55bdf17a7823acd084e3efaa133f9459 to your computer and use it in GitHub Desktop.
Autoscroll on Page Load for Colin
document.addEventListener('DOMContentLoaded', function () {
if (window.__autoScroll) {
cancelAnimationFrame(window.__autoScroll.raf);
window.__autoScroll.cleanup();
}
const pxPerSecond = 40; // ← adjust speed here
let last = null;
const scrollKeys = new Set([
'ArrowUp', 'ArrowDown', 'PageUp', 'PageDown', 'Home', 'End', ' '
]);
function stop(reason) {
if (!window.__autoScroll) return;
cancelAnimationFrame(window.__autoScroll.raf);
window.__autoScroll.cleanup();
window.__autoScroll = null;
console.log(`Auto-scroll stopped (${reason}).`);
}
function onUserInput(e) {
if (e.type === 'keydown' && !scrollKeys.has(e.key)) return;
stop('user input detected');
}
function step(now) {
if (last === null) last = now;
const dt = (now - last) / 1000;
last = now;
window.scrollBy(0, pxPerSecond * dt);
const atBottom =
window.innerHeight + window.scrollY >=
document.documentElement.scrollHeight - 1;
if (!atBottom) {
window.__autoScroll.raf = requestAnimationFrame(step);
} else {
stop('reached bottom');
}
}
window.addEventListener('wheel', onUserInput, { passive: true });
window.addEventListener('touchstart', onUserInput, { passive: true });
window.addEventListener('keydown', onUserInput);
window.__autoScroll = {
raf: requestAnimationFrame(step),
cleanup() {
window.removeEventListener('wheel', onUserInput);
window.removeEventListener('touchstart', onUserInput);
window.removeEventListener('keydown', onUserInput);
}
};
window.__stopScroll = () => stop('manual stop');
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment