Javascript 如何使用 Jquery 向上滚动?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/14536800/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How to scroll up with Jquery?
提问by LukyVj
I tried everything, and i can't write the good Jquery code to make my body scrolls up. This is my JS :
我尝试了所有方法,但我无法编写好的 Jquery 代码来使我的身体向上滚动。这是我的JS:
$("body").animate({scrollTop:$(document).height()}, 9000);
$(this).fadeOut(fast);
$("body").animate({scrollTop:$(document).height()}, 9000);
$(this).fadeOut(fast);
And it makes the body scroll down when user click on a specific div, then i want to reverse this function to make the body scrolls up.. any idea ?
当用户单击特定的 div 时,它会使主体向下滚动,然后我想反转此功能以使主体向上滚动.. 知道吗?
回答by simbabque
You need to supply a 0as the argument to scrollTopif you want it to scroll upwards.
如果您希望它向上滚动0,scrollTop则需要提供 a作为参数。
$(document).ready(function() {
// scroll down
$("html, body").animate({
scrollTop: $(document).height()
}, 9000);
/scroll back up
$("html, body").animate({
scrollTop: 0
}, 9000);
});
As you can see in the documentation, you need to provide a CSS property and a value as the first param to animate(). Since scrollTop(doc of scrollTop(), which returns the current value) is the vertical scroll bar and you want to scroll to the top, you need 0there to go upwards.
正如您在文档中看到的,您需要提供一个 CSS 属性和一个值作为 to 的第一个参数animate()。由于scrollTop( doc ofscrollTop(),它返回当前值) 是垂直滚动条并且您想要滚动到顶部,因此您需要0向上滚动。
Check this jfiddlefor a demonstration.
检查这个jfiddle进行演示。
回答by Praveen Kumar Purushothaman
Try this:
尝试这个:
$("html, body").animate({
scrollTop: $(document).height()
}, 9000);

![Javascript 为什么使用 jQuery(selector).get(0) 而不是 jQuery(selector)[0] 来获取 DOM 元素?](/res/img/loading.gif)