uniapp,h5

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263
  1. <template>
  2. <view class="msg-list">
  3. <view v-for="(msg, index) in messages" :key="index"
  4. :class="{ sent: msg.type === 'sent', received: msg.type === 'received' }">
  5. <view class="msg">
  6. {{ msg.content }}
  7. </view>
  8. </view>
  9. </view>
  10. </template>
  11. <script>
  12. export default {
  13. name: 'MsgList',
  14. props: {
  15. messages: {
  16. type: Array,
  17. default: () => [],
  18. },
  19. },
  20. methods: {
  21. // 新增事件处理函数,用于通知父组件滚动到底部
  22. scrollToBottom() {
  23. this.$emit('scroll-bottom');
  24. },
  25. },
  26. mounted() {
  27. this.$el.addEventListener('scroll', () => {
  28. if (this.$el.scrollTop + this.$el.clientHeight >= this.$el.scrollHeight) {
  29. this.scrollToBottom();
  30. }
  31. });
  32. },
  33. };
  34. </script>
  35. <style scoped>
  36. .msg-list {
  37. display: flex;
  38. flex-direction: column;
  39. gap: 10px;
  40. }
  41. .sent {
  42. align-self: flex-end;
  43. background-color: #DCF8C5;
  44. border-radius: 20px;
  45. padding: 10px;
  46. }
  47. .received {
  48. align-self: flex-start;
  49. background-color: #E5E7EB;
  50. border-radius: 20px;
  51. padding: 10px;
  52. }
  53. .msg {
  54. white-space: pre-wrap;
  55. word-wrap: break-word;
  56. }
  57. </style>