Align text with bottom as it comes in during a chat?

独自空忆成欢 提交于 2019-12-04 09:20:46

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;
    }
Ajith S

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

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.

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!