vue3 antd3.x ant-table组件 鼠标移入行出现tooltip

效果

代码

html 复制代码
<template>
  <a-table :columns="columns" :data-source="data" :customRow="onClickRow" ref="tableRef" :scroll="{ x: 1500 }" />

  <!-- Tooltip组件 -->
  <a-tooltip
    v-model:visible="tooltipVisible"
    placement="bottom"
    trigger="manual"
    :overlay-style="{
      position: 'absolute',
      pointerEvents: 'none',
      zIndex: 9999,
      top: `${tooltipStyle.top} !important`,
      left: `${tooltipStyle.left} !important`
    }"
  >
    <template #title>
      <div>
        <p>Additional Info: {{ tipInfo }}</p>
      </div>
    </template>
    <!-- 隐藏触发元素:使其不占据任何位置,避免作为锚点 -->
    <span style="position: fixed; top: -9999px; left: -9999px; width: 0; height: 0"></span>
  </a-tooltip>
</template>

<script setup>
  import { ref, reactive } from 'vue';
  // 表格DOM引用
  const tableRef = ref(null);

  const tipInfo = ref(''); // 提示信息
  const tooltipVisible = ref(false); // tooltip的显示与隐藏
  const tooltipStyle = reactive({ left: '0px', top: '0px' }); // tooltip的位置样式

  // 模拟数据
  const data = reactive([
    { key: '1', name: '张三', age: 32 },
    { key: '2', name: '李四', age: 42 },
    { key: '3', name: '王五', age: 32 },
    { key: '4', name: '赵六', age: 32 }
  ]);

  // 列定义
  const columns = [
    { title: 'Name', dataIndex: 'name', key: 'name' },
    { title: 'Age', dataIndex: 'age', key: 'age' }
  ];

  onMounted(() => {
    // 绑定表格的鼠标移出mouseleave事件
    if (tableRef.value) {
      const tableDom = tableRef.value.$el || tableRef.value;
      tableDom.addEventListener('mouseleave', onTableMouseLeave);
    }
  });

  onUnmounted(() => {
    // 取消表格的鼠标移出mouseleave事件
    if (tableRef.value) {
      const tableDom = tableRef.value.$el || tableRef.value;
      tableDom.removeEventListener('mouseleave', onTableMouseLeave);
    }
  });
  // 行点击
  const onClickRow = record => {
    return {
      onMouseenter: event => handleRowMouseEnter(event, record) // 鼠标移入行
    };
  };
  // 鼠标进入行
  const handleRowMouseEnter = (e, record) => {
    // 计算正确位置(行视口位置 + 滚动偏移)
    const rect = e.currentTarget.getBoundingClientRect();
    const scrollTop = window.pageYOffset || document.documentElement.scrollTop;
    const scrollLeft = window.pageXOffset || document.documentElement.scrollLeft;
    // 滚动偏移 (横向偏移)
    tooltipStyle.left = `${scrollLeft + 120}px`;
    // 行的底部 + 滚动偏移 (纵向偏移)
    tooltipStyle.top = `${rect.bottom + scrollTop}px`;
    // 设置提示信息
    tipInfo.value = `Hovered row: ${record.name}`;
    nextTick(() => {
      tooltipVisible.value = true;
    });
  };

  // 鼠标离开表格
  const onTableMouseLeave = () => {
    // console.log('Leave');
    tooltipVisible.value = false;
  };
</script>
相关推荐
华玥作者19 小时前
[特殊字符] VitePress 对接 Algolia AI 问答(DocSearch + AI Search)完整实战(下)
前端·人工智能·ai
Mr Xu_20 小时前
告别冗长 switch-case:Vue 项目中基于映射表的优雅路由数据匹配方案
前端·javascript·vue.js
前端摸鱼匠20 小时前
Vue 3 的toRefs保持响应性:讲解toRefs在解构响应式对象时的作用
前端·javascript·vue.js·前端框架·ecmascript
lang2015092820 小时前
JSR-340 :高性能Web开发新标准
java·前端·servlet
好家伙VCC21 小时前
### WebRTC技术:实时通信的革新与实现####webRTC(Web Real-TimeComm
java·前端·python·webrtc
未来之窗软件服务21 小时前
未来之窗昭和仙君(六十五)Vue与跨地区多部门开发—东方仙盟练气
前端·javascript·vue.js·仙盟创梦ide·东方仙盟·昭和仙君
嘿起屁儿整1 天前
面试点(网络层面)
前端·网络
VT.馒头1 天前
【力扣】2721. 并行执行异步函数
前端·javascript·算法·leetcode·typescript
phltxy1 天前
Vue 核心特性实战指南:指令、样式绑定、计算属性与侦听器
前端·javascript·vue.js
Byron07071 天前
Vue 中使用 Tiptap 富文本编辑器的完整指南
前端·javascript·vue.js