js根据文本是否溢出,判断是否显示el-tooltip、title

本文介绍了一个自定义的Tooltip组件,该组件使用Vue实现,并能够根据元素内容的宽度和高度智能判断是否显示提示信息。通过监听鼠标悬停事件,组件可以有效地解决文字溢出时的提示显示问题。
// customTooltip 组件

<template>
    <div @mouseenter="handleMouseenter" @mouseleave="mouseleave">
        <el-tooltip placement="top" :disabled="disabled" :content="props.content">
            <slot></slot>
        </el-tooltip>
    </div>
</template>

<script setup>
import { ref } from 'vue';

const props = defineProps({
    content: {
        type: String,
        default: ''
    }
})

const disabled = ref(true)

const handleMouseenter = (e) => {
    if(!props.content) {
        return
    }
    const cellChild = e.target.children[0]
    // range 表示文档的一个区域
    const range = document.createRange()
    range.setStart(cellChild, 0)
    range.setEnd(cellChild, cellChild.childNodes.length)

    const flag = getStyle(cellChild, '-webkit-line-clamp')
    if (flag == 'none') {
        // rangeWidth 表示元素内容的宽度
        const rangeWidth = range.getBoundingClientRect().width
        let padding = (parseInt(getStyle(cellChild, 'paddingLeft')) || 0) + (parseInt(getStyle(cellChild, 'paddingRight')) || 0)

        // cellChild.offsetWidth 表示选定区域的宽度
        if (rangeWidth > cellChild.offsetWidth - padding) {
            // 显示tooltip
            disabled.value = false
        }
    } else {
        // rangeHeight 表示元素内容的高度
        const rangeHeight = range.getBoundingClientRect().height
        let padding = (parseInt(getStyle(cellChild, 'paddingTop')) || 0) + (parseInt(getStyle(cellChild, 'paddingBottom')) || 0)

        // cellChild.offsetHeight 表示选定区域的高度
        if (rangeHeight > cellChild.offsetHeight - padding) {
            // 显示tooltip
            disabled.value = false
        }
    }
}

const mouseleave = () => {
    disabled.value = true;
}

// 获取dom的样式
const getStyle = (dom, attr) => {
    return getComputedStyle(dom, null)[attr]
}   
</script>
// 使用 customTooltip 组件

<CustomTooltip :content="formData[item.key] || ''">
    <p class="text">{{ formData[item.key] || '- -' }}</p>
</CustomTooltip>

Vue3 中使用 `el-tooltip` 实现仅在内容溢出显示,可以通过自定义指令或者组件封装的方式来实现。Element Plus 提供的 `el-tooltip` 组件本身没有直接支持“只在内容溢出显示”的功能,但可以借助 DOM 属性和判断逻辑来控制其行为。 ### 方法一:通过自定义指令实现 1. 创建一个自定义指令,在元素挂载后检查是否发生溢出。 2. 如果检测到内容溢出,则启用 `el-tooltip`;否则禁用它。 ```javascript // main.js 或单独的指令文件中注册指令 import { ElTooltip } from 'element-plus'; app.directive('tooltip-on-overflow', { mounted(el, binding) { const isOverflowing = el.scrollWidth > el.clientWidth || el.scrollHeight > el.clientHeight; if (isOverflowing) { // 动态绑定 tooltip 内容 el.setAttribute('title', binding.value); new ElTooltip({ trigger: 'hover', placement: 'top', content: binding.value, }).mount(el); } }, }); ``` 在模板中使用: ```html <template> <div v-tooltip-on-overflow="longText">{{ longText }}</div> </template> <script setup> const longText = "这是一个很长的内容,可能会导致文本溢出容器"; </script> ``` ### 方法二:封装一个条件性显示的组件 创建一个可复用的组件 `ConditionalTooltip.vue`,内部包含对溢出状态的判断。 ```vue <!-- ConditionalTooltip.vue --> <template> <div ref="contentRef" class="conditional-tooltip"> {{ text }} </div> </template> <script setup> import { ref, onMounted } from 'vue'; import { ElTooltip } from 'element-plus'; const props = defineProps({ text: { type: String, required: true, }, }); const contentRef = ref(null); onMounted(() => { const el = contentRef.value; const isOverflowing = el.scrollWidth > el.clientWidth || el.scrollHeight > el.clientHeight; if (isOverflowing) { new ElTooltip({ trigger: 'hover', placement: 'top', content: props.text, }).mount(el); } }); </script> <style scoped> .conditional-tooltip { white-space: nowrap; overflow: hidden; } </style> ``` 在父组件中使用: ```html <template> <ConditionalTooltip :text="longText" /> </template> <script setup> import ConditionalTooltip from './components/ConditionalTooltip.vue'; const longText = "这是一个很长的内容,可能会导致文本溢出容器"; </ConditionalTooltip> ``` ### 方法三:结合 `v-if` 控制渲染逻辑 如果不想使用自定义指令或组件,也可以通过 `ref` 获取 DOM 元素并手动判断是否溢出,然后通过 `v-if` 控制 `el-tooltip` 的显示: ```html <template> <div ref="textContent" class="text-container"> {{ longText }} </div> <el-tooltip v-if="shouldShowTooltip" effect="dark" content="这是完整内容" placement="top"> <span>{{ longText }}</span> </el-tooltip> </template> <script setup> import { ref, onMounted } from 'vue'; const longText = "这是一个很长的内容,可能会导致文本溢出容器"; const shouldShowTooltip = ref(false); const textContent = ref(null); onMounted(() => { const el = textContent.value; shouldShowTooltip.value = el.scrollWidth > el.clientWidth || el.scrollHeight > el.clientHeight; }); </script> <style scoped> .text-container { white-space: nowrap; overflow: hidden; } </style> ``` 以上方法均能实现 `el-tooltip` 在内容溢出时才显示的效果,开发者可以根据项目结构和需求选择最适合的方式 [^5]。 ---
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值