You cannot select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

44 lines
1.4 KiB
JavaScript

// Defines the framerate of autoscroller
setInterval(autoScroll, 50);
/**
* Auto scrolls all elements with class autoscroll
*/
function autoScroll () {
Array.from (document.getElementsByClassName("autoscroll")).forEach((objDiv) => {
// Skip non-scrollable
if (objDiv.scrollHeight - objDiv.clientHeight == 0) return;
// Wait timeout until scroll direction change
let scrollpause = parseInt(objDiv.dataset.scrollpause);
if (scrollpause != NaN && scrollpause > 0) {
scrollpause --;
objDiv.dataset.scrollpause = scrollpause;
return;
}
// Step either forward or backwards by one
let direction = 1;
if (objDiv.dataset.scrolldirection == "up") direction = -1;
objDiv.scrollTop += direction;
// If this is the first scroll step after, remove the scrollpause tag
if (scrollpause != NaN && scrollpause == 0) {
delete objDiv.dataset.scrollpause;
return;
}
// Reached the bottom, turn around
if (objDiv.scrollTop == objDiv.scrollHeight - objDiv.clientHeight) {
objDiv.dataset.scrolldirection = "up";
objDiv.dataset.scrollpause = 50;
}
// Reached the top, turn around
if (objDiv.scrollTop == 0) {
delete objDiv.dataset.scrolldirection;
objDiv.dataset.scrollpause = 50;
}
});
}