如何设置滚动条的默认位置

发布于 2024-10-10 05:14:00 字数 113 浏览 0 评论 0原文

在我的网页中有一个聊天窗口。填写聊天日志后,用户使用滚动条在消息之间导航。我希望我的聊天框架始终显示位于底部的最后一条消息。我如何使用 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

御弟哥哥 2024-10-17 05:14:00

即使你不是你的问题的标签,这里也是我用于聊天网络应用程序的 jQuery 解决方案:

//Where chat_message_tray is the scrollable div in your chat
function chat_scroll(chat_messages_tray){
   var scroll_amount = $(chat_messages_tray).attr("scrollHeight") -  $(chat_messages_tray).height();

   $(chat_messages_tray).animate({ scrollTop: scroll_amount}, 500);
}

Even thou is not a tag on your question here is a jQuery solution i use for my chat web app:

//Where chat_message_tray is the scrollable div in your chat
function chat_scroll(chat_messages_tray){
   var scroll_amount = $(chat_messages_tray).attr("scrollHeight") -  $(chat_messages_tray).height();

   $(chat_messages_tray).animate({ scrollTop: scroll_amount}, 500);
}
自控 2024-10-17 05:14:00

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 use scrollIntoView() 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.

偏爱自由 2024-10-17 05:14:00

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'

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文