Skip to content
Advertisement

JavaScript infinite up and down element scrolling?

I’m wondering if there is a simple way to make use of JavaScript (probably jQuery too?) in order to make the contents of a fixed-height div element scroll infinitely up and down (top, bottom, top, bottom, etc) when the page loads and without any user input or manipulation?

Thanks ahead of time, any input is greatly appreciated as I am hardly mediocre with JavaScript.

Advertisement

Answer

With pure js you can do something like this:

var scroller = document.getElementById('scroller');
var delta = 15;
var lastSc;

    //console.log(scroller.scrollTop, scrollHeight);
setInterval(function(){
    var sc = scroller.scrollTop + delta;
    scroller.scrollTop = sc;
    if (scroller.scrollTop === lastSc){
        delta = delta*(-1);
    }
    lastSc = scroller.scrollTop;
}, 10);

Here is demo

Edit: updated demo

User contributions licensed under: CC BY-SA
7 People found this is helpful
Advertisement