<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width,initial-scale=1,minimum-scale=1,maximum-scale=1,user-scalable=no" />
<title></title>
<style type="text/css">
*{margin: 0;padding: 0;}
html,body,#page{width: 100%;height: 100%;}
#page{position: relative;}
#page header{width: 100%;height: 45px;color: white;font-size: 15px;position: relative;text-align: center;background: #40a2fe;line-height: 45px;}
#page header .logo{position: absolute;width: 33px;height: 33px;border-radius: 50%;left: 10px;top: 6px;}
#page header .logo img{width: 100%;height: 100%;display: block;border-radius: 50%;}
#page .msg-box{position: absolute;top: 45px;left: 0;right: 0;bottom: 38px;overflow: auto;}
#page .msg-box ul{}
#page .msg-box ul li{font-size: 12px;list-style: none;overflow: auto;}
#page .msg-box ul .all-list{}
#page .msg-box ul .all-list img{width: 45px;height: 45px;border-radius: 50%;display: block;margin: 8px;float: left;}
#page .msg-box ul .all-list p{max-width: 239px;padding: 12px;float: left;background: #a2d6f3;color: white;border: 1px solid #91c9e9;border-radius: 5px;box-shadow: 2px 2px #369bdb;margin-top: 15px;word-wrap:break-word; word-break:normal;}
/*#page .msg-box ul .all-list:after{content: "";clear: both;visibility: hidden;height: 0;}*/
#page .msg-box ul .muchlist{padding: 10px;text-align: center;}
#page .msg-box ul li a{padding: 5px 10px;background: #e2e2e2;text-decoration: none;font-size: 12px;border-radius: 5px;color: #666;}
#page .msg-box ul li a:hover{text-decoration: none;}
#page .msg-box ul .right p{box-shadow: -2px 2px #369bdb;float: right;}
#page .msg-box ul .right img{float: right;}
#page .send-box{position: absolute;left: 0;width: 100%;bottom: 0;background: #CECECE;height: 38px;text-align: center;}
#page .send-box input{width: 298px;height: 30px;font-size: 12px;border: 1px solid #fff;border-radius: 5px;margin-top: 3px;}
#page .send-box button{height: 30px;padding: 0 10px;border:1px solid #cecece;color: white;}
</style>
</head>
<body>
<div id="page">
<header>
<div class="logo">
![](img/logo-me.png)
</div>
于永豪
</header>
<div class="msg-box">
<ul id="box" style="height: 100%;overflow: auto;">
<li class="muchlist" >
<a href="javascript:void(0)" v-on:click="historymsn()">查看更多历史消息</a>
</li>
<li v-for="item in items" :class="{right:item.right}" class="all-list">
![](img/logo-else.png)
<p>{{item.text}}</p>
</li>
</ul>
</div>
<div class="send-box">
<input type="text" name="" id="" value="" v-model="msgs" v-on:keyup.13="send()"/>
<button>发送</button>
</div>
</div>
<script src="https://cdn.bootcss.com/jquery/2.2.2/jquery.js"></script>
<script src="https://cdn.bootcss.com/vue/2.4.2/vue.js"></script>
<script type="text/javascript">
var datamsn = [
{
text:"你好1",
right:0
},
{
text:"finethanks",
right:1
},
{
text:"finethanks",
right:1
},
{
text:"finethanks",
right:1
},
{
text:"finethanks",
right:1
},
{
text:"finethanks",
right:1
},
{
text:"finethanksfinethanksfinethanksfinethanksfinethanksfinethanksfinethanksfinethanksfinethanksfinethanksfinethanks",
right:1
},
{
text:"finethanks",
right:1
},
{
text:"finethanks",
right:1
},
{
text:"finethanks",
right:1
},
{
text:"finethanks",
right:1
},
]
var cc = datamsn.reverse()
var dataIm = [
{
text:"数据已",
right:0
},
{
text:"数据已",
right:0
},
{
text:"数据已",
right:1
},
{
text:"数据已",
right:0
},
{
text:"数据已",
right:0
},
{
text:"数据已",
right:0
},
{
text:"数据已",
right:0
},
{
text:"数据已",
right:0
},
{
text:"数据已",
right:0
},
{
text:"数据已",
right:0
},
{
text:"数据已",
right:0
},
{
text:"数据已",
right:0
}
]
var vm = new Vue({
el:"#page",
data:{
items:dataIm,
msgs:'',
historymsg:true
},
computed:{
sendbox:function(){
return document.getElementById("box");
}
},
methods:{
send:function(){
this.items.push({
text:this.msgs,
right:1
})
this.msgs=''
},
historymsn: function(){
this.historymsg = false;
for(var i=0;i<datamsn.length;i++){
this.items.unshift(cc[i]);
}
}
},
updated:function(){
if(this.historymsg){
document.getElementById("box").scrollTop=document.getElementById("box").scrollHeight
}else{
this.historymsg = true;
var li = document.getElementById('box').getElementsByTagName('li');
var liheight = 0;
for(var i=0;i<datamsn.length;i++){
liheight += li[i].offsetHeight;
}
console.log(liheight);
document.getElementById("box").scrollTop = liheight;
}
}
})
</script>
</body>
</html>
imweb聊天(vue)添加信息自动滚动,查看历史信息保留最后一条
最后编辑于 :
©著作权归作者所有,转载或内容合作请联系作者
- 文/潘晓璐 我一进店门,熙熙楼的掌柜王于贵愁眉苦脸地迎上来,“玉大人,你说我怎么就摊上这事。” “怎么了?”我有些...
- 文/花漫 我一把揭开白布。 她就那样静静地躺着,像睡着了一般。 火红的嫁衣衬着肌肤如雪。 梳的纹丝不乱的头发上,一...
- 文/苍兰香墨 我猛地睁开眼,长吁一口气:“原来是场噩梦啊……” “哼!你这毒妇竟也来了?” 一声冷哼从身侧响起,我...
推荐阅读更多精彩内容
- 用scrollRangeToVisible函数进行滚动,可以跳动到最后一行内容上 [_textView scrol...