Skip to content

Tooltip 文字提示 ​

常用于展示鼠标 hover 时的提示信息。

引入

html
<script type="module">
  import "./node_modules/easy-component-ui/dist/components/ea-tooltip.js";
</script>
js
import "easy-component-ui/ea-tooltip";

自定义样式

移步到 CSS PartCSS Custom Properties

查看代码
css
ea-tooltip {
  margin: 1rem;
}

基础用法

在这里我们提供 9 种不同方向的展示方式,可以通过以下完整示例来理解,选择你要的效果。

使用 content 属性来设置悬停时显示的信息。 由 placement 属性决定 Popover 弹出框的位置。 该属性值格式为:[方向]-[对齐位置],可供选择的四个方向分别是 topleftrightbottom,可供选择的三种对齐方式分别是 start、end、null,默认的对齐方式为 null。 以 placement="left-end" 为例,Popover 弹出框会显示在悬停元素的左侧,且提示信息的底部与悬停元素的底部对齐。

top-start top toptop-endleft-startright-startleftrightleft-endright-endbottom-startbottombottom-end
查看代码
html
<div class="demo">
  <ea-row justify="center">
    <ea-col span="6">
      <ea-tooltip content="Top Left prompts info" placement="top-start">
        <ea-button type="primary" slot="reference">top-start</ea-button>
      </ea-tooltip>
    </ea-col>
    <ea-col span="6">
      <ea-tooltip content="Top Center prompts info" placement="top">
        top
        <ea-button type="primary" slot="reference">top</ea-button>
      </ea-tooltip>
    </ea-col>
    <ea-col span="6">
      <ea-tooltip content="Top Right prompts info" placement="top-end">
        <ea-button type="primary" slot="reference">top-end</ea-button>
      </ea-tooltip>
    </ea-col>
  </ea-row>
  <ea-row justify="space-between">
    <ea-col span="6">
      <ea-tooltip content="Left Top prompts info" placement="left-start">
        <ea-button type="primary" slot="reference">left-start</ea-button>
      </ea-tooltip>
    </ea-col>
    <ea-col span="6">
      <ea-tooltip content="Right Top prompts info" placement="right-start">
        <ea-button type="primary" slot="reference">right-start</ea-button>
      </ea-tooltip>
    </ea-col>
  </ea-row>
  <ea-row justify="space-between">
    <ea-col span="6">
      <ea-tooltip content="Left Center prompts info" placement="left">
        <ea-button type="primary" slot="reference">left</ea-button>
      </ea-tooltip>
    </ea-col>
    <ea-col span="6">
      <ea-tooltip content="Right Center prompts info" placement="right">
        <ea-button type="primary" slot="reference">right</ea-button>
      </ea-tooltip>
    </ea-col>
  </ea-row>
  <ea-row justify="space-between">
    <ea-col span="6">
      <ea-tooltip content="Left Bottom prompts info" placement="left-end">
        <ea-button type="primary" slot="reference">left-end</ea-button>
      </ea-tooltip>
    </ea-col>
    <ea-col span="6">
      <ea-tooltip content="Right Bottom prompts info" placement="right-end">
        <ea-button type="primary" slot="reference">right-end</ea-button>
      </ea-tooltip>
    </ea-col>
  </ea-row>
  <ea-row justify="center">
    <ea-col span="6">
      <ea-tooltip content="Bottom Left prompts info" placement="bottom-start">
        <ea-button type="primary" slot="reference">bottom-start</ea-button>
      </ea-tooltip>
    </ea-col>
    <ea-col span="6">
      <ea-tooltip content="Bottom Center prompts info" placement="bottom">
        <ea-button type="primary" slot="reference">bottom</ea-button>
      </ea-tooltip>
    </ea-col>
    <ea-col span="6">
      <ea-tooltip content="Bottom Right prompts info" placement="bottom-end">
        <ea-button type="primary" slot="reference">bottom-end</ea-button>
      </ea-tooltip>
    </ea-col>
  </ea-row>
</div>

主题 ​

Tooltip 组件内置了两个主题:darklight

DarkLightCustomized theme
查看代码
css
ea-tooltip[effect="customized"]::part(original) {
  background: linear-gradient(90deg, rgb(159, 229, 151), rgb(204, 229, 129));
}

ea-tooltip[effect="customized"]::part(original)::after {
  background: linear-gradient(45deg, #b2e68d, #bce689);
}
html
<div class="demo">
  <ea-tooltip content="Top center" placement="top">
    <ea-button slot="reference">Dark</ea-button>
  </ea-tooltip>
  <ea-tooltip content="Bottom center" placement="bottom" effect="light">
    <ea-button slot="reference">Light</ea-button>
  </ea-tooltip>
  <ea-tooltip content="Bottom center" effect="customized">
    <ea-button slot="reference">Customized theme</ea-button>
  </ea-tooltip>
</div>

更多内容的文字提示 ​

展示多行文本或者是设置文本内容的格式

用默认 slot 替代 content 属性,支持 HTML 格式内容。

multiple lines
second line Top center
查看代码
html
<div id="scalableSection" class="demo">
  <ea-tooltip placement="top">
    multiple lines<br />second line
    <ea-button id="popoverBtn" type="primary" slot="reference"
      >Top center</ea-button
    >
  </ea-tooltip>
</div>

Tooltip API

Tooltip Attributes

参数说明类型可选值默认值
effect显示效果。Stringdark / light / customizeddark
trigger触发方式。Stringclick / focus / hover / contextmenu / customizedhover
content显示的内容,也可以通过写入默认 slot 修改显示内容String
width宽度,单位 px。Number150
placement气泡的出现位置。Stringtop / top-start / top-end / bottom / bottom-start / bottom-end / left / left-start / left-end / right / right-start / right-endtop
show-arrow是否显示箭头Booleantrue
visible控制 Tooltip 显隐的属性Booleanfalse
offset气泡出现的位置偏移量。String"0 0"
flip是否在超过原 placement 视口时,进行翻转。Booleantrue

Tooltip CSS Part

名称说明
containerTooltip 外层容器
reference触发 Tooltip 显示的 HTML 元素的父容器
originalTooltip 弹出内容容器
contentTooltip 文本内容容器

Tooltip Events

事件名说明回调参数(event.detail)
ea-show开启 Tooltip 时触发的事件
ea-shown开启 Tooltip 的动画结束时触发
ea-hide关闭 Tooltip 时触发的事件
ea-hidden关闭 Tooltip 的动画结束时触发

Tooltip Methods

方法名说明参数
show显示 Tooltip
hide隐藏 Tooltip
toggle切换 Tooltip 显示状态

Tooltip Slots

名称描述
defaultTooltip 内容插槽
reference触发 Tooltip 显示的 HTML 元素插槽

Tooltip CSS Custom Properties

属性名说明默认值
--ea-tooltip-spacing内边距5px 11px
--ea-tooltip-bg-color-dark暗色主题背景颜色var(--grey-900)
--ea-tooltip-color-dark暗色主题文字颜色var(--color-white)
--ea-tooltip-bg-color-light亮色主题背景颜色var(--color-white)
--ea-tooltip-color-light亮色主题文字颜色var(--grey-900)
--ea-tooltip-font-size字体大小var(--font-size-sm)
--ea-tooltip-border-radius圆角var(--border-radius-sm)
--ea-tooltip-z-index层级100