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 p开发者_运维百科age...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;
}
精彩评论