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

javascript - How can I auto-scroll as content is added to a div? - Stack Overflow

programmeradmin1浏览0评论

I have a div of fixed dimensions into which some JavaScript functions will be placing text over time. When the amount of text exceeds the height of the box, a scrollbar appears thanks to overflow:scroll.

The new problem is that the view into the div stays at the same place as more content appears. What I mean to say is that it stays scrolled wherever it is as more content appears beneath, hidden unless you manually scroll down. I want to make it automatically scroll to the bottom as new content appears so that the user naturally sees what appeared most recently instead of what's oldest.

Ideas?

I have a div of fixed dimensions into which some JavaScript functions will be placing text over time. When the amount of text exceeds the height of the box, a scrollbar appears thanks to overflow:scroll.

The new problem is that the view into the div stays at the same place as more content appears. What I mean to say is that it stays scrolled wherever it is as more content appears beneath, hidden unless you manually scroll down. I want to make it automatically scroll to the bottom as new content appears so that the user naturally sees what appeared most recently instead of what's oldest.

Ideas?

Share Improve this question asked May 14, 2012 at 19:30 temporary_user_nametemporary_user_name 37.2k48 gold badges160 silver badges243 bronze badges
Add a ment  | 

2 Answers 2

Reset to default 7

You can use scrollTop method after each text addition:

$("div").scrollTop($("div").children().height());

Use inner block to get the true height.

DEMO: http://jsfiddle/eyY5k/1/

I found this approach to work for my needs:

var realHeight = $("#history")[0].scrollHeight;
$("#history").scrollTop(realHeight);

Do note this uses jquery.

发布评论

评论列表(0)

  1. 暂无评论