Tooltips工具提示
Documentation and examples for adding custom Bootstrap tooltips with CSS and JavaScript using CSS3 for animations and data-bs-attributes for local title storage.使用CSS和JavaScript添加自定义Bootstrap工具提示的文档和示例,使用CSS3为动画和数据bs属性为本地标题存储。
Overview概述
Things to know when using the tooltip plugin:使用工具提示插件时需要注意的事项:
Tooltips rely on the 3rd party library Popper for positioning.工具提示依赖于第三方库Popper进行定位。You must include popper.min.js before bootstrap.js or use您必须在bootstrap.js之前包含popper.min.js,或者使用包含Popper的bootstrap.bundle.min.js
/bootstrap.bundle.js
which contains Popper in order for tooltips to work!bootstrap.bundle.min.js
/bootstrap.bundle.js
才能使工具提示工作!Tooltips are opt-in for performance reasons, so you must initialize them yourself.出于性能原因,工具提示是可选的,因此您必须自己初始化它们。Tooltips with zero-length titles are never displayed.不显示标题长度为零的工具提示。Specify指定container: 'body'
to avoid rendering problems in more complex components (like our input groups, button groups, etc).container: 'body'
以避免在更复杂的组件(如输入组、按钮组等)中呈现问题。Triggering tooltips on hidden elements will not work.在隐藏元素上触发工具提示将不起作用。Tooltips for必须在包装元素上触发.disabled
ordisabled
elements must be triggered on a wrapper element..disabled
或disabled
元素的工具提示。When triggered from hyperlinks that span multiple lines, tooltips will be centered.当从跨多行的超链接触发时,工具提示将居中。Use在white-space: nowrap;
on your<a>
s to avoid this behavior.<a>
上使用white-space: nowrap;
以避免这种行为。Tooltips must be hidden before their corresponding elements have been removed from the DOM.在从DOM中删除相应的元素之前,必须隐藏工具提示。Tooltips can be triggered thanks to an element inside a shadow DOM.由于阴影DOM中的元素,可以触发工具提示。
prefers-reduced-motion
media query. prefers-reduced-motion
媒体查询。Got all that? Great, let’s see how they work with some examples.明白了吗?太好了,让我们看看他们是如何使用一些示例的。
Example: Enable tooltips everywhere示例:到处启用工具提示
One way to initialize all tooltips on a page would be to select them by their 初始化页面上所有工具提示的一种方法是通过其data-bs-toggle
attribute:data-bs-toggle
属性选择它们:
var tooltipTriggerList = [].slice.call(document.querySelectorAll('[data-bs-toggle="tooltip"]'))
var tooltipList = tooltipTriggerList.map(function (tooltipTriggerEl) {
return new bootstrap.Tooltip(tooltipTriggerEl)
})
Examples示例
Hover over the links below to see tooltips:将鼠标悬停在下面的链接上以查看工具提示:
Placeholder text to demonstrate some inline links with tooltips. 占位符文本,用于演示带有工具提示的一些内联链接。This is now just filler, no killer. 这只是填充物,没有杀手。Content placed here just to mimic the presence of real text. 放置在此处的内容只是为了模拟真实文本的存在。And all that just to give you an idea of how tooltips would look when used in real-world situations. 所有这些都只是为了让您了解工具提示在现实世界中使用时的外观。So hopefully you've now seen how these tooltips on links can work in practice, once you use them on your own site or project.因此,希望您现在已经看到了链接上的这些工具提示在实践中是如何工作的,一旦您在自己的网站或项目上使用它们。
Hover over the buttons below to see the four tooltips directions: top, right, bottom, and left. Directions are mirrored when using Bootstrap in RTL.将鼠标悬停在下面的按钮上可查看四个工具提示方向:顶部、右侧、底部和左侧。在RTL中使用Bootstrap时,方向是镜像的。
<button type="button" class="btn btn-secondary" data-bs-toggle="tooltip" data-bs-placement="top" title="Tooltip on top">
Tooltip on top
</button>
<button type="button" class="btn btn-secondary" data-bs-toggle="tooltip" data-bs-placement="right" title="Tooltip on right">
Tooltip on right
</button>
<button type="button" class="btn btn-secondary" data-bs-toggle="tooltip" data-bs-placement="bottom" title="Tooltip on bottom">
Tooltip on bottom
</button>
<button type="button" class="btn btn-secondary" data-bs-toggle="tooltip" data-bs-placement="left" title="Tooltip on left">
Tooltip on left
</button>
And with custom HTML added:并且添加了自定义HTML:
<button type="button" class="btn btn-secondary" data-bs-toggle="tooltip" data-bs-html="true" title="<em>Tooltip</em> <u>with</u> <b>HTML</b>">
Tooltip with HTML
</button>
With an SVG:使用SVG:
Sass
Variables变量
$tooltip-font-size: $font-size-sm;
$tooltip-max-width: 200px;
$tooltip-color: $white;
$tooltip-bg: $black;
$tooltip-border-radius: $border-radius;
$tooltip-opacity: .9;
$tooltip-padding-y: $spacer * .25;
$tooltip-padding-x: $spacer * .5;
$tooltip-margin: 0;
$tooltip-arrow-width: .8rem;
$tooltip-arrow-height: .4rem;
$tooltip-arrow-color: $tooltip-bg;
Usage用法
The tooltip plugin generates content and markup on demand, and by default places tooltips after their trigger element.工具提示插件按需生成内容和标记,默认情况下,将工具提示放置在触发器元素之后。
Trigger the tooltip via JavaScript:通过JavaScript触发工具提示:
var exampleEl = document.getElementById('example')
var tooltip = new bootstrap.Tooltip(exampleEl, options)
Overflow auto
and scroll
溢出auto
和scroll
auto
and scroll
Tooltip position attempts to automatically change when a parent container has 当父容器具有overflow: auto
or overflow: scroll
like our .table-responsive
, but still keeps the original placement’s positioning. overflow: auto
或overflow: scroll
时,工具提示位置尝试自动更改,就像.table-responsive
那样,但仍保持原始位置的位置。To resolve this, set the 要解决此问题,请将boundary
option (for the flip modifier using the popperConfig
option) to any HTMLElement to override the default value, 'clippingParents'
, such as document.body
:boundary
选项(对于使用popperConfig
选项的翻转修饰符)设置为任何HTMLElement,以覆盖默认值'clippingParents'
,如document.body
:
var exampleEl = document.getElementById('example')
var tooltip = new bootstrap.Tooltip(exampleEl, {
boundary: document.body // or document.querySelector('#boundary')
})
Markup
The required markup for a tooltip is only a 工具提示所需的标记仅是希望具有工具提示的HTML元素上的data
attribute and title
on the HTML element you wish to have a tooltip. data
属性和title
。The generated markup of a tooltip is rather simple, though it does require a position (by default, set to 生成的工具提示标记相当简单,但它确实需要一个位置(默认情况下,由插件设置为top
by the plugin).top
)。
Making tooltips work for keyboard and assistive technology users为键盘和辅助技术用户提供工具提示
You should only add tooltips to HTML elements that are traditionally keyboard-focusable and interactive (such as links or form controls). 您应该只将工具提示添加到HTML元素中,这些元素传统上是以键盘为焦点的和交互式的(例如链接或表单控件)。Although arbitrary HTML elements (such as 尽管通过添加 <span>
s) can be made focusable by adding the tabindex="0"
attribute, this will add potentially annoying and confusing tab stops on non-interactive elements for keyboard users, and most assistive technologies currently do not announce the tooltip in this situation. tabindex="0"
隆隆可以使任意HTML元素(例如<span>
)可聚焦,这将为键盘用户在非交互式元素上添加潜在的烦人和令人困惑的制表符,并且大多数辅助技术目前不会在这种情况下公布工具提示。Additionally, do not rely solely on 此外,不要只依赖hover
as the trigger for your tooltip, as this will make your tooltips impossible to trigger for keyboard users.hover
作为工具提示的触发器,因为这将使键盘用户无法触发工具提示。
<!-- HTML to write -->
<a href="#" data-bs-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>
Disabled elements禁用的元素
Elements with the 具有disabled
attribute aren’t interactive, meaning users cannot focus, hover, or click them to trigger a tooltip (or popover). disabled
属性的元素不是交互式的,这意味着用户不能聚焦、悬停或单击它们来触发工具提示(或弹出窗口)。As a workaround, you’ll want to trigger the tooltip from a wrapper 作为解决方法,您需要从包装器<div>
or <span>
, ideally made keyboard-focusable using tabindex="0"
.<div>
或<span>
触发工具提示,理想情况下,使用tabindex="0"
使键盘可聚焦。
<span class="d-inline-block" tabindex="0" data-bs-toggle="tooltip" title="Disabled tooltip">
<button class="btn btn-primary" type="button" disabled>Disabled button</button>
</span>
Options选项
Options can be passed via data attributes or JavaScript. 选项可以通过数据属性或JavaScript传递。For data attributes, append the option name to 对于数据属性,将选项名称附加到data-bs-
, as in data-bs-animation=""
. data-bs-
,如data-bs-animation=""
。Make sure to change the case type of the option name from camelCase to kebab-case when passing the options via data attributes. 通过数据属性传递选项时,确保将选项名称的大小写类型从camelCase更改为kebab case。For example, instead of using 例如,不是使用data-bs-customClass="beautifier"
, use data-bs-custom-class="beautifier"
.data-bs-customClass="beautifier"
,而是使用data-bs-custom-class="beautifier"
。
sanitize
, sanitizeFn
, and allowList
options cannot be supplied using data attributes.sanitize
、sanitizeFn
和allowList
选项。
Name | Type | Default | Description |
---|---|---|---|
animation |
boolean | true |
|
container |
string | element | false | false |
|
delay |
number | object | 0 |
|
html |
boolean | false |
|
placement |
string | function | 'top' |
|
selector |
string | false | false |
jQuery.on support). jQuery.on 支持)。 |
template |
string | '<div class="tooltip" role="tooltip"><div class="tooltip-arrow"></div><div class="tooltip-inner"></div></div>' |
|
title |
string | element | function | '' |
|
trigger |
string | 'hover focus' |
|
fallbackPlacements |
array | ['top', 'right', 'bottom', 'left'] |
|
boundary |
string | element | 'clippingParents' |
'clippingParents' and can accept an HTMLElement reference (via JavaScript only). 'clippingParents' ,可以接受HTMLElement引用(仅通过JavaScript)。 |
customClass |
string | function | '' |
|
sanitize |
boolean | true |
'template' and 'title' options will be sanitized. 'template' 和'title' 选项,将被清除。 |
allowList |
object | Default value | |
sanitizeFn |
null | function | null |
|
offset |
array | string | function | [0, 0] |
|
popperConfig |
null | object | function | null |
|
Data attributes for individual tooltips单个工具提示的数据属性
Options for individual tooltips can alternatively be specified through the use of data attributes, as explained above.如上所述,也可以通过使用数据属性指定单个工具提示的选项。
Using function with popperConfig
将函数与popperConfig
一起使用
popperConfig
var tooltip = new bootstrap.Tooltip(element, {
popperConfig: function (defaultBsPopperConfig) {
// var newPopperConfig = {...}
// use defaultBsPopperConfig if needed...
// return newPopperConfig
}
})
Methods方法
Asynchronous methods and transitions异步方法和转换
All API methods are asynchronous and start a transition. 所有API方法都是异步的,并开始转换。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有关更多信息,请参阅JavaScript文档.
show
Reveals an element’s tooltip. 显示元素的工具提示。Returns to the caller before the tooltip has actually been shown (i.e. before the 在实际显示工具提示之前(即在shown.bs.tooltip
event occurs). shown.bs.tooltip
事件发生之前)返回给调用者。This is considered a “manual” triggering of the tooltip. 这被认为是工具提示的“手动”触发。Tooltips with zero-length titles are never displayed.不显示标题长度为零的工具提示。
tooltip.show()
hide
Hides an element’s tooltip. 隐藏元素的工具提示。Returns to the caller before the tooltip has actually been hidden在实际隐藏工具提示之前返回调用方 (i.e. before the (即,在hidden.bs.tooltip
event occurs). hidden.bs.tooltip
事件发生之前)。This is considered a “manual” triggering of the tooltip.这被认为是工具提示的“手动”触发。
tooltip.hide()
toggle
Toggles an element’s tooltip. 切换元素的工具提示。Returns to the caller before the tooltip has actually been shown or hidden在实际显示或隐藏工具提示之前返回调用方 (i.e. before the (即在shown.bs.tooltip
or hidden.bs.tooltip
event occurs). shown.bs.tooltip
或hidden.bs.tooltip
事件发生之前)。This is considered a “manual” triggering of the tooltip.这被认为是工具提示的“手动”触发。
tooltip.toggle()
dispose
Hides and destroys an element’s tooltip (Removes stored data on the DOM element). 隐藏并销毁元素的工具提示(删除DOM元素上存储的数据)。Tooltips that use delegation (which are created using the 使用委托(使用selector
option) cannot be individually destroyed on descendant trigger elements.selector
选项创建)的工具提示不能在后代触发器元素上单独销毁。
tooltip.dispose()
enable
Gives an element’s tooltip the ability to be shown. 提供显示元素的工具提示。Tooltips are enabled by default.默认情况下启用工具提示。
tooltip.enable()
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.工具提示只有在重新启用时才能显示。
tooltip.disable()
toggleEnabled
Toggles the ability for an element’s tooltip to be shown or hidden.切换显示或隐藏元素工具提示的功能。
tooltip.toggleEnabled()
update
Updates the position of an element’s tooltip.更新元素工具提示的位置。
tooltip.update()
getInstance
Static method which allows you to get the tooltip instance associated with a DOM element静态方法,允许您获取与DOM元素关联的工具提示实例
var exampleTriggerEl = document.getElementById('example')
var tooltip = bootstrap.Tooltip.getInstance(exampleTriggerEl) // Returns a Bootstrap tooltip instance
getOrCreateInstance
Static method which allows you to get the tooltip instance associated with a DOM element, or create a new one in case it wasn’t initialised静态方法,允许您获取与DOM元素关联的工具提示实例,或者在未初始化的情况下创建一个新实例
var exampleTriggerEl = document.getElementById('example')
var tooltip = bootstrap.Tooltip.getOrCreateInstance(exampleTriggerEl) // Returns a Bootstrap tooltip instance
Events事件
show.bs.tooltip |
show instance method is called.show 实例方法时,此事件立即激发。 |
shown.bs.tooltip |
|
hide.bs.tooltip |
hide instance method has been called.hide 实例方法后立即激发此事件。 |
hidden.bs.tooltip |
|
inserted.bs.tooltip |
show.bs.tooltip event when the tooltip template has been added to the DOM.show.bs.tooltip 事件之后激发此事件。 |
var myTooltipEl = document.getElementById('myTooltip')
var tooltip = new bootstrap.Tooltip(myTooltipEl)
myTooltipEl.addEventListener('hidden.bs.tooltip', function () {
// do something...
})
tooltip.hide()