如何设置滚动条的默认位置
在我的网页中有一个聊天窗口。填写聊天日志后,用户使用滚动条在消息之间导航。我希望我的聊天框架始终显示位于底部的最后一条消息。我如何使用 CSS、JavaScript 来做到这一点?
谢谢, 托梅尔
in my web page there is a chat window. When the chat's log is filled out, the user uses scrollbar in order to navigate between messages. I want my chat frame to present always the last message that is located at the bottom. How can I do this using CSS, JavaScript?
Thanks,
Tomer
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
即使你不是你的问题的标签,这里也是我用于聊天网络应用程序的 jQuery 解决方案:
Even thou is not a tag on your question here is a jQuery solution i use for my chat web app:
DOM 元素(无论如何,好的元素)支持一种名为
scrollIntoView()
的方法。如果您有对与最后一个聊天条目对应的 DOM 元素的引用(或其他),您可以使用
scrollIntoView()
告诉浏览器它是应滚动周围的内容以使可见。
现在,对于一些复杂的(或者可能不是那么复杂的;也许只是不幸的)页面布局,我不得不与 Internet Explorer 进行斗争,因为 Internet Explorer 想要滚动错误的内容,或者只是对页面做一些奇怪的事情。像
scrollIntoView()
这样的东西的本质是让浏览器决定它想要如何进行滚动。一般来说,在一个简单的滚动容器中(一个带有“overflow: auto”和固定高度的容器)中相当简单的内容,它确实可以工作。DOM elements (the good ones, anyway) support a method called
scrollIntoView()
. If you have a reference to the DOM element corresponding to the last chat entry (a<div>
or whatever) you can usescrollIntoView()
to tell the browser that its surrounding content should be scrolled in order to make the<div>
visible.Now, with some complicated (or not-so-complicated, maybe; perhaps just unlucky) page layouts, I've had to contend with Internet Explorer wanting to scroll the wrong thing, or just do something weird to the page. The nature of a thing like
scrollIntoView()
is such that you're letting the browser decide exactly how it wants to do that scrolling. Generally, with fairly simple content in a simple scrolling container (one with "overflow: auto" and a fixed height, basically), it does work however.http://radio.javaranch.com/pascarello/2005/12/14 /1134573598403.html
Google 的第三个结果 - 'div 滚动条底部'
http://radio.javaranch.com/pascarello/2005/12/14/1134573598403.html
3rd result from Google - 'div scrollbar bottom'