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>
相关推荐
摸鱼老王不带83 分钟前
不用后端,纯前端读出"网站眼里你的真实出口 IP"——聊聊 Cloudflare 的 cdn-cgi/trace
前端
程序员黑豆13 分钟前
鸿蒙开发入门:Row 和 Column 布局组件详解
前端·harmonyos
hoLzwEge36 分钟前
前端环境变量配置指南:.env 文件详解
前端·前端框架
hunterandroid1 小时前
Paging 3 RemoteMediator 实战:构建离线优先的分页列表
android·前端
Underwood_171 小时前
星级评价——了解useState
前端
hunterandroid1 小时前
[鸿蒙从零到一] ArkUI 组件化实战:构建可复用、可组合的自定义组件
前端·华为·架构
一只公羊1 小时前
iPhone摄像头 在开发/调试过程中强行停止 App,导致 `AVCaptureSession` 没有被正常释放
前端
活于未来1 小时前
从零搭建期权波动率曲面可视化平台:FastAPI + Vue 3 实战
vue.js
橘子海全栈攻城狮1 小时前
【最新源码】基于SpringBoot + Vue的超市管理系统的设计与实现D002
java·开发语言·vue.js·spring boot·后端·spring
浮江雾2 小时前
Flutter第十节-----Flutter布局与组件全解析
android·开发语言·前端·学习·flutter·入门