当前位置: 动力学知识库 > 问答 > 编程问答 >

How do I create a function that scrolls up the page bit-by-bit, using JQuery?

问题描述:

How do I create a function like this that scrolls up the page bit-by-bit?

$('#down').click(function(){

$('html, body').animate({

scrollTop: $(this).offset().top += 20

});

return false;

});

This function works for scrolling down the page...I've a play trying to get it to scroll up the page but no success.

Any help is much appreciated, Thanks

网友答案:

window.pageYOffset will get you what you desire!

$('#up').click(function(){
    $('html, body').animate({
        scrollTop: window.pageYOffset -= 20
    });
    return false;
});

Have a fiddle: http://jsfiddle.net/mGwJs/2/ Scroll down to the "up" link then keep clicking it.

Did some cross-browser testing on my code, found that it doesn't work for IE7, 8. See this link here for making it jive on the old browsers: http://www.howtocreate.co.uk/tutorials/javascript/browserwindow

网友答案:

from here

$('#down').click(function(){
    $('html,body').animate({
       scrollTop: $("#scrollToHere").offset().top
    }, 2000);
    return false;
}

2nd:

$('#up').click(function(){
    $('html,body').animate({
       scrollTop: 0
    }, 2000);
    return false;
}
分享给朋友:
您可能感兴趣的文章:
随机阅读: