schangxiang@126.com
2025-05-07 cace264ad9d86a7831099810b079da1141957add
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
<!-- 
<EllipsisTooltipLabel width="100%" :content="content" />
 
-->
<template>
  <div
    class="content"
    :style="{ width: props.width }"
    @mouseover="mouseover"
    @mouseleave="mouseleave"
    :title="props.tooltipContent ? props.tooltipContent : props.content"
    :data-tooltip-disabled="!visible"
    :data-tooltip-effect="props.effect"
    :data-tooltip-placement="props.placement"
    :data-tooltip-popper-class="props.popperClass"
    :data-tooltip-raw-content="props.rawContent"
  >
    <span ref="contentRef">
      <slot name="content">{{ props.content }}</slot>
    </span>
  </div>
</template>
<script setup lang="ts">
import { ref } from 'vue'
interface Props {
  width: string // 超出宽度时用省略号表示
  content: string // 显示内容
  tooltipContent?: string // 提示内容(设置‘提示内容’与‘显示内容’不同)
  effect?: string
  placement?: string
  popperClass?: string
  disabled?: boolean
  rawContent?: boolean
}
const props = withDefaults(defineProps<Props>(), {
  effect: 'dark',
  placement: 'top-start',
  content: '',
  popperClass: '',
  width: '',
  tooltipContent: '',
  disabled: false,
  rawContent: false,
})
 
let visible = ref(false)
const contentRef = ref()
const mouseover = function (): void {
  // 计算span标签的offsetWidth与父盒子元素的offsetWidth,来判断tooltip是否显示
  visible.value =
    contentRef.value.offsetWidth > contentRef.value.parentNode.offsetWidth ? true : false
}
const mouseleave = function (): void {
  visible.value = false
}
</script>
<style lang="scss" scoped>
.content {
  text-overflow: ellipsis;
  word-break: keep-all;
  white-space: nowrap;
  overflow: hidden;
}
</style>