Josh
Josh

Reputation: 2725

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

Upvotes: 3

Views: 4159

Answers (1)

John_C
John_C

Reputation: 1166

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();

Upvotes: 4

Related Questions