Skip to content

Instantly share code, notes, and snippets.

@CodeNinja96x
Forked from pmulik34/Scrolling webpage
Last active July 24, 2021 11:18
Show Gist options
  • Save CodeNinja96x/de5c1b6a43a2d5a4a3c29bb5e2a1c8e1 to your computer and use it in GitHub Desktop.
Save CodeNinja96x/de5c1b6a43a2d5a4a3c29bb5e2a1c8e1 to your computer and use it in GitHub Desktop.
let scrollerID;
let paused = true;
let speed = 3; // 1 - Fast | 2 - Medium | 3 - Slow
let interval = speed * 5;
function startScroll(){
let id = setInterval(function() {
window.scrollBy(0, 2);
if ((window.innerHeight + window.scrollY) >= document.body.offsetHeight) {
// Reached end of page
stopScroll();
}
}, interval);
return id;
}
function stopScroll() {
clearInterval(scrollerID);
}
function checkScroll(event) {
// commenting out the below code as we need the scroll to work on every key press
//if (event.which == 13 || event.keyCode == 13) {
console.log(event.type);
console.log(paused);
if(paused == true) {
scrollerID = startScroll();
paused = false;
console.log(scrollerID);
}
else {
stopScroll();
paused = true;
}
// }
}
document.body.addEventListener('keypress',checkScroll , true);
document.body.addEventListener('click',checkScroll , true);
window.onload = checkScroll;
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment