jQuery 溢出时自动滚动到底部自动
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/2530829/
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
auto scroll to bottom when overflow auto
提问by Phil Hymanson
does anyone know how to automatically jump to the bottom of a scrollable area by event in jquery ( or even javascript if no easy jquery solution)?
有谁知道如何通过 jquery 中的事件自动跳转到可滚动区域的底部(如果没有简单的 jquery 解决方案,甚至是 javascript)?
regards
问候
回答by PetersenDidIt
<div id="myDiv" style="height:300px;overflow:auto;">
<p>my content here</p>
</div>
var myDiv = $("#myDiv");
myDiv.animate({ scrollTop: myDiv.attr("scrollHeight") - myDiv.height() }, 3000);
Edit:
编辑:
jQuery 1.6 introduced .prop
and changed the meaning of .attr
thus $("#someDiv").attr("scrollHeight")
won't work anymore.
jQuery 1.6 引入.prop
并更改了 的含义,.attr
因此$("#someDiv").attr("scrollHeight")
将不再起作用。
Need to be changed to: $("#someDiv").prop("scrollHeight")
需要改为: $("#someDiv").prop("scrollHeight")
参考。
回答by eleotlecram
myDiv.attr("scrollHeight")
won't work in recent jQuery versions. You will need to resort to:
在最近的 jQuery 版本中不起作用。您将需要求助于:
myDiv[0].scrollHeight
回答by olikaf
mm.prop("scrollHeight")
does the trick (for jQuery 1.6 and forward).
mm.prop("scrollHeight")
可以解决问题(对于 jQuery 1.6 及更高版本)。