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>
相关推荐
像风一样自由20202 小时前
HTML与JavaScript:构建动态交互式Web页面的基石
前端·javascript·html
aiprtem3 小时前
基于Flutter的web登录设计
前端·flutter
浪裡遊3 小时前
React Hooks全面解析:从基础到高级的实用指南
开发语言·前端·javascript·react.js·node.js·ecmascript·php
why技术3 小时前
Stack Overflow,轰然倒下!
前端·人工智能·后端
幽络源小助理3 小时前
SpringBoot基于Mysql的商业辅助决策系统设计与实现
java·vue.js·spring boot·后端·mysql·spring
GISer_Jing3 小时前
0704-0706上海,又聚上了
前端·新浪微博
止观止3 小时前
深入探索 pnpm:高效磁盘利用与灵活的包管理解决方案
前端·pnpm·前端工程化·包管理器
whale fall3 小时前
npm install安装的node_modules是什么
前端·npm·node.js
烛阴3 小时前
简单入门Python装饰器
前端·python
袁煦丞4 小时前
数据库设计神器DrawDB:cpolar内网穿透实验室第595个成功挑战
前端·程序员·远程工作