Search code examples
javascriptwordpressautoscroll

slowing down javascript autoscroll function


I found this javascript autoscroll function and got it to work by pasting it in the header file of a wordpress site. However, I'd like to slow down the scroll so that it does not immediately snap to the bottom of the page.

I am brand new to javascript and could not get any other autoscroll functions to work on my site so am hoping I can just adjust this function by adding a speed variable. Many thanks!

<script type="text/javascript"><!--

    function AutoScrollOnload() {

        var InFromLeft = 172;
        var DownFromTop = 964;

        window.scrollTo(InFromLeft,DownFromTop);
    }

    function AddOnloadEvent(f) {
        if (typeof window.onload != 'function') {
            window.onload = f;
        }
        else {
            var cache = window.onload;
            window.onload = function() {
                if (cache) { cache(); }
                f();
            };
        }
    }

    AddOnloadEvent(AutoScrollOnload);

//--></script>

Solution

  • Try this. Play with the values (10) to adjust to your liking.

    function slowScroll(inFromLeft, downFromTop) {
        var left = 0;
        var top = 0;
    
        function scroll() {
            left = left + 10;
            top = top + 10;
            window.scrollTo(left, top);
    
            if (left >= inFromLeft && top >= downFromTop)
              clearInterval(id)
          }
    
          var id = setInterval(scroll, 10) 
        }
    
    function AutoScrollOnload() {
        var InFromLeft = 172;
        var DownFromTop = 964;
    
        slowScroll(InFromLeft,DownFromTop);
    }