最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - Cross-Browser method for setting ScrollTop of an element? - Stack Overflow

programmeradmin1浏览0评论

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

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

Share Improve this question edited Jun 13, 2011 at 16:09 Marcel Korpel 21.8k6 gold badges62 silver badges80 bronze badges asked Jun 13, 2011 at 16:05 nvcnvnnvcnvn 5,1758 gold badges52 silver badges78 bronze badges
Add a comment  | 

3 Answers 3

Reset to default 16

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.

Make sure that overflow property is set:

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

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

document.getElementById("divscroll").scrollTop = document.getElementById("divscroll").scrollHeight; 
发布评论

评论列表(0)

  1. 暂无评论