Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
421 views
in Technique[技术] by (71.8m points)

javascript - 滚动到div的底部?(Scroll to bottom of div?)

I am creating a chat using ajax requests in rails and I'm trying to get a div to scroll to the bottom without much luck.

(我正在使用Rails中的Ajax请求创建聊天,并且试图使div滚动到底部没有太多运气。)

I am wrapping everything in this div:

(我将所有内容包装在这个div中:)

#scroll {
    height:400px;
    overflow:scroll;
}

Is there a way to keep it scrolled to the bottom by default using JS?

(有没有一种方法可以使用JS将其默认滚动到底部?)

Is there a way to keep it scrolled to the bottom after an ajax request?

(是否有办法在ajax请求后将其滚动到底部?)

  ask by dMix translate from so

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

Here's what I use on my site:

(这是我在网站上使用的内容:)

var objDiv = document.getElementById("your_div");
objDiv.scrollTop = objDiv.scrollHeight;

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...