ellipsisScroll.vue 1.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172
  1. <!--
  2. * @FileDescription: 文本超出显示省略号 移动鼠标上去滚动显示
  3. * @Author: 黄琪勇
  4. * @Date:2024-03-25 11:50:36
  5. -->
  6. <template>
  7. <div ref="ellipsisScrollDom" :class="{ isScroll: isScroll }" class="ellipsisScroll">
  8. {{ props.title }}
  9. </div>
  10. </template>
  11. <script setup lang="ts">
  12. import { ref, onMounted, onUnmounted } from "vue"
  13. const props = defineProps<{
  14. title: string
  15. }>()
  16. const ellipsisScrollDom = ref<HTMLElement>()
  17. const isScroll = ref(false)
  18. onMounted(() => {
  19. ellipsisScrollDom.value?.addEventListener("mouseenter", handleIsScroll)
  20. ellipsisScrollDom.value?.addEventListener("mouseleave", handleLeaveScroll)
  21. })
  22. onUnmounted(() => {
  23. ellipsisScrollDom.value?.removeEventListener("mouseenter", handleIsScroll)
  24. ellipsisScrollDom.value?.removeEventListener("mouseleave", handleLeaveScroll)
  25. })
  26. let widthCalc = 0
  27. function handleIsScroll(e: MouseEvent) {
  28. const target = e.target as HTMLElement
  29. widthCalc = target.scrollWidth - target.clientWidth
  30. if (widthCalc > 0) {
  31. isScroll.value = true
  32. } else {
  33. isScroll.value = false
  34. }
  35. }
  36. function handleLeaveScroll() {
  37. isScroll.value = false
  38. }
  39. </script>
  40. <style lang="scss" scoped>
  41. .xx {
  42. width: 100%;
  43. }
  44. .ellipsisScroll {
  45. width: 100%;
  46. display: inline-block;
  47. white-space: nowrap;
  48. text-overflow: ellipsis;
  49. overflow: hidden;
  50. &.isScroll {
  51. &:hover {
  52. width: auto;
  53. overflow: visible;
  54. animation: 3s roll linear infinite normal;
  55. }
  56. @keyframes roll {
  57. 0% {
  58. transform: translateX(0);
  59. }
  60. 100% {
  61. transform: translateX(-100%);
  62. }
  63. }
  64. }
  65. }
  66. </style>