<template>模板
<template>
...
<div ref="textRef" data-fulltext="name" :class={ 'is-overflow': isOverflow }>
{{ name }}
</div>
...
</template>
<script> 模块
<script setup>
import { ref, computed, nextTick } from 'vue'
const textRef = ref()
const isOverflow = computed(async () => {
await nextTick()
const { scrollWidth, offsetWidth } = textRef.value
return scrollWidth > offsetWidth
})
</script>
<style> 模块
<style lang="scss" scoped>
.text {
display: inline-block;
position: relative;
max-width: 100%;
overflow: hidden;
text-overflow: ellipse;
white-space: nowrap;
&:hover::after {
content: attr(data-fulltext);
position: absolute;
padding: 2px;
top: 100%;
left: 0;
z-index: 1;
border: 1px solid #000000;
font-size: 16px;
visibility: hidden;
}
.is-overflow {
&:hover::after {
visibility: visible;
}
}
}
</style>