主要代码

class中的样式在uniapp交友项目样式列表 《uniapp交友项目样式列表》中查看
input框中的,:adjust-position=”false” 在键盘弹起时,是否向上顶起页面

  1. <template>
  2. <view>
  3. <!-- 聊天列表 -->
  4. <scroll-view scroll-y :style="'height:' + scrollH + 'px;'">
  5. <block v-for="(item,index) in list " :key="index">
  6. <!-- 左侧气泡 -->
  7. //:style="item.user_id === 1 item.user_id的值是1时,表示这一条消息是当前账号的,
  8. //显示在左侧,反之显示在右侧
  9. <view class="flex align-start px-2 my-2"
  10. :style="item.user_id === 1 ? '' : 'flex-direction:row-reverse'">
  11. <image :src="item.avatar"
  12. style="width: 100rpx;height: 100rpx;"
  13. class="rounded-circle"
  14. ></image>
  15. <view class="bg-light p-2 rounded mx-2" style="max-width: 400rpx;min-width: 100rpx;">
  16. {{item.data}}
  17. </view>
  18. </view>
  19. <!-- 右侧气泡 -->
  20. <!-- <view class="flex align-start px-2 my-2" style="flex-direction:row-reverse">
  21. <image :src="item.avatar"
  22. style="width: 100rpx;height: 100rpx;"
  23. class="rounded-circle"
  24. ></image>
  25. <view class="bg-light p-2 rounded mx-2" style="max-width: 400rpx;min-width: 100rpx;">
  26. {{item.data}}
  27. </view>
  28. </view> -->
  29. </block>
  30. </scroll-view>
  31. <!-- 底部操作条 -->
  32. <view class="fixed-bottom flex align-center border-top bg-white" style="height: 100rpx;">
  33. //:adjust-position="false" 在键盘弹起时,是否向上顶起页面
  34. <input type="text"
  35. :adjust-position="false"
  36. class="flex-1 rounded bg-light ml-2" style="padding: 5rpx;" placeholder="请文明喷人">
  37. <view class="iconfont icon-fabu flex align-center justify-center font-md animated "
  38. hover-class="jello text-main"
  39. style="width: 100rpx; ">
  40. </view>
  41. </view>
  42. </view>
  43. </template>
  44. <script>
  45. // 模拟当前登录的用户的userid
  46. const uid = 1;
  47. export default {
  48. data() {
  49. return {
  50. scrollH:500,
  51. list:[{
  52. avatar:"/static/default.jpg",
  53. username:"昵称",
  54. data:"我好啊",
  55. type:"text",
  56. create_time:1570783999,
  57. user_id:2,//当为2时 不是当前用户,显示在右侧
  58. },
  59. {
  60. avatar:"/static/default.jpg",
  61. username:"昵称",
  62. data:"我好啊",
  63. type:"text",
  64. create_time:1570783999,
  65. user_id:1,//当为1时 是当前用户,显示在左侧
  66. }]
  67. }
  68. },
  69. onLoad() {
  70. uni.getSystemInfo({
  71. success(res) {
  72. this.scrollH = res.windowHeight - uni.upx2px(101)
  73. }
  74. })
  75. },
  76. methods: {
  77. }
  78. }
  79. </script>
  80. <style>
  81. </style>

页面配置(page.json)

  1. {
  2. "path" : "pages/user-chat/user-chat",
  3. "style" :
  4. {
  5. "app-plus": {
  6. "bounce": "none",//关闭回弹的效果
  7. "titleNView": {
  8. "buttons": [
  9. {
  10. "color": "#333333",
  11. "colorPressed": "#FD597C",
  12. "float": "right",
  13. "fontSize": "20px",
  14. "fontSrc": "/static/iconfont.ttf",
  15. "text": "\ue628"
  16. }
  17. ]
  18. }
  19. }
  20. }
  21. }

image.png

组件化

  1. <template>
  2. <view>
  3. <!-- 聊天列表 -->
  4. <scroll-view scroll-y :style="'height:' + scrollH + 'px;'">
  5. <block v-for="(item,index) in list " :key="index">
  6. <userChatList :index="index" :item="item" :pretime="index > 0 ? list[index-1].create_time : 0"></userChatList>
  7. </block>
  8. </scroll-view>
  9. <!-- 底部操作条 -->
  10. <view class="fixed-bottom flex align-center border-top bg-white" style="height: 100rpx;">
  11. <input type="text"
  12. :adjust-position="false"
  13. v-model="content"
  14. class="flex-1 rounded bg-light ml-2" style="padding: 5rpx;" placeholder="请文明喷人">
  15. <view class="iconfont icon-fabu flex align-center justify-center font-md animated "
  16. @click="submit"
  17. hover-class="jello text-main"
  18. style="width: 100rpx; ">
  19. </view>
  20. </view>
  21. </view>
  22. </template>
  23. <script>
  24. import userChatList from "@/components/user-chat/user-chat-list.vue"
  25. export default {
  26. components:{
  27. userChatList
  28. },
  29. data() {
  30. return {
  31. content:"",
  32. scrollH:500,
  33. list:[{
  34. avatar:"/static/default.jpg",
  35. username:"昵称",
  36. data:"我好啊",
  37. type:"text",
  38. create_time:1570783999,
  39. user_id:2,//当为2时 不是当前用户,显示在右侧
  40. },
  41. {
  42. avatar:"/static/default.jpg",
  43. username:"昵称",
  44. data:"我好啊",
  45. type:"text",
  46. create_time:1570783999,
  47. user_id:1,//当为1时 是当前用户,显示在左侧
  48. }]
  49. }
  50. },
  51. onLoad() {
  52. uni.getSystemInfo({
  53. success(res) {
  54. this.scrollH = res.windowHeight - uni.upx2px(101)
  55. }
  56. })
  57. },
  58. methods: {
  59. // 发送
  60. submit() {
  61. let obj = {
  62. avatar:"/static/default.jpg",
  63. username:"昵称",
  64. data:this.content,
  65. type:"text",
  66. create_time:(new Date()).getTime(),
  67. user_id:1,//当为1时 是当前用户,显示在左侧
  68. }
  69. if(this.content === "") {
  70. // 消息提示
  71. return uni.showToast({
  72. title:"消息不能为空",
  73. icon:"none"
  74. })
  75. }
  76. this.list.push(obj)
  77. }
  78. }
  79. }
  80. </script>
  81. <style>
  82. </style>
  1. <template>
  2. <view class=".icon-nv">
  3. <!-- 时间显示 -->
  4. <view v-if="shortTime" class="py-2 flex align-center justify-center font-sm text-light-muted">
  5. {{shortTime}}
  6. </view>
  7. <!-- 消息显示 -->
  8. <view class="flex align-start px-2 my-2"
  9. :style="isSelf ? '' : 'flex-direction:row-reverse'">
  10. <image :src="item.avatar"
  11. style="width: 100rpx;height: 100rpx;"
  12. class="rounded-circle"
  13. ></image>
  14. <view class="bg-light p-2 rounded mx-2" style="max-width: 400rpx;">
  15. {{item.data}}
  16. </view>
  17. </view>
  18. </view>
  19. </template>
  20. <script>
  21. // 引入时间库
  22. import $T from "@/common/time.js"
  23. // 模拟当前登录的用户的userid
  24. const uid = 1;
  25. export default {
  26. props:{
  27. item:Object,
  28. index:Number,
  29. pretime:[Number,String]
  30. },
  31. computed:{
  32. // 是否是登录用户本人
  33. isSelf(){
  34. return uid === this.item.user_id
  35. },
  36. // 转换时间
  37. shortTime() {
  38. return $T.getChatTime(this.item.create_time,this.pretime)
  39. }
  40. }
  41. }
  42. </script>
  43. <style>
  44. </style>

监听键盘的高度变化,防止键盘弹起将页面挤上去

input的adjust-position属性

1:input框中的,:adjust-position=”false” 在键盘弹起时,是否向上顶起页面

uni.onKeyboardHeightChange()

2:监听键盘的高度变化,只在app端和小程序端可以使用