Javascript 用于设置元素的 ScrollTop 的跨浏览器方法?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/6333028/
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
Cross-Browser method for setting ScrollTop of an element?
提问by nvcnvn
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:
例如,我有一个 div 标签,在 div 标签的右侧有滚动条。
我想用最后一行显示 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!
Is there any simple Cross-borwser script (not JQuery or any big framework!)
我可以让它在 Firefox 中滚动到最后,但永远不会成功使用更大数字的 IE 事件!
是否有任何简单的跨浏览器脚本(不是 JQuery 或任何大框架!)
采纳答案by Matt Ball
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
mostlyworks.
回答by Scherbius.com
Make sure that overflowproperty is set:
确保设置了溢出属性:
<div id="divscroll" style="height: 100px; width: 100px; overflow: scroll;">
//// something something
</div>
回答by Chris Baker
Try setting the scroll position to a real figure, instead of just an arbitrary big number:
尝试将滚动位置设置为一个真实的数字,而不仅仅是一个任意的大数字:
document.getElementById("divscroll").scrollTop = document.getElementById("divscroll").scrollHeight;