简体   繁体   中英

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

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

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM