我有一个聊天程序,我的目标是,当有人键入并发送文本时,我希望文本从底部显示。就像其他聊天框一样。为此,我编写了以下html/css结构。这里的"li“标签是在有人发送文本时动态创建的。
问题是:
问题是,如果我从".chatbox ul“中删除"height:100%”,chatbox中的文本将从底部开始。但在这种情况下,我看不到任何滚动条。但我需要看到滚动条。而且,如果我保持".chatbox ul“中的"height:100%”,我会看到一个滚动条,但chatbox中的文本从顶部开始。
我的目标是有一个滚动条,文本也应该从底部开始。我怎么才能得到这个..??有帮手吗??
<div class="chatBox">
<ul id="messages" class="chatText">
<li>
<div class="chatterName">maverick </div>
<div class="chatterMessage"> hi</div>
<div class="clear"></div>
</li>
<li>
<div class="chatterName">johny</div>
<div class="chatterMessage"> hello</div>
<div class="clear"></div>
</li>
</ul>CSS:
.chatBox
{
background-image: url('../images/DefaultitemBg.gif');
font-family: Verdana;
font-size: 14px;
color: #333333;
border: 1px double #FFFFFF;
padding: 5px;
margin: 10px;
width:76%;
height:600px;
position:relative;
border: 1px solid #E9E9E9;
overflow:auto;
float:left;
}
.chatBox ul
{
list-style-type: none;
padding: 0px;
margin: 0px;
position:absolute;
overflow:auto;
bottom:0;
height:100%;
}
.chatBox ul li
{
border-bottom-style: solid;
border-bottom-width: 1px;
border-bottom-color: #F2F2F2;
margin-bottom: 0px;
}
.chatterName
{
width: 120px;
background-color: #EEEEEE;
color: #333333;
float: left;
border-right-style: solid;
border-right-width: 1px;
border-right-color: #CCCCCC;
height: 30px;
border-bottom-style: solid;
border-bottom-width: 1px;
border-bottom-color: #E6E6E6;
}
.chatterMessage
{
width: 91%;
padding-left:4px;
color: #333333;
float:left;
min-height:30px;
border-bottom-style: solid;
border-bottom-width: 1px;
border-bottom-color: #EFEFEF;
}
.chatText
{
bottom: 0;
left: 0;
border-top-style: 0;
border-right-style: 0;
border-bottom-style: 1;
border-left-style: 0;
border-bottom-width: 1px;
border-bottom-color: #E6E6E6;
width:100%;}
编辑:
See it live here
如果你仔细观察,你会注意到,当我在chatbox中输入文本时,从顶部开始,以前的文本正在消失。我想要的是一个滚动条,这样用户就可以看到以前的消息。
发布于 2012-10-03 22:28:54
这应该可以解决这个问题:
使用max-height而不是height
.chatBox ul
{
list-style-type: none;
padding: 0px;
margin: 0px;
position:absolute;
overflow:auto;
bottom:0;
max-height: 100%;
/*height:100%;*/
}工作示例:http://jsfiddle.net/pt43J/1/
发布于 2012-10-03 21:47:11
您可以使用javascript滚动到底部:
document.getElementById('messages').innerHTML += '<li>'+your message here+'</li>'
window.scrollTo(0, document.body.scrollHeight)https://stackoverflow.com/questions/12709744
复制相似问题