工具提示

工具提示-Bootstrap 5和Material Design 2.0组件

使用CSS3在CSS和JavaScript中添加自定义工具提示的文档和示例 用于本地标题存储的动画和data-mdb-attributes。

注意:阅读 API 标签以找到所有可用选项,然后 高级定制


基本例子

Hover the link to see the tooltip


          <p>
            Hover the link to see the
            <a href="#" data-mdb-toggle="tooltip" title="Hi! I'm tooltip">tooltip</a>
          </p>
        

总览

使用工具提示插件时要了解的事项:

  • 出于性能原因选择使用工具提示,因此 您必须自己对其进行初始化
  • 标题长度为零的工具提示永远不会显示。
  • 指定 container: 'body' 避免更复杂的渲染问题 组件(例如我们的输入组,按钮组等)。
  • 在隐藏元素上触发工具提示将不起作用。
  • .disabled disabled 元素的工具提示必须在 包装器元素。
  • 从跨越多行的超链接触发时,工具提示将居中。采用 您的 white-space:nowrap; <a>避免这种行为。
  • 在从DOM中删除其相应元素之前,必须先隐藏工具提示。
  • 借助影子DOM中的元素,可以触发工具提示。

四个方向

将鼠标悬停在下面的按钮上可以查看四个工具提示的方向:顶部,右侧,底部和 剩下。


          <button
            type="button"
            class="btn btn-secondary"
            data-mdb-toggle="tooltip"
            data-mdb-placement="top"
            title="Tooltip on top"
          >
            Tooltip on top
          </button>
          <button
            type="button"
            class="btn btn-secondary"
            data-mdb-toggle="tooltip"
            data-mdb-placement="right"
            title="Tooltip on right"
          >
            Tooltip on right
          </button>
          <button
            type="button"
            class="btn btn-secondary"
            data-mdb-toggle="tooltip"
            data-mdb-placement="bottom"
            title="Tooltip on bottom"
          >
            Tooltip on bottom
          </button>
          <button
            type="button"
            class="btn btn-secondary"
            data-mdb-toggle="tooltip"
            data-mdb-placement="left"
            title="Tooltip on left"
          >
            Tooltip on left
          </button>
        

并添加了自定义HTML:


          <button
            type="button"
            class="btn btn-secondary"
            data-mdb-toggle="tooltip"
            data-mdb-html="true"
            title="<em>Tooltip</em> <u>with</u> <b>HTML</b>"
          >
            Tooltip with HTML
          </button>
        

禁用元素

具有 disabled 属性的元素不是交互式的,这意味着用户不能 聚焦,悬停或单击它们以触发工具提示(或弹出窗口)。解决方法是,您需要 从包装器触发工具提示 <div>要么 <span>, 理想的使用键盘聚焦 tabindex="0", 并覆盖 pointer-events 在禁用元素上。


          <span
            class="d-inline-block"
            tabindex="0"
            data-mdb-toggle="tooltip"
            title="Disabled tooltip"
          >
            <button class="btn btn-primary" style="pointer-events: none" type="button" disabled>
              Disabled button
            </button>
          </span>
        

工具提示 - API


用法

工具提示插件可按需生成内容和标记,默认情况下会放置工具提示 在其触发元素之后。

Via JavaScript:


          const exampleEl = document.getElementById('example')
          const tooltip = new mdb.Tooltip(exampleEl)
        

溢出 auto scroll

当父容器具有以下内容时,工具提示位置会尝试自动更改 溢出:自动溢出:滚动 .table-sensitive ,但仍保持原始展示位置的位置。至 解决,将 boundary 选项设置为默认值以外的任何值, 'scrollParent',例如 'window':


          const exampleEl = document.getElementById('example')
          const tooltip = new bootstrap.Tooltip(exampleEl, {
            boundary: 'window'
          })
        

标记

工具提示所需的标记只是一个 data 属性,并且 您希望获得工具提示的HTML元素上的 title 。生成的标记 工具提示非常简单,尽管它确实需要位置(默认情况下,设置为 插件的 top )。

使工具提示对键盘和辅助技术用户有效

您只应向传统上以键盘为焦点的HTML元素添加工具提示, 互动式(例如链接或表单控件)。尽管任意HTML元素(例如 <span>s) 可以通过添加 tabindex="0" 属性,这将添加潜在的令人讨厌和混乱的标签 停止针对键盘用户的非交互式元素和大多数辅助技术 目前在这种情况下不会宣布工具提示。此外,不要仅仅依靠 hover 作为工具提示的触发器,因为这将使您的工具提示无法使用 触发键盘用户。


          <!-- HTML to write -->
          <a href="#" data-mdb-toggle="tooltip" title="Some tooltip text!">Hover over me</a>

          <!-- Generated markup by the plugin -->
          <div class="tooltip bs-tooltip-top" role="tooltip">
            <div class="tooltip-arrow"></div>
            <div class="tooltip-inner">Some tooltip text!</div>
          </div>
        

选件

可以通过数据属性或JavaScript传递选项。对于数据属性,请附加 选项名称为 data-mdb-,如data-mdb-animation="".

注意:出于安全原因,消毒, 无法使用数据提供 sanitizeFn whiteList 选项 属性。

Name Type Default Description
animation boolean true Apply a CSS fade transition to the tooltip
container string | element | false false

Appends the tooltip to a specific element. Example: container: 'body'. This option is particularly useful in that it allows you to position the tooltip in the flow of the document near the triggering element - which will prevent the tooltip from floating away from the triggering element during a window resize.

customClass string | function ''

Add classes to the tooltip when it is shown. Note that these classes will be added in addition to any classes specified in the template. To add multiple classes, separate them with spaces: 'class-1 class-2'.

You can also pass a function that should return a single string containing additional class names.

delay number | object 0

Delay showing and hiding the tooltip (ms) - does not apply to manual trigger type

If a number is supplied, delay is applied to both hide/show

Object structure is: delay: { "show": 500, "hide": 100 }

html boolean false

Allow HTML in the tooltip.

If true, HTML tags in the tooltip's title will be rendered in the tooltip. If false, innerText property will be used to insert content into the DOM.

Use text if you're worried about XSS attacks.

placement string | function 'top'

How to position the tooltip - auto | top | bottom | left | right.
When auto is specified, it will dynamically reorient the tooltip.

When a function is used to determine the placement, it is called with the tooltip DOM node as its first argument and the triggering element DOM node as its second. The this context is set to the tooltip instance.

selector string | false false If a selector is provided, tooltip objects will be delegated to the specified targets. In practice, this is used to also apply tooltips to dynamically added DOM elements (jQuery.on support). See this and an informative example.
template string '<div class="tooltip" role="tooltip"><div class="tooltip-arrow"></div><div class="tooltip-inner"></div></div>'

Base HTML to use when creating the tooltip.

The tooltip's title will be injected into the .tooltip-inner.

.tooltip-arrow will become the tooltip's arrow.

The outermost wrapper element should have the .tooltip class and role="tooltip".

title string | element | function ''

Default title value if title attribute isn't present.

If a function is given, it will be called with its this reference set to the element that the tooltip is attached to.

trigger string 'hover focus'

How tooltip is triggered - click | hover | focus | manual. You may pass multiple triggers; separate them with a space.

'manual' indicates that the tooltip will be triggered programmatically via the .tooltip('show'), .tooltip('hide') and .tooltip('toggle') methods; this value cannot be combined with any other trigger.

'hover' on its own will result in tooltips that cannot be triggered via the keyboard, and should only be used if alternative methods for conveying the same information for keyboard users is present.

offset number | string | function 0

Offset of the tooltip relative to its target.

When a function is used to determine the offset, it is called with an object containing the offset data as its first argument. The function must return an object with the same structure. The triggering element DOM node is passed as the second argument.

For more information refer to Popper's offset docs.

fallbackPlacement string | array 'flip' Allow to specify which position Popper will use on fallback. For more information refer to Popper's behavior docs
boundary string | element 'scrollParent' Overflow constraint boundary of the tooltip. Accepts the values of 'viewport', 'window', 'scrollParent', or an HTMLElement reference (JavaScript only). For more information refer to Popper's preventOverflow docs.
sanitize boolean true Enable or disable the sanitization. If activated 'template' and 'title' options will be sanitized.
whiteList object Default value Object which contains allowed attributes and tags
sanitizeFn null | function null Here you can supply your own sanitize function. This can be useful if you prefer to use a dedicated library to perform sanitization.
popperConfig null | object null To change Bootstrap's default Popper config, see Popper's configuration

各个工具提示的数据属性:
Options for individual tooltips can alternatively be specified through the use of data attributes, as explained above.


方法

异步方法和转换:
所有API方法都是异步并开始过渡。 转换开始后,他们会立即返回给呼叫者,但 在结束之前。另外,在 转换组件将被忽略

show Reveals an element’s tooltip. Returns to the caller before the tooltip has actually been shown (i.e. before the shown.mdb.tooltip event occurs). This is considered a “manual” triggering of the tooltip. tooltips whose title and content are both zero-length are never displayed.
hide Hides an element’s tooltip. Returns to the caller before the tooltip has actually been hidden (i.e. before the hidden.mdb.tooltip event occurs). This is considered a “manual” triggering of the tooltip.
toggle Toggles an element’s tooltip. Returns to the caller before the tooltip has actually been shown or hidden (i.e. before the shown.mdb.tooltip or hidden.mdb.tooltip event occurs). This is considered a “manual” triggering of the tooltip.
dispose Hides and destroys an element’s tooltip. tooltips that use delegation (which are created using the selector option) cannot be individually destroyed on descendant trigger elements.
enable Gives an element’s tooltip the ability to be shown. tooltips are enabled by default.
disable Removes the ability for an element’s tooltip to be shown. The tooltip will only be able to be shown if it is re-enabled.
toggleEnabled Toggles the ability for an element’s tooltip to be shown or hidden.
update Updates the position of an element’s tooltip.

        const tooltipEl = document.getElementById('tooltip')
        const tooltip = new mdb.Tooltip(tooltipEl)
        tooltip.show()
      

大事记

Event type Description
show.mdb.tooltip This event fires immediately when the show instance method is called.
shown.mdb.tooltip This event is fired when the tooltip has been made visible to the user (will wait for CSS transitions to complete).
hide.mdb.tooltip This event is fired immediately when the hide instance method has been called.
hidden.mdb.tooltip This event is fired when the tooltip has finished being hidden from the user (will wait for CSS transitions to complete).
inserted.mdb.tooltip This event is fired after the show.mdb.tooltip event when the tooltip template has been added to the DOM.

          const tooltipEl = document.getElementById('tooltip')
          const tooltip = new mdb.Tooltip(tooltipEl)

          tooltipEl.addEventListener('hidden.mdb.tooltip', () => {
            // do something...
          })

          tooltip.hide()
        

进口

MDB UI KIT也可以与模块捆绑器一起使用。使用以下代码导入此组件:


          import { Tooltip } from 'mdb-ui-kit';