Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Animating window scrolling

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

window.scrollBy(0, delta)

The current implementation of scrollBy just jumps the document to the new position. Is there a way of animating this? I am using webkit specifically, and jQuery (or any other javascript framework) is not an option.

Thanks for any help in advance.

like image 587
D.C. Avatar asked Dec 12 '22 16:12

D.C.


1 Answers

You can just animate it by invoking an interval:

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

This of course would do it over and over, so you need to put in a conditional check, when to cancel the interval. Could look like:

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

    if( window.pageYOffset >= 500 )
        clearInterval(timerID);
}, 13);
like image 159
jAndy Avatar answered Dec 21 '22 01:12

jAndy