Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Continuous scrolling page using javascript

I am trying to repeat an animation where a page automatically scrolls to the bottom. When it reaches the bottom I want it to then scroll to the top. Then, repeat forever. However, I can't get it to even perform the first callback. Any help would be greatly appreciated.

Code:

pageScroll(pageScrollUp);





function pageScroll(callback) {
    window.scrollBy(0,1); // horizontal and vertical scroll increments
    scrolldelay = setTimeout('pageScroll()',50); // scrolls every 100 milliseconds

    callback(pageScroll);

}


function pageScrollUp(callback) {

    window.scrollBy(0,-1); // horizontal and vertical scroll increments
    scrolldelay = setTimeout('pageScroll()',50); // scrolls every 100 milliseconds

    callback(pageScrollUp);

}

Thanks Josh

like image 745
Josh Avatar asked Jan 29 '26 18:01

Josh


1 Answers

This should do it: http://jsfiddle.net/John_C/8ZfKr/

var scrollDirection = 1;
function pageScroll() {
    window.scrollBy(0,scrollDirection); // horizontal and vertical scroll increments
    scrolldelay = setTimeout('pageScroll()',50); // scrolls every 50 milliseconds
    if ( (window.scrollY === 0) || (window.innerHeight + window.scrollY) >= document.body.offsetHeight) {
        scrollDirection = -1*scrollDirection;
    }
}
pageScroll();
like image 156
John_C Avatar answered Jan 31 '26 06:01

John_C