vue中实现聊天的功能,vue2+vant;聊天界面滚动条始终在底部,H5中自己写一个聊天

在Vue中实现聊天界面的功能,并使滚动条始终保持在底部,可以通过监听消息的添加和DOM更新,然后使用原生JavaScript方法来滚动到底部。以下是一个简单的示例:




<template>
  <div class="chat-container" ref="chatContainer">
    <div class="messages">
      <div v-for="(message, index) in messages" :key="index" class="message">
        {{ message }}
      </div>
    </div>
    <div class="input-container">
      <input v-model="inputMessage" @keyup.enter="sendMessage" type="text" placeholder="输入消息" />
      <button @click="sendMessage">发送</button>
    </div>
  </div>
</template>
 
<script>
export default {
  data() {
    return {
      messages: [],
      inputMessage: ''
    };
  },
  watch: {
    messages() {
      this.scrollToBottom();
    }
  },
  methods: {
    sendMessage() {
      if (this.inputMessage.trim() === '') {
        return;
      }
      this.messages.push(this.inputMessage);
      this.inputMessage = '';
    },
    scrollToBottom() {
      this.$nextTick(() => {
        const container = this.$refs.chatContainer;
        container.scrollTop = container.scrollHeight;
      });
    }
  },
  mounted() {
    this.scrollToBottom();
  }
};
</script>
 
<style scoped>
.chat-container {
  height: 100%;
  overflow-y: scroll;
  padding: 10px;
}
.messages {
  padding-bottom: 50px; /* Leave space for input field */
}
.message {
  padding: 10px;
  border-bottom: 1px solid #ccc;
}
.input-container {
  position: fixed;
  bottom: 0;
  width: 100%;
  padding: 10px;
  background-color: white;
}
input {
  width: 100%;
  padding: 10px;
  margin-right: -1px; /* Align with send button */
  border: none;
  box-sizing: border-box;
}
button {
  width: 100px;
  padding: 10px;
  border: none;
  background-color: #007bff;
  color: white;
  cursor: pointer;
}
</style>

在这个例子中,messages数组用于存储聊天信息,inputMessage用于暂存用户输入的消息。当用户按下Enter键或点击发送按钮时,sendMessage方法会被触发,将输入的消息加入到messages数组中,并清空输入框。

watch属性用于监听messages数组的变化,一旦有新消息加入,scrollToBottom方法会被调用,将滚动条滚动到聊天容器的最底部。

scrollToBottom方法在mounted生命周期钩子中也被调用,确保进入页面时滚动条位于底部。

请注意,这个例子没有考虑性能优化,如节流和防抖发送消息,以及对大量消息的优化渲染。在实际应用中,可能需要考虑这些因素以避免性能问题。

最后修改于:2024年08月19日 07:52

评论已关闭

推荐阅读

Vue中使用mind-map实现在线思维导图
2024年08月04日
VUE
Web前端最全Vue实现免密登录跳转的方式_vue怎么样不登录返回首页,最强技术实现
2024年08月04日
VUE
vue3 项目搭建教程(基于create-vue,vite,Vite + Vue)
2024年08月04日
VUE
Vue-颜色选择器实现方案——>Vue-Color( 实战*1+ Demo*7)
2024年08月04日
VUE
Vue项目卡顿慢加载?这些优化技巧告诉你!_vue数据多渲染卡顿
2024年08月04日
VUE
vue中的keep-alive详解与应用场景
2024年08月04日
VUE
Vue、React实现excel导出功能(三种实现方式保姆级讲解)
2024年08月04日
vue-office/docx插件实现docx文件预览
2024年08月04日
VUE
java调用js文件的两种方法(支持V8引擎)
2024年08月04日
JavaScript:解决计算精度问题/mathjs/bignumber.js/big.js/decimal.js
2024年08月04日
两周从爬虫小白变大神 _yjs_js_security_passport
2024年08月04日
JS笔记(对象、函数、数组)
2024年08月04日
Markdown.js:强大的纯JavaScript Markdown解析器
2024年08月04日
Vue项目:js模拟点击a标签下载文件并重命名,URL文件地址下载方法、请求接口下载文件方法总结。
2024年08月04日
vue 父组件怎么获取子组件里面的data数据
2024年08月04日
VUE
个人开发实现AI套壳网站快速搭建(Vue+elementUI+SpringBoot)
2024年08月04日
el-table 表格封装并改造实现单元格可编辑
2024年08月04日
none
nodejs环境下创建vue项目、SSH密钥登陆!!!
2024年08月04日
vue+quill+element-ui实现视频、图片上传及缩放保姆级教程,轻松使用富文本
2024年08月04日
【three.js】22. Imported Models导入模型
2024年08月04日