auto scroll to bottom when overflow auto

2019-01-17 15:15发布

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)?

regards

标签: jquery scroll
3条回答
家丑人穷心不美
2楼-- · 2019-01-17 16:01
<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.

Need to be changed to: $("#someDiv").prop("scrollHeight")

Reference.

查看更多
干净又极端
3楼-- · 2019-01-17 16:08
myDiv.attr("scrollHeight")

won't work in recent jQuery versions. You will need to resort to:

myDiv[0].scrollHeight
查看更多
三岁会撩人
4楼-- · 2019-01-17 16:09

mm.prop("scrollHeight") does the trick (for jQuery 1.6 and forward).

查看更多
登录 后发表回答