ellipsisScroll.vue 1.7 KB

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