概览

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

The animation effect of this component is dependent on the prefers-reduced-motion media query. See the reduced motion section of our accessibility documentation.

知道了吗? 太好了,让我们看看它们是如何工作的。

示例:随处启用工具提示

初始化页面上所有工具提示的一种方法是通过其data-toggle属性选择它们:

$(function () {
  $('[data-toggle="tooltip"]').tooltip()
})

例子

将鼠标悬停在下面的链接上可以查看工具提示:

Tight pants next level keffiyeh you probably haven't heard of them. Photo booth beard raw denim letterpress vegan messenger bag stumptown. Farm-to-table seitan, mcsweeney's fixie sustainable quinoa 8-bit american apparel have a terry richardson vinyl chambray. Beard stumptown, cardigans banh mi lomo thundercats. Tofu biodiesel williamsburg marfa, four loko mcsweeney's cleanse vegan chambray. A really ironic artisan whatever keytar, scenester farm-to-table banksy Austin twitter handle freegan cred raw denim single-origin coffee viral.

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

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

并添加了自定义HTML:

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

用法

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

通过JavaScript触发工具提示:

$('#example').tooltip(options)
自动和滚动溢出

当父容器具有“溢位:自动”或“溢位:滚动”时,工具提示位置会尝试自动更改,就像我们的.table-sensitive一样,但仍保持原始展示位置。 要解决此问题,请将“ boundary”选项设置为默认值“ scrollParent”以外的其他值,例如“ window”。

$('#example').tooltip({ boundary: 'window' })

标记

工具提示所需的标记只是您希望拥有工具提示的HTML元素上的data属性和title。 工具提示的生成标记相当简单,尽管它确实需要位置(默认情况下,插件将其设置为“ top”)。

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

您只应向传统上可键盘聚焦和交互的HTML元素添加工具提示(例如链接或表单控件)。 尽管可以通过添加tabindex =“ 0”属性使任意HTML元素(例如`s)成为可聚焦的,但这将为键盘用户和大多数用户在非交互式元素上添加潜在的令人讨厌的和令人困惑的制表位。 在这种情况下,辅助技术目前尚未公布工具提示。 此外,不要仅仅依靠“ hover”作为工具提示的触发器,因为这将使您的工具提示无法为键盘用户触发。

<!-- HTML to write -->
<a href="#" data-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="arrow"></div>
  <div class="tooltip-inner">
    Some tooltip text!
  </div>
</div>

禁用元素

具有“ disabled”属性的元素不是交互式的,这意味着用户无法聚焦,悬停或单击它们来触发工具提示(或弹出窗口)。 作为一种解决方法,您将希望从包装器<div>或中触发工具提示,最好使用tabindex =“ 0”将其设置为键盘焦点,并覆盖上的pointer-events 禁用的元素。

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

选项(Options)

可以通过数据属性或JavaScript传递选项。 对于数据属性,将选项名称附加到data-中,如`data-animation =“”中所示。

Note that for security reasons the sanitize, sanitizeFn and whiteList options cannot be supplied using data attributes.

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.

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, jQuery's text method 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="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.

.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.js's offset docs.

fallbackPlacement string | array 'flip' Allow to specify which position Popper will use on fallback. For more information refer to Popper.js'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.js'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.js config, see Popper.js's configuration

各个工具提示的数据属性

如上所述,可以单独使用数据属性来指定各个工具提示的选项。

方法(Methods)

Asynchronous methods and transitions

All API methods are asynchronous and start a transition. They return to the caller as soon as the transition is started but before it ends. In addition, a method call on a transitioning component will be ignored.

See our JavaScript documentation for more information.

$().tooltip(options)

将工具提示处理程序附加到元素集合。

.tooltip('show')

显示元素的工具提示。 在实际显示工具提示之前返回调用者(即在shown.bs.tooltip事件发生之前)。 这被认为是工具提示的“手动”触发。 标题长度为零的工具提示永远不会显示。

$('#element').tooltip('show')

.tooltip('hide')

隐藏元素的工具提示。 在工具提示实际上被隐藏之前返回调用者(即在“ hidden.bs.tooltip”事件发生之前)。 这被认为是工具提示的“手动”触发。

$('#element').tooltip('hide')

.tooltip('toggle')

切换元素的工具提示。 在工具提示实际显示或隐藏之前返回调用者(即在“ shown.bs.tooltip”或“ hidden.bs.tooltip”事件发生之前)。 这被认为是工具提示的“手动”触发。

$('#element').tooltip('toggle')

.tooltip('dispose')

隐藏和销毁元素的工具提示。 使用委派的工具提示(使用[selector选项](#options)创建)不能在后代触发器元素上单独销毁。

$('#element').tooltip('dispose')

.tooltip('enable')

使元素的工具提示能够显示。 默认情况下启用工具提示。

$('#element').tooltip('enable')

.tooltip('disable')

删除显示元素工具提示的功能。 只有重新启用后,才能显示工具提示。

$('#element').tooltip('disable')

.tooltip('toggleEnabled')

切换显示或隐藏元素工具提示的功能。

$('#element').tooltip('toggleEnabled')

.tooltip('update')

更新元素工具提示的位置。

$('#element').tooltip('update')

事件(Events)

Event Type Description
show.bs.tooltip This event fires immediately when the show instance method is called.
shown.bs.tooltip This event is fired when the tooltip has been made visible to the user (will wait for CSS transitions to complete).
hide.bs.tooltip This event is fired immediately when the hide instance method has been called.
hidden.bs.tooltip This event is fired when the tooltip has finished being hidden from the user (will wait for CSS transitions to complete).
inserted.bs.tooltip This event is fired after the show.bs.tooltip event when the tooltip template has been added to the DOM.
$('#myTooltip').on('hidden.bs.tooltip', function () {
  // do something...
})