How can a vertical scroll bar stay at the bottom when text is added continuously?

孤街浪徒 提交于 2019-12-12 02:47:41

问题


I have a div and it has a scroll bar here is the code I've used:

<div style="width: 200px; height: 250px; overflow: scroll; overflow-x: hidden; -ms-overflow-x: hidden;" id="container"></div>

There will be more text added to the div but when the div does expand how can I have the vertical scroll bar remain at the bottom of the bar so the user can see the appended text?


回答1:


You should set the scrollTop to scrollHeight, e.g.: http://jsfiddle.net/fAK2L/1/.

var div = $('div');
div.scrollTop( div.get(0).scrollHeight );



回答2:


Use element.scrollIntoView for the last added element



来源:https://stackoverflow.com/questions/7786014/how-can-a-vertical-scroll-bar-stay-at-the-bottom-when-text-is-added-continuously

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!