Vue 给表格单元格加省略号和hover提示

基于antd vue的a-popover封装一下即可

js 复制代码
<template>
  <a-popover
    v-model="visible"
    trigger="none"
  >
    <template #content>
      {{ text }}
    </template>
    <div
      class="ellipsis"
      @mouseenter="onMouseenter"
      @mouseleave="onMouseleave"
    >
      {{ text }}
    </div>
  </a-popover>
</template>

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

defineProps({
  text: {
    type: String,
    default: '',
  },
});

const visible = ref(false);

const checkOverflow = element => element.scrollWidth > element.clientWidth;

let cancelEnter = true;
let cancelLeave = true;

const onMouseenter = (e) => {
  cancelEnter = false;
  cancelLeave = true;
  setTimeout(() => {
    if (!cancelEnter) {
      visible.value = checkOverflow(e.target);
    }
  }, 100);
};

const onMouseleave = () => {
  cancelLeave = false;
  cancelEnter = true;
  setTimeout(() => {
    if (!cancelLeave) {
      visible.value = false;
    }
  }, 120);
};

</script>

<style lang="less">
.ellipsis {
  white-space: nowrap;
  overflow: hidden;
  text-overflow: ellipsis;
}
</style>
相关推荐
老前端的功夫4 分钟前
前端技术选型的理性之道:构建可量化的ROI评估模型
前端·javascript·人工智能·ubuntu·前端框架
汝生淮南吾在北15 分钟前
SpringBoot+Vue超市收银管理系统
vue.js·spring boot·后端
狮子座的男孩15 分钟前
js函数高级:04、详解执行上下文与执行上下文栈(变量提升与函数提升、执行上下文、执行上下文栈)及相关面试题
前端·javascript·经验分享·变量提升与函数提升·执行上下文·执行上下文栈·相关面试题
p***930317 分钟前
SpringBoot + vue 管理系统
vue.js·spring boot·后端
爱学习的程序媛27 分钟前
《JavaScript权威指南》核心知识点梳理
开发语言·前端·javascript·ecmascript
乐观主义现代人1 小时前
go 面试
java·前端·javascript
8***v2571 小时前
SpringBoot + vue 管理系统
vue.js·spring boot·后端
1***Q7841 小时前
前端在移动端中的离线功能
前端
星环处相逢1 小时前
Nginx 优化与防盗链及扩展配置指南
服务器·前端·nginx
2501_941886861 小时前
多语言微服务架构下的微服务熔断与限流优化实践
javascript