当前位置:   article > 正文

vue3简易对话窗口_vue实现一个聊天对话框

vue实现一个聊天对话框
<template>
  <div id="chat-window">
    <ul id="chat-log">
      <li
        v-for="(message, index) in messages"
        :key="index"
        :class="{ received: !message.fromMe }"
      >
        <div class="message-header">{{ message.from }}</div>
        <div class="message-body">{{ message.text }}</div>
      </li>
    </ul>
    <div id="chat-input">
      <textarea v-model="messageText"></textarea>
      <button @click="sendMessage">发送</button>
    </div>
  </div>
</template>

<script lang="ts">
import { defineComponent, ref, nextTick } from "vue";

export default defineComponent({
  name: "ChatWindow",
  setup() {
    const messages = ref<
      Array<{ from: string; text: string; fromMe?: boolean }>
    >([]);
    const messageText = ref<string>("");
    const bol = ref(false);

    function sendMessage() {
      if (messageText.value) {
        if (bol.value) {
          return;
        }
        bol.value = true;
        const newMessage = {
          from: "Me",
          text: "",
          fromMe: true,
        };

        const newMessage1 = {
          from: "You",
          text: "狗",
          fromMe: false,
        };

        messages.value.push(newMessage1);
        messages.value.push(newMessage);
        const originalText = messageText.value;
        // messageText.value = "";

        scrollToBottom(); // 先滚动到底部
        // 等待滚动到底部后再依次显示消息
        nextTick(() => {
          // 定时器每200ms将消息一个一个字符地显示出来
          let currentIndex = 0;
          const timer = setInterval(() => {
            if (currentIndex >= originalText.length) {
              clearInterval(timer);
              nextTick(() => {
                newMessage.text = originalText;
                scrollToBottom(); // 再次滚动到底部
                bol.value = false;
              });
              return;
            }
            const currentText = originalText.substring(0, currentIndex + 1);
            messages.value[messages.value.length - 1].text = currentText;
            scrollToBottom(); // 再次滚动到底部
            currentIndex++;
          }, 200);
        });
      }
    }

    function scrollToBottom() {
      const chatLog = document.getElementById("chat-log");
      if (chatLog) {
        chatLog.scrollTop = chatLog.scrollHeight;
      }
    }

    scrollToBottom(); // 初始化滚动到底部

    return {
      messages,
      messageText,
      sendMessage,
      scrollToBottom,
    };
  },
  mounted() {
    this.scrollToBottom(); // 每次更新聊天记录后滚动到底部
  },
});
</script>

<style>
#chat-window {
  width: 100%;
  height: 100vh;
  border: 1px solid #ccc;
  overflow: hidden;
  display: flex;
  flex-direction: column;
}

#chat-log {
  list-style: none;
  margin: 0;
  padding: 0;
  height: 100%;
  overflow-y: scroll;
}

#chat-log li {
  margin-bottom: 10px;
  padding: 5px;
  border-radius: 5px;
  display: flex;
  flex-direction: column;
  max-width: 100%;
  /* max-width: 80%; */
  align-items: flex-end;
}

.received {
  /* align-self: flex-end; */
  align-items: flex-start !important;
  background-color: #f5f5f5;
}

.message-header {
  font-size: 14px;
  font-weight: bold;
  margin-bottom: 5px;
}

.message-body {
  font-size: 16px;
  line-height: 1.5;
}

#chat-input {
  display: flex;
  align-items: center;
  padding: 10px;
  background-color: #f5f5f5;
}

#chat-input textarea {
  flex: 1;
  margin-right: 10px;
  border-radius: 5px;
  padding: 5px;
  font-size: 16px;
  border: 1px solid #ccc;
}

#chat-input button {
  background-color: #007bff;
  color: #fff;
  padding: 5px 10px;
  border-radius: 5px;
  border: none;
  cursor: pointer;
  font-size: 16px;
}
</style>

  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
  • 28
  • 29
  • 30
  • 31
  • 32
  • 33
  • 34
  • 35
  • 36
  • 37
  • 38
  • 39
  • 40
  • 41
  • 42
  • 43
  • 44
  • 45
  • 46
  • 47
  • 48
  • 49
  • 50
  • 51
  • 52
  • 53
  • 54
  • 55
  • 56
  • 57
  • 58
  • 59
  • 60
  • 61
  • 62
  • 63
  • 64
  • 65
  • 66
  • 67
  • 68
  • 69
  • 70
  • 71
  • 72
  • 73
  • 74
  • 75
  • 76
  • 77
  • 78
  • 79
  • 80
  • 81
  • 82
  • 83
  • 84
  • 85
  • 86
  • 87
  • 88
  • 89
  • 90
  • 91
  • 92
  • 93
  • 94
  • 95
  • 96
  • 97
  • 98
  • 99
  • 100
  • 101
  • 102
  • 103
  • 104
  • 105
  • 106
  • 107
  • 108
  • 109
  • 110
  • 111
  • 112
  • 113
  • 114
  • 115
  • 116
  • 117
  • 118
  • 119
  • 120
  • 121
  • 122
  • 123
  • 124
  • 125
  • 126
  • 127
  • 128
  • 129
  • 130
  • 131
  • 132
  • 133
  • 134
  • 135
  • 136
  • 137
  • 138
  • 139
  • 140
  • 141
  • 142
  • 143
  • 144
  • 145
  • 146
  • 147
  • 148
  • 149
  • 150
  • 151
  • 152
  • 153
  • 154
  • 155
  • 156
  • 157
  • 158
  • 159
  • 160
  • 161
  • 162
  • 163
  • 164
  • 165
  • 166
  • 167
  • 168
  • 169
  • 170
  • 171
  • 172
  • 173
声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/繁依Fanyi0/article/detail/355526?site
推荐阅读
相关标签
  

闽ICP备14008679号