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>
相关推荐
六月June June20 小时前
自定义调色盘组件
前端·javascript·调色盘
SY_FC20 小时前
实现一个父组件引入了子组件,跳转到其他页面,其他页面返回回来重新加载子组件函数
java·前端·javascript
糟糕好吃20 小时前
我让 AI 操作网页之后,开始不想点按钮了
前端·javascript·后端
陈天伟教授20 小时前
人工智能应用- 天文学家的助手:08. 星系定位与分类
前端·javascript·数据库·人工智能·机器学习
VaJoy20 小时前
给到夯!前端工具链新标杆 Vite Plus 初探
前端·vite
小彭努力中1 天前
191.Vue3 + OpenLayers 实战:可控化版权信息(Attribution)详解与完整示例
前端·javascript·vue.js·#地图开发·#cesium
奇舞精选1 天前
用去年 github 最火的 n8n 快速实现自动化推送工具
前端·agent
奇舞精选1 天前
实践:如何为智能体推理引入外部决策步骤
前端·agent
无限大61 天前
AI实战02:一个万能提示词模板,搞定90%的文案/设计/分析需求
前端·后端
朝阳5811 天前
控制 Nuxt 页面的渲染模式:客户端 vs 服务端渲染
前端·javascript