主要代码
class中的样式在uniapp交友项目样式列表 《uniapp交友项目样式列表》中查看
input框中的,:adjust-position=”false” 在键盘弹起时,是否向上顶起页面
<template>
<view>
<!-- 聊天列表 -->
<scroll-view scroll-y :style="'height:' + scrollH + 'px;'">
<block v-for="(item,index) in list " :key="index">
<!-- 左侧气泡 -->
//:style="item.user_id === 1 item.user_id的值是1时,表示这一条消息是当前账号的,
//显示在左侧,反之显示在右侧
<view class="flex align-start px-2 my-2"
:style="item.user_id === 1 ? '' : 'flex-direction:row-reverse'">
<image :src="item.avatar"
style="width: 100rpx;height: 100rpx;"
class="rounded-circle"
></image>
<view class="bg-light p-2 rounded mx-2" style="max-width: 400rpx;min-width: 100rpx;">
{{item.data}}
</view>
</view>
<!-- 右侧气泡 -->
<!-- <view class="flex align-start px-2 my-2" style="flex-direction:row-reverse">
<image :src="item.avatar"
style="width: 100rpx;height: 100rpx;"
class="rounded-circle"
></image>
<view class="bg-light p-2 rounded mx-2" style="max-width: 400rpx;min-width: 100rpx;">
{{item.data}}
</view>
</view> -->
</block>
</scroll-view>
<!-- 底部操作条 -->
<view class="fixed-bottom flex align-center border-top bg-white" style="height: 100rpx;">
//:adjust-position="false" 在键盘弹起时,是否向上顶起页面
<input type="text"
:adjust-position="false"
class="flex-1 rounded bg-light ml-2" style="padding: 5rpx;" placeholder="请文明喷人">
<view class="iconfont icon-fabu flex align-center justify-center font-md animated "
hover-class="jello text-main"
style="width: 100rpx; ">
</view>
</view>
</view>
</template>
<script>
// 模拟当前登录的用户的userid
const uid = 1;
export default {
data() {
return {
scrollH:500,
list:[{
avatar:"/static/default.jpg",
username:"昵称",
data:"我好啊",
type:"text",
create_time:1570783999,
user_id:2,//当为2时 不是当前用户,显示在右侧
},
{
avatar:"/static/default.jpg",
username:"昵称",
data:"我好啊",
type:"text",
create_time:1570783999,
user_id:1,//当为1时 是当前用户,显示在左侧
}]
}
},
onLoad() {
uni.getSystemInfo({
success(res) {
this.scrollH = res.windowHeight - uni.upx2px(101)
}
})
},
methods: {
}
}
</script>
<style>
</style>
页面配置(page.json)
{
"path" : "pages/user-chat/user-chat",
"style" :
{
"app-plus": {
"bounce": "none",//关闭回弹的效果
"titleNView": {
"buttons": [
{
"color": "#333333",
"colorPressed": "#FD597C",
"float": "right",
"fontSize": "20px",
"fontSrc": "/static/iconfont.ttf",
"text": "\ue628"
}
]
}
}
}
}
组件化
<template>
<view>
<!-- 聊天列表 -->
<scroll-view scroll-y :style="'height:' + scrollH + 'px;'">
<block v-for="(item,index) in list " :key="index">
<userChatList :index="index" :item="item" :pretime="index > 0 ? list[index-1].create_time : 0"></userChatList>
</block>
</scroll-view>
<!-- 底部操作条 -->
<view class="fixed-bottom flex align-center border-top bg-white" style="height: 100rpx;">
<input type="text"
:adjust-position="false"
v-model="content"
class="flex-1 rounded bg-light ml-2" style="padding: 5rpx;" placeholder="请文明喷人">
<view class="iconfont icon-fabu flex align-center justify-center font-md animated "
@click="submit"
hover-class="jello text-main"
style="width: 100rpx; ">
</view>
</view>
</view>
</template>
<script>
import userChatList from "@/components/user-chat/user-chat-list.vue"
export default {
components:{
userChatList
},
data() {
return {
content:"",
scrollH:500,
list:[{
avatar:"/static/default.jpg",
username:"昵称",
data:"我好啊",
type:"text",
create_time:1570783999,
user_id:2,//当为2时 不是当前用户,显示在右侧
},
{
avatar:"/static/default.jpg",
username:"昵称",
data:"我好啊",
type:"text",
create_time:1570783999,
user_id:1,//当为1时 是当前用户,显示在左侧
}]
}
},
onLoad() {
uni.getSystemInfo({
success(res) {
this.scrollH = res.windowHeight - uni.upx2px(101)
}
})
},
methods: {
// 发送
submit() {
let obj = {
avatar:"/static/default.jpg",
username:"昵称",
data:this.content,
type:"text",
create_time:(new Date()).getTime(),
user_id:1,//当为1时 是当前用户,显示在左侧
}
if(this.content === "") {
// 消息提示
return uni.showToast({
title:"消息不能为空",
icon:"none"
})
}
this.list.push(obj)
}
}
}
</script>
<style>
</style>
<template>
<view class=".icon-nv">
<!-- 时间显示 -->
<view v-if="shortTime" class="py-2 flex align-center justify-center font-sm text-light-muted">
{{shortTime}}
</view>
<!-- 消息显示 -->
<view class="flex align-start px-2 my-2"
:style="isSelf ? '' : 'flex-direction:row-reverse'">
<image :src="item.avatar"
style="width: 100rpx;height: 100rpx;"
class="rounded-circle"
></image>
<view class="bg-light p-2 rounded mx-2" style="max-width: 400rpx;">
{{item.data}}
</view>
</view>
</view>
</template>
<script>
// 引入时间库
import $T from "@/common/time.js"
// 模拟当前登录的用户的userid
const uid = 1;
export default {
props:{
item:Object,
index:Number,
pretime:[Number,String]
},
computed:{
// 是否是登录用户本人
isSelf(){
return uid === this.item.user_id
},
// 转换时间
shortTime() {
return $T.getChatTime(this.item.create_time,this.pretime)
}
}
}
</script>
<style>
</style>
监听键盘的高度变化,防止键盘弹起将页面挤上去
input的adjust-position属性
1:input框中的,:adjust-position=”false” 在键盘弹起时,是否向上顶起页面
uni.onKeyboardHeightChange()
2:监听键盘的高度变化,只在app端和小程序端可以使用