index.vue 1.1 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253
  1. <template>
  2. <el-tooltip
  3. class="my-tooltip"
  4. :disabled="disabled"
  5. :content="content"
  6. :effect="effect"
  7. >
  8. <div ref="tooltipBox" class="text-box" @mouseover="checkWidth">
  9. <span ref="tooltipItem" class="text-item">{{ content }}</span>
  10. </div>
  11. </el-tooltip>
  12. </template>
  13. <script setup lang="ts">
  14. import { PropType } from 'vue'
  15. import { Placement } from 'element-plus'
  16. const props = defineProps({
  17. content: {
  18. type: String,
  19. required: true,
  20. },
  21. placement: {
  22. type: String as PropType<Placement>,
  23. default: 'bottom',
  24. },
  25. effect: {
  26. type: String as PropType<'dark' | 'light'>,
  27. default: 'dark',
  28. },
  29. })
  30. const disabled = ref(true)
  31. const tooltipBox = ref<HTMLElement | null>(null)
  32. const tooltipItem = ref<HTMLElement | null>(null)
  33. const checkWidth = () => {
  34. const boxWidth = tooltipBox.value!.offsetWidth
  35. const itemWidth = tooltipItem.value!.offsetWidth
  36. disabled.value = boxWidth > itemWidth
  37. }
  38. </script>
  39. <style lang="scss" scoped>
  40. .text-box {
  41. overflow: hidden;
  42. white-space: nowrap;
  43. .text-item {
  44. max-width: 100%;
  45. overflow: hidden;
  46. text-overflow: ellipsis;
  47. line-height: 0.7;
  48. }
  49. }
  50. </style>