Adi
Adi

Reputation: 4022

javascript or mootools way to animate scrollTop like jQuery

What is the equivalent of this:

$('html, body').animate({
    scrollTop: 200
}, 400); 

In MooTools or agnostic javascript?

Im going round in circles....

Cheers

Upvotes: 1

Views: 1988

Answers (2)

Marc J. Schmidt
Marc J. Schmidt

Reputation: 8459

Because you asked for a version in mootools, it's just one line:

new Fx.Scroll(window).start(0, 200);​

http://jsfiddle.net/ye3vX/

Note: It needs mootools more's Fx.Scroll.

Upvotes: 2

Stano
Stano

Reputation: 8939

This loop can animate the scrollTop property of the window in basic javascript:

window.onload = function() {
    var offset = 0; //vertical offset
    var interval = setInterval(function() {
        window.scrollTo(0, offset);
        offset += 4; // plus 4 pixels
        if (offset >= 200) {
            clearInterval(interval);
        }
    }, 8); //  200px/4px==50 cycles ; 400ms/50cycles == 8ms per cycle
};

fiddle

Upvotes: 2

Related Questions