- 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>
<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>