Cross-Browser method for setting ScrollTop of an element?

感情迁移 提交于 2019-12-12 07:13:10

问题


For example I have I a div tag with the scroll bar on the right of the div tag.
I want to show the div with the last line, so I have this:

document.getElementById("divscroll").scrollTop = 250000;

I can make it scroll to the end in Firefox but never succcess with IE event with the bigger number!
Is there any simple Cross-borwser script (not JQuery or any big framework!)


回答1:


scrollTop works in all major browsers.

To scroll to the bottom of the element:

var div = document.getElementById('divscroll');
div.scrollTop = div.scrollHeight - div.clientHeight;

clientHeight also works across browsers, and scrollHeight mostly works.




回答2:


Make sure that overflow property is set:

<div id="divscroll" style="height: 100px; width: 100px; overflow: scroll;">
 //// something something 
</div>



回答3:


Try setting the scroll position to a real figure, instead of just an arbitrary big number:

document.getElementById("divscroll").scrollTop = document.getElementById("divscroll").scrollHeight; 


来源:https://stackoverflow.com/questions/6333028/cross-browser-method-for-setting-scrolltop-of-an-element

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