Popover

插件依赖

弹出框依赖工具提示插件,因此需要先加载工具提示插件。

选择性加入的功能

出于性能方面的考虑,工具提示和弹框组件的data属性api是选择性加入的,也就是说你必须自己初始化他们

弹出框在按钮组和输入框组中使用时,需要额外的设置

当提示框与.btn-group.input-group联合使用时,你需要指定container: 'body'选项(见下面的文档)以避免不需要的副作用(例如,当弹出框显示之后,与其合作的页面元素可能变得更宽或是去圆角)。

在禁止使用的页面元素上使用弹出框时需要额外增加一个元素将其包裹起来

为了给disabled.disabled元素添加弹出框时,将需要增加弹出框的页面元素包裹在一个<div>中,然后对这个<div>元素应用弹出框。

类型

静态类型

Popover top

Sed posuere consectetur est at lobortis. Aenean eu leo quam. Pellentesque ornare sem lacinia quam venenatis vestibulum.

Popover right

Sed posuere consectetur est at lobortis. Aenean eu leo quam. Pellentesque ornare sem lacinia quam venenatis vestibulum.

Popover bottom

Sed posuere consectetur est at lobortis. Aenean eu leo quam. Pellentesque ornare sem lacinia quam venenatis vestibulum.

Popover left

Sed posuere consectetur est at lobortis. Aenean eu leo quam. Pellentesque ornare sem lacinia quam venenatis vestibulum.

不带箭头指示

Popover top

Sed posuere consectetur est at lobortis. Aenean eu leo quam. Pellentesque ornare sem lacinia quam venenatis vestibulum.

动态演示

不同方向

指定html元素作为弹出内容

Click to toggle popover

Popover top

Lorem ipsum dolor. Nisi, ad, quisquam! Eum, excepturi, placeat.
Lorem ipsum dolor. Explicabo, nobis, nesciunt! Sequi, doloribus, natus.
Lorem ipsum dolor. Ipsum, suscipit, quibusdam? Minus, tenetur, neque.

用法

通过JavaScript启用弹出框:

js$('#example').popover(options)

选项

可以将选项通过data属性或JavaScript传递。对于data属性,需要将选项名称放到data-之后,例如data-animation=""

名称 类型 默认值 描述
animation boolean true apply a CSS fade transition to the tooltip
html boolean false Insert HTML into the popover. 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 'right' how to position the popover - top | bottom | left | right | auto.
When "auto" is specified, it will dynamically reorient the popover. For example, if placement is "auto left", the tooltip will display to the left when possible, otherwise it will display right.
selector string false if a selector is provided, tooltip objects will be delegated to the specified targets. in practice, this is used to enable dynamic HTML content to have popovers added. See this and an informative example.
trigger string 'click' how popover is triggered - click | hover | focus | manual
title string | function '' default title value if title attribute isn't present
content string | function '' default content value if data-content attribute isn't present
delay number | object 0

delay showing and hiding the popover (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 }

container string | false false

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

为单个弹出框应用data属性

对单个弹出框可以通过data属性单独指定选项,如上所示。

方法

$().popover(options)

为一组元素初始化弹出框。

.popover('show')

显示弹出框。

js$('#element').popover('show')

.popover('hide')

隐藏弹出框。

js$('#element').popover('hide')

.popover('toggle')

展示或隐藏弹出框。

js$('#element').popover('toggle')

.popover('destroy')

隐藏并销毁弹出框。

js$('#element').popover('destroy')

事件

事件类型 描述
show.bs.popover show方法被调用之后,此事件将被立即触发。
shown.bs.popover 当弹出框展示到用户面前之后(同时CSS过渡效果执行完之后)此事件被触发。
hide.bs.popover hide方法被调用之后,此事件被触发。
hidden.bs.popover 当弹出框被隐藏之后(同时CSS过渡效果执行完之后),此事件被触发。
$('#myPopover').on('hidden.bs.popover', function () {
  // do something…
})