Skip to content

Tooltip 文字提示

警告提示,展现需要关注的信息。

代码演示

基础用法

Tooltip will show when mouse enter.
vue
<template>
  <a-tooltip>
    <template #title>prompt text</template>
    Tooltip will show when mouse enter.
  </a-tooltip>
</template>

位置

位置有 12 个方向。

vue
<template>
  <div id="components-a-tooltip-demo-placement">
    <div :style="{ marginLeft: `${buttonWidth}px`, whiteSpace: 'nowrap' }">
      <a-tooltip placement="topLeft">
        <template #title>
          <span>prompt text</span>
        </template>
        <a-button>TL</a-button>
      </a-tooltip>
      <a-tooltip placement="top">
        <template #title>
          <span>prompt text</span>
        </template>
        <a-button>Top</a-button>
      </a-tooltip>
      <a-tooltip placement="topRight">
        <template #title>
          <span>prompt text</span>
        </template>
        <a-button>TR</a-button>
      </a-tooltip>
    </div>
    <div :style="{ width: `${buttonWidth}px`, float: 'left' }">
      <a-tooltip placement="leftTop">
        <template #title>
          <span>prompt text</span>
        </template>
        <a-button>LT</a-button>
      </a-tooltip>
      <a-tooltip placement="left">
        <template #title>
          <span>prompt text</span>
        </template>
        <a-button>Left</a-button>
      </a-tooltip>
      <a-tooltip placement="leftBottom">
        <template #title>
          <span>prompt text</span>
        </template>
        <a-button>LB</a-button>
      </a-tooltip>
    </div>
    <div :style="{ width: `${buttonWidth}px`, marginLeft: `${buttonWidth * 4 + 24}px` }">
      <a-tooltip placement="rightTop">
        <template #title>
          <span>prompt text</span>
        </template>
        <a-button>RT</a-button>
      </a-tooltip>
      <a-tooltip placement="right">
        <template #title>
          <span>prompt text</span>
        </template>
        <a-button>Right</a-button>
      </a-tooltip>
      <a-tooltip placement="rightBottom">
        <template #title>
          <span>prompt text</span>
        </template>
        <a-button>RB</a-button>
      </a-tooltip>
    </div>
    <div :style="{ marginLeft: `${buttonWidth}px`, clear: 'both', whiteSpace: 'nowrap' }">
      <a-tooltip placement="bottomLeft">
        <template #title>
          <span>prompt text</span>
        </template>
        <a-button>BL</a-button>
      </a-tooltip>
      <a-tooltip placement="bottom">
        <template #title>
          <span>prompt text</span>
        </template>
        <a-button>Bottom</a-button>
      </a-tooltip>
      <a-tooltip placement="bottomRight">
        <template #title>
          <span>prompt text</span>
        </template>
        <a-button>BR</a-button>
      </a-tooltip>
    </div>
  </div>
</template>
<script setup>
const buttonWidth = 70;
</script>
<style scoped>
:deep(#components-a-tooltip-demo-placement) .ant-btn {
  width: 70px;
  text-align: center;
  padding: 0;
  margin-right: 8px;
  margin-bottom: 8px;
}
</style>

箭头指向

设置了 arrowPointAtCenter 后,箭头将指向目标元素的中心。

vue
<template>
  <a-space>
    <a-tooltip placement="topLeft" title="Prompt Text">
      <a-button>Align edge / 边缘对齐</a-button>
    </a-tooltip>
    <a-tooltip placement="topLeft" title="Prompt Text" arrow-point-at-center>
      <a-button>Arrow points to center / 箭头指向中心</a-button>
    </a-tooltip>
  </a-space>
</template>

自动调整位置

气泡框不可见时自动调整位置。


vue
<template>
  <div :style="wrapStyles">
    <a-tooltip placement="left" title="Prompt Text" :get-popup-container="getPopupContainer">
      <a-button>Adjust automatically / 自动调整</a-button>
    </a-tooltip>
    <br />
    <a-tooltip
      placement="left"
      title="Prompt Text"
      :get-popup-container="getPopupContainer"
      :auto-adjust-overflow="false"
    >
      <a-button style="margin-top: 10px">Ingore / 不处理</a-button>
    </a-tooltip>
  </div>
</template>
<script setup>
const wrapStyles = {
  overflow: 'hidden',
  position: 'relative',
  padding: '24px',
  border: '1px solid #e9e9e9',
};
const getPopupContainer = trigger => {
  return trigger.parentElement;
};
</script>

多彩文字提示

我们添加了多种预设色彩的文字提示样式,用作不同场景使用。

vue
<template>
  <div id="components-a-tooltip-demo-color">
    <a-divider orientation="left">Presets</a-divider>
    <div>
      <a-tooltip v-for="color in colors" :key="color" title="prompt text" :color="color">
        <a-button>{{ color }}</a-button>
      </a-tooltip>
    </div>
    <a-divider orientation="left">Custom</a-divider>
    <div>
      <a-tooltip v-for="color in customColors" :key="color" title="prompt text" :color="color">
        <a-button>{{ color }}</a-button>
      </a-tooltip>
    </div>
  </div>
</template>
<script setup>
const colors = [
  'pink',
  'red',
  'yellow',
  'orange',
  'cyan',
  'green',
  'blue',
  'purple',
  'geekblue',
  'magenta',
  'volcano',
  'gold',
  'lime',
];
const customColors = ['#f50', '#2db7f5', '#87d068', '#108ee9'];
</script>
<style scoped>
:deep(#components-a-tooltip-demo-color) .ant-btn {
  margin-right: 8px;
  margin-bottom: 8px;
}
</style>

箭头展示

支持显示、隐藏以及将箭头保持居中定位。

vue
<template>
  <div id="components-a-tooltip-demo-arrow">
    <div style="margin-bottom: 24px">
      <a-segmented v-model:value="arrow" :options="options" />
    </div>
    <div :style="{ marginLeft: `${buttonWidth}px`, whiteSpace: 'nowrap' }">
      <a-tooltip placement="topLeft" :arrow="mergedArrow">
        <template #title>
          <span>prompt text</span>
        </template>
        <a-button>TL</a-button>
      </a-tooltip>
      <a-tooltip placement="top" :arrow="mergedArrow">
        <template #title>
          <span>prompt text</span>
        </template>
        <a-button>Top</a-button>
      </a-tooltip>
      <a-tooltip placement="topRight" :arrow="mergedArrow">
        <template #title>
          <span>prompt text</span>
        </template>
        <a-button>TR</a-button>
      </a-tooltip>
    </div>
    <div :style="{ width: `${buttonWidth}px`, float: 'left' }">
      <a-tooltip placement="leftTop" :arrow="mergedArrow">
        <template #title>
          <span>prompt text</span>
        </template>
        <a-button>LT</a-button>
      </a-tooltip>
      <a-tooltip placement="left" :arrow="mergedArrow">
        <template #title>
          <span>prompt text</span>
        </template>
        <a-button>Left</a-button>
      </a-tooltip>
      <a-tooltip placement="leftBottom" :arrow="mergedArrow">
        <template #title>
          <span>prompt text</span>
        </template>
        <a-button>LB</a-button>
      </a-tooltip>
    </div>
    <div :style="{ width: `${buttonWidth}px`, marginLeft: `${buttonWidth * 4 + 24}px` }">
      <a-tooltip placement="rightTop" :arrow="mergedArrow">
        <template #title>
          <span>prompt text</span>
        </template>
        <a-button>RT</a-button>
      </a-tooltip>
      <a-tooltip placement="right" :arrow="mergedArrow">
        <template #title>
          <span>prompt text</span>
        </template>
        <a-button>Right</a-button>
      </a-tooltip>
      <a-tooltip placement="rightBottom" :arrow="mergedArrow">
        <template #title>
          <span>prompt text</span>
        </template>
        <a-button>RB</a-button>
      </a-tooltip>
    </div>
    <div :style="{ marginLeft: `${buttonWidth}px`, clear: 'both', whiteSpace: 'nowrap' }">
      <a-tooltip placement="bottomLeft" :arrow="mergedArrow">
        <template #title>
          <span>prompt text</span>
        </template>
        <a-button>BL</a-button>
      </a-tooltip>
      <a-tooltip placement="bottom" :arrow="mergedArrow">
        <template #title>
          <span>prompt text</span>
        </template>
        <a-button>Bottom</a-button>
      </a-tooltip>
      <a-tooltip placement="bottomRight" :arrow="mergedArrow">
        <template #title>
          <span>prompt text</span>
        </template>
        <a-button>BR</a-button>
      </a-tooltip>
    </div>
  </div>
</template>
<script setup>
import { ref, computed } from 'vue';
const buttonWidth = 70;
const arrow = ref('show');
const options = [
  {
    label: 'Show',
    value: 'show',
  },
  {
    label: 'Hide',
    value: 'hide',
  },
  {
    label: 'Center',
    value: 'center',
  },
];
const mergedArrow = computed(() => {
  switch (arrow.value) {
    case 'show':
      return true;
    case 'hide':
      return false;
    case 'center':
    default:
      return {
        pointAtCenter: true,
      };
  }
});
</script>
<style scoped>
:deep(#components-a-tooltip-demo-arrow) .ant-btn {
  width: 70px;
  text-align: center;
  padding: 0;
  margin-right: 8px;
  margin-bottom: 8px;
}
</style>

API

参数说明类型默认值
title提示文字string | slot-

共同的 API

以下 API 为 Tooltip、Popconfirm、Popover 共享的 API。

参数说明类型默认值
align该值将合并到 placement 的配置中Object-
arrowPointAtCenter箭头是否指向目标元素中心booleanfalse
arrow修改箭头的显示状态以及修改箭头是否指向目标元素中心boolean | { pointAtCenter: boolean }true
autoAdjustOverflow气泡被遮挡时自动调整位置booleantrue
color背景颜色string-
destroyTooltipOnHide隐藏后是否销毁 tooltipbooleanfalse
getPopupContainer浮层渲染父节点,默认渲染到 body 上(triggerNode: HTMLElement) => HTMLElement() => document.body
mouseEnterDelay鼠标移入后延时多少才显示 Tooltip,单位:秒number0.1
mouseLeaveDelay鼠标移出后延时多少才隐藏 Tooltip,单位:秒number0.1
overlayClassName卡片类名string-
overlayStyle卡片样式object-
overlayInnerStyle卡片内容区域样式object-
placement气泡框位置,可选 top left right bottom topLeft topRight bottomLeft bottomRight leftTop leftBottom rightTop rightBottomstringtop
trigger触发行为,可选 hover/focus/click/contextmenustringhover
open(v-model)用于手动控制浮层显隐, 小于 4.0.0 使用 visiblebooleanfalse

事件

事件名称说明回调参数
openChange显示隐藏的回调(visible) => void