闲暇时间,介绍一下我做一个聊天窗口的心得。如图:

首先要考虑的是得判断出是自己的信息还是对方发来的信息,给出如图的布局,切换不同的类。

<li class="clearfix" v-for="(talk,index) in talks" v-bind:class="{'even':othername!=talk.send_from_id,'odd':othername==talk.send_from_id}">
<span v-show="showTime(index)" :id="index">{{getLocalTime(talk.message_time)}}</span>
<img v-bind:src="getUserimg" alt="" v-if="othername!=talk.send_from_id">
<img v-bind:src="meUserimg" alt="" v-else>
<b></b>
<div>
<p>{{talk.content}}</p>
</div>
</li>
/*even 偶  odd 奇*/
.content-talk ul li.odd img{width:45px;height:45px;border-radius: %;float: left;background: url(../assets/peoper.png) no-repeat center center;background-size: 45px 45px;}
.content-talk ul li.even img{width:45px;height:45px;border-radius: %;float: right;background: url(../assets/peoper.png) no-repeat center center;background-size: 45px 45px;}
.content-talk ul li.odd .in_talk p{background: #fff;float:left;margin-left: 10px;max-width:%;font-size: 14px;padding:10px;border-radius: 6px;}
.content-talk ul li.odd b{width:8px;height:14px;display: inline-block;background: url(../images/left.jpg) no-repeat;background-size: 8px 14px;position: absolute;left:58px;top:8px;}
.content-talk ul li.even .in_talk p{background: #f6fff6;float:right;margin-right: 10px;max-width:%;border-radius: 6px;font-size: 14px;padding:10px;
}
.content-talk ul li.even b{width:8px;height:14px;display: inline-block;background: url(../images/right.jpg) no-repeat;background-size: 8px 14px;position: absolute;right:58px;top:8px;}

或许你会看得一脸懵,根据接口拿到的信息,othername就是当前与你聊天的这个人,如果与send_from_id一致那么就是对方的信息,反之是你自己发的信息。

接下来就是输入框了,定位在底部,因为没有发送按钮,所以必须触发键盘和手机的Enter键,还有一个问题是,在我手机Enter键显示的是'换行'字眼。

以下是解决办法,加了form标签,但必须关掉action的功能。“换行”就变成“前往”

<form action=""  onkeydown="if(event.keyCode==13)return false;">
<mt-field type="text" v-model="content" @keyup.native="send($event)"></mt-field>
</form>
//发送消息
send(ev){
if(ev.keyCode==){

var sendtoid=this.userid;
// console.log(sendtoid)
var sendfromid="";
var subject="";
var replyid=;
if(this.content!=''){
this.$http.post(this._getUrl()+"User/send",
{"sendfromid":sendfromid,"sendtoid":sendtoid,"subject":subject,"content":this.content,"replyid":replyid},{emulateJSON:true}
).then((response) => {
response = response.body;
if(response.error_code==){
Toast({
message: '发送成功'
});
this.getread(); //重新获取数据
this.content=''
}
else{
Toast({
message: response.error_msg
});
}
});
}else{
Toast({
message: '不能为空'
});
}
}
},

接下来我用到了Mint-ui的Loadmore  顶部下拉刷新加载更多消息

http://mint-ui.github.io/docs/#/zh-cn2/loadmore

接口数据,如图:

在 loadTop()函数加载更多中,不能再用push()。

如下这3张图可能形象一点,created(),获取到第一页数据

第一页数据:

第二页数据:

首先得让第二页最底下的数据先添加进来,用了for循环 for(let j = sayLists.length-1; j >-1; j--)  数组序号index从0开始,

然后数据就需要使用unshift() 方法向数组的开头添加元素,于是用了下面方法:

     //加载更多
loadTop() {
// var userid=this.$route.params.userid;
let page = (Math.ceil(this.talks.length/this.pagesize))+ ;
// console.log(page)
this.$http.get(this._getUrl()+"User/readAll/userid/" + this.userid +"/pagesize/"+this.pagesize+"/page/" + page).then((response) => {
let say_code =response.body.error_code;
let sayLists = response.body.list;
if(say_code==''){
for (let j = sayLists.length-; j >-; j--) {

// console.log(sayLists[j])
this.talks.unshift(sayLists[j]);
this.contentlogin=true;
}
}else{
this.allLoaded = true;
this.$refs.loadmore.onTopLoaded();
this.contentlogin=false;
}
});
},

最后的问题是,页面进来显示最新聊天,在body底部。

//显示最底部信息
bottomshow(){
let count=;
let interval=setInterval(() =>{
if(count>){
clearInterval(interval);
}
count++;
if(document.body.scrollTop != document.body.scrollHeight){
document.body.scrollTop = document.body.scrollHeight;
// console.log(document.body.scrollTop)
}
if(document.body.scrollTop == document.body.scrollHeight){
clearInterval(interval);
}
},);
}

加上定时器,是因为页面一进来未能获取到盒子高度并发生滚动,所以加了定时器,利用count++,延迟点。

vue,一路走来(9)--聊天窗口的更多相关文章

  1. vue,一路走来(1)--构建vue项目

    2016年12月--2017年5月,接触前端框架vue,一路走来,觉得有必要把遇到的问题记录下来. 那时,vux用的是1.0的vue,然而vue2.0已经出来了,于是我结合了mint-ui一起来做项目 ...

  2. vue,一路走来(17)--vue使用scss,并且全局引入公共scss样式

    最近朋友问如何在vue项目中使用scss样式,想起之前项目是直接在main.js直接import css文件的,然而main.js不可以直接import scss文件. import './asset ...

  3. vue,一路走来(16)--本地及手机调试

    闲暇时间记录一下如何绑定域名,实现本地及手机调试的过程.我的是微信开发项目,很多功能及操作都是基于微信来开发的,理所当然的就用到微信开发者工具了. 1.首先打开目录C:\Windows\System3 ...

  4. vue,一路走来(13)--vue微信分享

    vue微信分享 今天记录一下vue微信分享. 1.先登录微信公众平台进入“公众号设置”的“功能设置”里填写“JS接口安全域名”.这个不多说,见文档,只有绑定了才能进行下一步的动作 2.需要引入js文件 ...

  5. vue,一路走来(12)--父与子之间传参

    今天想起一直没有记录父组件与子组件的传参问题,这在项目中一直用到. 父向子组件传参 Index.vue父组件中 <component-a :msgfromfa="(positionno ...

  6. vue,一路走来(7)--响应路由参数的变化

    今天描述的问题估计会有很多人也遇到过. vue-router多个路由地址绑定一个组件造成created不执行 也就是文档描述的,如下图 我的解决方案: created () { console.log ...

  7. vue,一路走来(6)--微信支付

    微信支付 https://pay.weixin.qq.com/wiki/doc/api/jsapi.php?chapter=7_7&index=6 分享一下vue实现微信支付.在微信浏览器里面 ...

  8. vue,一路走来(3)--数据交互vue-resource

    所有的静态页面布局完成后,最重要的就是数据交互了,简单来说,vue-resource就像jquery里的$.ajax,用来和后台交互数据的.放在created或ready里运行来获取或者更新数据的.不 ...

  9. vue,一路走来(2)--路由vue-router

    安装 Mint UI cnpm install mint-ui --save 如果你的项目会用到 Mint UI 里较多的组件,最简单的方法就是把它们全部引入.此时需要在入口文件 main.js 中: ...

随机推荐

  1. django之路由的理解

    一:路由 简单的路由过程图: 1. 路由的定义位置 路由定义方式一:主路由和子路由分开定义 主路由的定义 urls.py from django.conf.urls import url from d ...

  2. 饿了么CTO张雪峰:允许90后的技术人员“浮躁“一点

    编者按:今年4月,饿了么正式加入了阿里新零售战队,进一步加速其在本地生活市场的扩张速度.在创业9年的时间中,饿了么在外卖领域经历了真正的“从0到1”,尤其是在外卖平台的技术升级方面,越过了一个又一个的 ...

  3. OC + RAC (九) 过滤

    // 跳跃 : 如下,skip传入2 跳过前面两个值 // 实际用处: 在实际开发中比如 后台返回的数据前面几个没用,我们想跳跃过去,便可以用skip - (void)skip { RACSubjec ...

  4. [USACO2011 Feb]Best Parenthesis

    Time Limit: 10 Sec Memory Limit: 128 MB Description Recently, the cows have been competing with stri ...

  5. 20180712-Java Character类

    char ch = 'a';// Unicode for uppercase Greek omega characterchar uniChar = '\u039A';//字符数组char[] cha ...

  6. LRU management

    LRU management 字典树用来查找值,实现map<string,int>操作 tips:tot必须从一开始QAQ #include<bits/stdc++.h> us ...

  7. 129、TensorFlow计算图的可视化

    import tensorflow as tf # Build your graph x = tf.constant([[37.0, -23.0], [1.0, 4.0]], name="i ...

  8. Keras 层layers总结

    https://blog.csdn.net/u010159842/article/details/78983841

  9. canvas绘制验证码

    css样式: <style> body{ text-align: center; } canvas{ background:#ddd; } </style> body中添加标签 ...

  10. 爬虫之requests 请求

    1.发送不同的请求 import requests r = requests.get('https://www.baidu.com/') r = requests.post('http://httpb ...