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

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

<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. springboot 集成rabbitMQ

    package com.jd.ng.shiro.config.rabbitMQconfig; import com.jd.ng.shiro.rabbitMqListener.SimpleMessage ...

  2. w = tf.Variable(<initial-value>, name=<optional-name>)

    w = tf.Variable(<initial-value>, name=<optional-name>)

  3. 运行 tensorboard

    使用下面命令总是报错: tensorboard --logdir=mylogdir tensorboard --logdir='./mylogdir' 正确命令 tensorboard --logdi ...

  4. windows命令整理

    本文只是作为知识整理,尽可能的收集一些常用的内网指令.本人原伸手党一枚,希望这些内容对新人有用,大牛可自行忽略. 0x00 内网信息收集 一.单机基础信息收集 如果是获得第一台初始主机的权限的话,我们 ...

  5. 如何删除发布服务器distribution

    在建立发布服务器后自动生成distribution数据库为系统数据库,drop无法删除,实际删除方法如下:在“对象资源管理器”-“复制”上点击右键,选择“禁用发布和分发”,依次执行即可完成该系统数据库 ...

  6. prim 模板

    #include<cstdio> #include<vector> #include<cstring> #include<set> #define ma ...

  7. [CF-GYM]Abu Tahun Mod problem题解

    前言 这道题比较简单,但我还是想了好一会 题意简述 Abu Tahun很喜欢回文. 一个数组若是回文的,那么它从前往后读和从后往前读都是一样的,比如数组\(\left\{1\right\},\left ...

  8. Java架构师/高并发/高可用/高扩展/性能优化/框架源码分析实战

    https://ke.qq.com/course/401944?taid=3389721334391320

  9. BUUCTF | [RoarCTF 2019]Easy Calc

    看一下页面源码,发现了提示: calc.php?num=encodeURIComponent($("#content").val()) $("#content" ...

  10. layoutSubviews 详解

    ios layout机制相关方法 - (CGSize)sizeThatFits:(CGSize)size - (void)sizeToFit ------- - (void)layoutSubview ...