javascript - 向下滚动到文档高度但 + 100
问题描述
我得到了这个脚本
$("body, html").animate({scrollTop: $(document).height()}, 1000)
如上所示,它将使页面向下滚动到文档高度:也就是底部。但是在我的情况下它太远了,我想向下滚动到bottom:100px;
或者bottom:10%;
这可能吗?我找到了例子,但它们对我不起作用。我缺少什么使它起作用?:)
解决方案
当然,您可以将 scrollTop 的值设置为您可以数学计算的任何值:
// Top is 100 px short of bottom
$("body, html").animate({scrollTop: $(document).height() - 100}, 1000)
// 90% scroll down
$("body, html").animate({scrollTop: $(document).height() * 0.9}, 1000)
请注意,对于所有这些,100px非常小。您的浏览器窗口的高度必须小于 100 像素,才不会一直滚动到底部。
您也可以在方程式中使用浏览器窗口的可见高度:
$(window).height()