css位置:绝对,底部:0,父级溢出:auto



这是一个聊天室,有一个div.words作为容器,里面有一个.contentdiv。

我希望文本从.content的底部显示,所以我使用位置:绝对,底部:0;上。内容。但如果内容增长到高于。单词,我希望它显示滚动条。所以我添加了溢出:自动打开。单词。

如果删除底部:0,溢出:auto将正常工作。但如果底部为0,则不起作用。

如何让输入从底部显示,溢出时会有滚动条?

CSS:

.tbchat-room{
    position: fixed;
    bottom:0;
    width:260px;
    border:1px solid #aaa;
    background:#fff;
}
.tbchat-room .head{
    padding: 3px 10px;
    background:#3d9ac1;
    color:#fff;
}
.tbchat-room .words{
    height:230px;
    background:#ececec;
    overflow:auto;
    position:relative;
}
.tbchat-room .words .content{
    position:absolute;
}
.tbchat-room .say{
    width:246px;
    margin:0;
    border-top: 1px solid #ccc;
    border-bottom: 0;
    border-left: 0;
    border-right: 0;
    padding:4px 6px;
}
.pull-right{
    float:right;
}
.content{
    padding:5px 10px;
}

HTML:

<div class="tbchat-room">
    <div class="head">
        <span class="title">Chat Room</span>
        <a class="room-close-button pull-right">&times;</a>
    </div>
    <div class="words">
        <div class="content">
            <div>sample text</div>
            <div>sample text</div>
        </div>
    </div>
    <input class="say" type="text"/>
</div>

http://jsfiddle.net/s8jD5/

每次在框中添加新的聊天消息时,请尝试保留overflow-y:auto并运行此jQuery语句

$('.words').scrollTop($('.words')[0].scrollHeight);

您将始终处于的底部

您可以给.contentdiv一个最大高度,并将溢出设置为该div,而不是.wordsdiv,如下所示:

CSS

.tbchat-room .words .content{
    position:absolute;
    bottom:0;
    overflow:auto;
    width: 100%;
    box-sizing: border-box;
    max-height: 230px;
}

更新的Fiddle

编辑:

您还应该将以下代码添加到您的Javascript中,以确保在添加新消息时自动转到.contentdiv的底部(即查看新消息(

$('.content').scrollTop($('.content')[0].scrollHeight);

最新更新