Animated Window Scrolling

Hi I am using the following method to programmatically scroll a web document:

window.scrollBy(0, delta)

The current implementation of scrollBy simply moves the document to a new position. Is there any way to revive this? I use webkit on purpose, and jQuery (or any other javascript framework) is not an option.

Thanks for any help in advance.

+3
source share
2 answers

You can simply animate it by calling the interval:

setInterval(function() {
    window.scrollBy(0, 5);
}, 13);

This, of course, will do it again and again, so you need to put a conditional check when you need to cancel the interval. It might look like this:

var timerID = setInterval(function() {
    window.scrollBy(0, 5);

    if( window.pageYOffset >= 500 )
        clearInterval(timerID);
}, 13);
+12
source

, , window.scrollBy(0,5), , YOffset. , pageYOffset 300px, 305px.

if else
:

var timerID = setInterval(function() {
    if( window.pageYOffset <= 500 )
        window.scrollBy(0, 5);

    else
        clearInterval(timerID);
}, 1);
0

Source: https://habr.com/ru/post/1791041/


All Articles