// 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; } }); }