简体   繁体   English

用于设置元素的ScrollTop的跨浏览器方法?

[英]Cross-Browser method for setting ScrollTop of an element?

For example I have I a div tag with the scroll bar on the right of the div tag. 例如,我有一个带有div标签右侧滚动条的div标签。
I want to show the div with the last line, so I have this: 我想用最后一行显示div,所以我有这个:

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! 我可以让它滚动到Firefox的最后但是从未成功使用更大数量的IE事件!
Is there any simple Cross-borwser script (not JQuery or any big framework!) 有没有简单的Cross-borwser脚本(不是JQuery或任何大框架!)

scrollTop works in all major browsers. scrollTop适用于所有主流浏览器。

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 . clientHeight也适用于各种浏览器 ,而scrollHeight 主要适用

Make sure that overflow property is set: 确保设置了overflow属性:

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

尝试将滚动位置设置为实际数字,而不仅仅是任意大数字:

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM