在聊天过程中将文本与底部对齐?

发布于 2024-09-04 12:19:27 字数 957 浏览 0 评论 0原文

我正在构建一个自定义网络聊天应用程序,虽然我已经解决了基础知识,但我一直想知道这是否可能......现在,聊天出现在 div 的顶部,当它到达底部,div 开始滚动。这有效。这太棒了。但我想知道是否可以将其创建得更像 IRC 客户端,其中聊天最初出现在 div 的底部,然后每个新行出现在旧行的下方,等等,再次,当 div 已满时,它开始滚动。

我已经成功地完成了这项工作的一部分:我可以让它以这种方式显示。但我找不到滚动它的方法;要么滚动不出现(当内部文本 div 上没有溢出时,尽管外部容器 div 上有溢出),或者它仅限于文本的宽度而不是容器 div 的宽度。

我尝试过的一些选项:

<div id="chatbox" style="overflow: auto; position: relative; width: 100%; height: 400px;">
<div id="chatmessages" style="overflow: auto; position: absolute; bottom: 0;"></div></div>

这使文本正确显示在底部,但不会出现滚动条。

<div id="chatbox" style="overflow: auto; position: relative; width: 100%; height: 400px;">
<div id="chatmessages" style="overflow: scroll; position: absolute; bottom: 0;"></div></div>

这使得文本正确显示在底部,并且出现滚动条,但它的宽度仅与文本一样宽,即使宽度= 100%...并且当文本到达顶部时,滚动条保持灰色。

基本上,我是否想要内部或容器 div 上的滚动条,这是否可能,我如何强制它工作,我的做法是否完全错误?

I'm building a custom web chat application, and while I have the basics worked out, I've been wondering if this was possible... right now, the chat comes in at the top of the div, and when it reaches the bottom, the div starts scrolling. This works. This is great. But I was wondering if it was possible to create it more like an IRC client, where the chat comes in initially at the bottom of the div, then each new line comes in below the old ones, etc, and again, when the div is full, it starts scrolling.

I've managed to get part of this working: I can get it displaying this way. But I can't find a way to scroll it; either the scroll doesn't appear (when there's no overflow on the inner, text div, despite there being an overflow on the outer, container div), or it's confined to the width of the text rather than the width of the container div.

Some options I've tried:

<div id="chatbox" style="overflow: auto; position: relative; width: 100%; height: 400px;">
<div id="chatmessages" style="overflow: auto; position: absolute; bottom: 0;"></div></div>

This has the text appear properly at the bottom, but no scrollbar ever appears.

<div id="chatbox" style="overflow: auto; position: relative; width: 100%; height: 400px;">
<div id="chatmessages" style="overflow: scroll; position: absolute; bottom: 0;"></div></div>

This has the text appear properly at the bottom, and a scrollbar appears, but it's only ever as wide as the text, even if width=100%... and when the text reaches the top, the scrollbar remains gray.

Basically, do I want the scrollbar on the inner or the container div, is this even possible, how do I force it to work, and am I going about this entirely wrong?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

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

发布评论

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

评论(3

很快妥协 2024-09-11 12:19:27

滚动条不会启动,因为 chatmessages 只是不断变高。

使用这些样式:

    #chatbox
    {
        overflow:   none;
        position:   relative;
        width:      100%;
        height:     400px;
    }
    #chatmessages
    {
        overflow:   auto;
        position:   absolute;
        bottom:     0;
        max-height: 400px;
    }

The scroll-bars don't kick in because chatmessages just keeps getting taller.

Use these styles:

    #chatbox
    {
        overflow:   none;
        position:   relative;
        width:      100%;
        height:     400px;
    }
    #chatmessages
    {
        overflow:   auto;
        position:   absolute;
        bottom:     0;
        max-height: 400px;
    }
太阳哥哥 2024-09-11 12:19:27

使用此 jquery 滚动到底部,因此它将始终显示最新消息:

$('#chatmessages').scrollTop($('#chatmessages')[0].scrollHeight);

对于 #chatmessages 使用宽度:100% 和最大高度与 #chatbox 的高度相同

#chatbox {
    overflow:   none;
    position:   relative;
    width:      100%;
    height:     200px;
    border:     1px solid #ccc;
}
#chatmessages
{
    overflow:   auto;
    position:   absolute;
    bottom:     0;
    width:      100%;
    max-height: 200px;
}

我用示例文本消息创建了这些小提琴:

jsfiddle 带滚动

jsfiddle 不带滚动

Use this jquery for scrolling to bottom, so it will always display the latest message:

$('#chatmessages').scrollTop($('#chatmessages')[0].scrollHeight);

For #chatmessages use width: 100% and max-height same as height of #chatbox

#chatbox {
    overflow:   none;
    position:   relative;
    width:      100%;
    height:     200px;
    border:     1px solid #ccc;
}
#chatmessages
{
    overflow:   auto;
    position:   absolute;
    bottom:     0;
    width:      100%;
    max-height: 200px;
}

I created these fiddles with sample text messages:

jsfiddle with scroll

jsfiddle without scroll

嘿看小鸭子会跑 2024-09-11 12:19:27

我想出了一个不需要 JavaScript 的解决方案,这意味着它运行得更快。当将聊天滚动到底部时,需要注意很多变量,例如正在加载和改变滚动高度的图像。

在我提出的解决方案中,聊天在可滚动包装器中旋转 180 度。在包装器内部,您有另一个旋转 180 度的 div。这样,当新消息出现时,内部 div 自然浮动到底部。

<div id="chat_wrap" class="scrollable" style="transform-origin: 50% 50%; transform: rotate(180deg);" onscroll="reverseScroll()">
   <div id="messages_wrap" style="float: left;width: 100%; transform: rotate(180deg);">
      <div class="message">Your message</div>
   </div>
</div>

旁注,我的“chat_wrap”div 具有绝对位置,顶部/底部/左/右设置为适合它应该在的位置。

I came up with a solution that doesn't require javascript, which means it works faster. When scrolling chat to bottom, there's just to many variables to watch out for, as an example images that are loading and alter the scroll height.

In my proposed solution the chat is rotated 180degrees in a scrollable wrapper. Inside the wrapper, you have another div that is rotated 180 degrees. This way the inner div floats naturally to bottom, also when new messages appear.

<div id="chat_wrap" class="scrollable" style="transform-origin: 50% 50%; transform: rotate(180deg);" onscroll="reverseScroll()">
   <div id="messages_wrap" style="float: left;width: 100%; transform: rotate(180deg);">
      <div class="message">Your message</div>
   </div>
</div>

Side note, my "chat_wrap" div has position absolute with top/bottom/left/right set to fit where it's supposed to.

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