Simple Page Scroller

Joined
Jul 10, 2025
Messages
1
Reaction score
0
I have a javascript that is supposed to scroll a page line by line. When it reaches the bottom of the page, it is supposed to return to the top and start again. Instead, it goes to the bottom and just stops. This is what I have.

<script>
// Get the body element for scrolling
const body = document.body;

// Scrolling speed (adjust as needed)
const scrollSpeed = 1; // pixels per interval

// Interval duration (adjust for smoother/faster scrolling)
const intervalDuration = 40; // milliseconds

let scrollInterval;

function startScrolling() {
scrollInterval = setInterval(() => {
// Scroll down by the specified speed
window.scrollBy(0, scrollSpeed);

// Check if we've reached the bottom
if (window.innerHeight + window.scrollY >= body.scrollHeight) {
// If at the bottom, reset to the top
window.scrollTo(0, 0);
}
}, intervalDuration);
}

function stopScrolling() {
clearInterval(scrollInterval);
}

// Start scrolling when the page loads
window.onload = startScrolling;

// Optional: Stop scrolling on a key press (e.g., Spacebar)
document.addEventListener('keydown', (event) => {
if (event.code === 'Space') {
if (scrollInterval) {
stopScrolling();
scrollInterval = null;
} else {
startScrolling();
}
}
});
</script>
 
Joined
Jun 12, 2020
Messages
36
Reaction score
2
Hi, to ensure that the scrolling continues smoothly after reaching the bottom, you can modify the code to reset the scroll position and immediately continue scrolling. Here's an updated version of your script:
JavaScript:
<script>
// Get the body element for scrolling
const body = document.body;

// Scrolling speed (adjust as needed)
const scrollSpeed = 1; // pixels per interval

// Interval duration (adjust for smoother/faster scrolling)
const intervalDuration = 40; // milliseconds

let scrollInterval;

function startScrolling() {
    scrollInterval = setInterval(() => {
        // Scroll down by the specified speed
        window.scrollBy(0, scrollSpeed);

        // Check if we've reached the bottom
        if (window.innerHeight + window.scrollY >= body.scrollHeight) {
            // If at the bottom, reset to the top
            window.scrollTo(0, 0);
        }
    }, intervalDuration);
}

function stopScrolling() {
    clearInterval(scrollInterval);
}

// Start scrolling when the page loads
window.onload = startScrolling;

// Optional: Stop scrolling on a key press (e.g., Spacebar)
document.addEventListener('keydown', (event) => {
    if (event.code === 'Space') {
        if (scrollInterval) {
            stopScrolling();
            scrollInterval = null;
        } else {
            startScrolling();
        }
    }
});
</script>
 
Joined
Jul 4, 2023
Messages
607
Reaction score
81
JavaScript:
document.addEventListener('keydown', (event) => {
  if (event.code === 'Space') {
    if (scrollInterval) {
      stopScrolling();
      scrollInterval = null;
    } else {
      startScrolling();
    }
  }
})
Without preventDefault(), pressing the Spacebar while scrolling is paused causes the page to "jump" down as if the user manually pressed Spacebar to scroll. This default browser behavior can be prevented by calling event.preventDefault(), ensuring a smooth toggle between scrolling and pausing without any visual jump.
JavaScript:
document.addEventListener('keydown', (event) => {
  if (event.code !== 'Space') return;
  event.preventDefault(); // prevent page from jumping
  scrollInterval ? stopScrolling() : startScrolling();
})

Here's a slightly improved version of your code shown below.
JavaScript:
// Scrolling configuration
const scrollSpeed = 1;       // pixels per interval
const intervalDuration = 40; // milliseconds

let scrollInterval = null;

function startScrolling() {
  if (scrollInterval) return; // avoid multiple intervals

  scrollInterval = setInterval(() => {
    window.scrollBy(0, scrollSpeed);
    const atBottom =
          Math.ceil(window.innerHeight + window.scrollY) >= document.documentElement.scrollHeight;

    if (atBottom) window.scrollTo(0, 0);
  }, intervalDuration);
}

function stopScrolling() {
  clearInterval(scrollInterval);
  scrollInterval = null;
}

// Start scrolling when the page loads
window.addEventListener('load', startScrolling);

// Toggle scrolling with spacebar
document.addEventListener('keydown', (event) => {
  if (event.code !== 'Space') return;
  event.preventDefault(); // prevent page from jumping
  scrollInterval ? stopScrolling() : startScrolling();
});

To add a 2-second pause before scrolling returns to position (0, 0), you need to delay the window.scrollTo(0, 0) call using setTimeout. Here's the updated code with that effect [demo]:
JavaScript:
// Scrolling configuration
const scrollSpeed = 1;      // pixels per interval
const intervalDuration = 40; // milliseconds
const pauseBeforeReset = 2; // pause duration 2 seconds

let scrollInterval = null;

function startScrolling() {
  if (scrollInterval) return; // avoid multiple intervals

  scrollInterval = setInterval(() => {
    window.scrollBy(0, scrollSpeed);
    const atBottom =
          Math.ceil(window.innerHeight + window.scrollY) >= document.documentElement.scrollHeight;

    if (atBottom) {
      stopScrolling(); // stop scrolling during the pause
      setTimeout(() => {
        window.scrollTo(0, 0); // go back to top
        startScrolling();      // resume scrolling
      }, (pauseBeforeReset * 1000));
    }
  }, intervalDuration);
}

function stopScrolling() {
  clearInterval(scrollInterval);
  scrollInterval = null;
}

// Start scrolling when the page loads
window.addEventListener('load', startScrolling);

// Toggle scrolling with spacebar
document.addEventListener('keydown', (event) => {
  if (event.code !== 'Space') return;
  event.preventDefault(); // prevent page from jumping
  scrollInterval ? stopScrolling() : startScrolling();
});
 
Last edited:
Joined
Jul 7, 2024
Messages
4
Reaction score
0
That script is close, but the issue is you’re checking
Code:
body.scrollHeight
On some browsers the full page height isn’t on body, it’s on
Code:
document.documentElement
So your bottom check fails and it just hangs there.
Try this instead:


JavaScript:
const scrollSpeed = 1;      // pixels per interval
const intervalDuration = 40; // ms
let scrollInterval;


function startScrolling() {
  scrollInterval = setInterval(() => {
    window.scrollBy(0, scrollSpeed);


    // use documentElement for cross-browser page height
    const doc = document.documentElement;
    if (window.innerHeight + window.scrollY >= doc.scrollHeight) {
      window.scrollTo(0, 0); // reset to top
    }
  }, intervalDuration);
}


function stopScrolling() {
  clearInterval(scrollInterval);
}


// start on load
window.onload = startScrolling;


// toggle with Space
document.addEventListener('keydown', e => {
  if (e.code === 'Space') {
    if (scrollInterval) {
      stopScrolling();
      scrollInterval = null;
    } else {
      startScrolling();
    }
  }
});
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Staff online

Members online

Forum statistics

Threads
474,342
Messages
2,571,405
Members
48,796
Latest member
katerack

Latest Threads

Top