Search code examples
javascriptvertical-scrollingcustom-scrolling

Making a custom scrolling in Javascript


I would like to force a scroll in javascript so i wrote that script but it doesn't work. The script makes the user scroll and after his execution, it goes back to the top of the page.

function myscroll(i)
{
    if (document.body.scrollTop < i - 100)
    {
        document.body.scrollTop += 10;
        setTimeout(myscroll(i), 10);
    }
}

How can i fix that ?

Thanks for reading.


Solution

  • You need to use an anonymous function for setTimeout to work as you'd expect:

    setTimeout(function() {
        myscroll(i)
    }, 10);