chat.vue 2.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100
  1. <template>
  2. <div id="chat">
  3. <div id="contents">
  4. <div
  5. class="chat-item flex justify-start items-start"
  6. v-for="(i, index) in chatList"
  7. :key="index"
  8. >
  9. <div class="chat-msg">
  10. <!-- {{ user_info }} -->
  11. <!-- <span :class="{ my: i.UserId == user_info.UserId }" class="chat-name">
  12. {{ i.Nickname }} &nbsp;
  13. </span> -->
  14. <span class="chat-name" :class="{ my: isMe(i.UserId) }"> {{ i.Nickname }} &nbsp; </span>
  15. <span class="chat-content"> {{ i.text }}</span>
  16. </div>
  17. </div>
  18. </div>
  19. </div>
  20. </template>
  21. <script setup lang="ts">
  22. // import { propTypes } from "/@/utils/propTypes";
  23. import { computed, watch } from 'vue';
  24. import { ChatContentType } from '/@/store/modules/rtc';
  25. import { propTypes } from '/@/utils/propTypes';
  26. const props = defineProps({
  27. currentUser: propTypes.string.def(''),
  28. chatList: {
  29. type: Array as PropType<ChatContentType[]>,
  30. default: () => [],
  31. },
  32. });
  33. const isMe = computed(() => (currentUser: string) => props.currentUser === currentUser);
  34. const chatAutoScroll = () => {
  35. let el = document.getElementById('chat');
  36. let client_h = document.getElementById('chat')?.clientHeight;
  37. let all = document.getElementById('contents')?.clientHeight;
  38. // console.log("chatAutoScroll", client_h, all);
  39. el?.scrollTo(0, (client_h || 0) + (all || 0));
  40. console.log('拉底');
  41. };
  42. watch(
  43. () => props.chatList,
  44. (_) => {
  45. setTimeout(chatAutoScroll, 100);
  46. },
  47. {
  48. deep: true,
  49. immediate: true,
  50. },
  51. );
  52. </script>
  53. <style scoped lang="scss">
  54. #chat {
  55. // width: 7.03rem;
  56. width: 50%;
  57. max-width: 4rem;
  58. max-height: 4.58rem;
  59. overflow: auto;
  60. position: fixed;
  61. left: 0.44rem;
  62. bottom: 2.83rem;
  63. .chat-item {
  64. text-align: left;
  65. }
  66. .chat-msg {
  67. width: auto;
  68. border-radius: 0.44rem;
  69. background: rgba(0, 0, 0, 0.3);
  70. // padding: 0.17rem 0.28rem;
  71. padding: 0.07rem 0.28rem;
  72. box-sizing: border-box;
  73. display: inline-block;
  74. // margin-bottom: 0.17rem;
  75. margin-bottom: 0.1rem;
  76. .chat-name {
  77. color: #70bbff;
  78. font-size: 0.28rem;
  79. &.my {
  80. color: #ff9a6a;
  81. }
  82. }
  83. .chat-content {
  84. color: #fff;
  85. font-size: 0.28rem;
  86. word-break: break-all;
  87. }
  88. }
  89. &::-webkit-scrollbar {
  90. display: none;
  91. }
  92. }
  93. </style>