Bootstrap Popover (Popover) Plugin
The popover (Popover) is similar to the tooltip (Tooltip), providing an extended view. To activate a popover, the user only needs to hover the mouse over the element. The content of the popover can be filled entirely using the Bootstrap Data API (Bootstrap Data API). This method relies on the tooltip (tooltip).
If you want to reference the plugin's functionality separately, you need to include popover.js, which depends on Tooltip (Tooltip) Plugin. Or, as Bootstrap Plugins Overview chapter mentions, you can include bootstrap.js or the compressed version bootstrap.min.js。
Usage
The popover (Popover) plugin generates content and markup on demand, and by default places the popover after its trigger element. You can add a popover in the following two ways:
- Via data attributes: to add a popover, simply add the data-toggle="popover" to an anchor/button tag. The anchor's title is the text of the popover. By default, the plugin places the popover at the top.
请悬停在我的上面
$('#identifier').popover(options)
Unlike the dropdowns and other plugins discussed earlier, the popover (Popover) plugin is not a pure CSS plugin. To use it, you must activate it with jQuery (read JavaScript). Use the following script to enable all popovers on the page:
$(function () { $("[data-toggle='popover']").popover(); });
Example
The following example demonstrates using the popover (Popover) plugin via data attributes.
Example
<div class="container" style=">padding: 100px 50px 10px;
"
<button type="button" class=">btn btn-default
"
title
=
"
Popover title
"
data-container
=
"
body
"
data-toggle
=
"
popover
"
data-placement
=
"
left
"
data-content
=
"
Some content in the left Popover
"
Left Popover
button
<button type="button" class=">btn btn-primary
"
title
=
"
Popover title
"
data-container
=
"
body
"
data-toggle
=
"
popover
"
data-placement
=
"
top
"
data-content
=
"
Some content in the top Popover
"
Top Popover
button
<button type="button" class=">btn btn-success
"
title
=
"
Popover title
"
data-container
=
"
body
"
data-toggle
=
"
popover
"
data-placement
=
"
bottom
"
data-content
=
"
Some content in the bottom Popover
"
Bottom Popover
button
<button type="button" class=">btn btn-warning
"
title
=
"
Popover title
"
data-container
=
"
body
"
data-toggle
=
"
popover
"
data-placement
=
"
right
"
data-content
=
"
Some content in the right Popover
"
Right Popover
button
div
<script$(function(){$("[data-toggle='popover']" )>.
popover
(
)
;
}
)
;
script
div
Try it »
The result is as follows:
Option
Some options are added through the Bootstrap Data API (Bootstrap Data API) or called via JavaScript. The following table lists these options:
| Option name | Type / Default value | Data attribute name | Description |
|---|---|---|---|
| animation | boolean | ||
| Default value: true | data-animation | Apply a CSS fade transition to the popover. | |
| html | boolean | ||
| Default value: false | data-html | Insert HTML into the popover. If false, jQuery's text method will be used to insert content into the DOM. If you are concerned about XSS attacks, use text. | |
| placement | string|function | ||
| Default value: top | data-placement | Specifies how to position the popover (i.e., top|bottom|left|right|auto). | |
| When specified as auto the popover is dynamically adjusted. For example, if placement is "auto left", the popover will be displayed on the left as much as possible, and only on the right when conditions do not allow it. | |||
| selector | string | ||
| Default value: false | data-selector | If a selector is provided, the popover object will be delegated to the specified target. | |
| title | string | function | ||
| Default value: '' | data-title | If the title attribute is not specified, the title option is the default title value. | |
| trigger | string | ||
| Default value: 'hover focus' | data-trigger | Defines how the popover is triggered: ** click| hover | focus | manual**. You can pass multiple triggers, separated by spaces. | |
| delay | number | object | ||
| Default value: 0 | data-delay | The number of milliseconds to delay showing and hiding the popover - not applicable to the manual trigger type. If a number is provided, the delay applies to both showing and hiding. If an object is provided, the structure is as follows: |
delay: { show: 500, hide: 100 }| onSelect | event | container | string | false
Default value: false | data-container | Append the popover to the specified element.
Example: container: 'body' |
Method
Below are some useful methods in the popover (Popover) plugin:
| Method | Description | Example |
|---|---|---|
| Options: .popover(options) | Attach a popover handler to a collection of elements. | |
| $().popover(options) | ||
| Toggle: .popover('toggle') | Toggle the display of an element's popover. |
$('#element').popover('toggle')| onSelect | event | Show: .popover('show') | Show an element's popover. |
$('#element').popover('show')
| onSelect | event | Hide: .popover('hide') | Hide an element's popover. |
$('#element').popover('hide')
| onSelect | event | Destroy: .popover('destroy') | Hide and destroy an element's popover. |
$('#element').popover('destroy')
|
Example
The following example demonstrates the methods of the popover (Popover) plugin:
Example
<div class="container" style=">padding: 100px 50px 10px;
"
<button type="button" class=">btn btn-default popover-show
"
title
=
"
Popover title
"
data-container
=
"
body
"
data-toggle
=
"
popover
"
data-placement
=
"
left
"
data-content
=
"
Some content in the left Popover -- show method
"
Left Popover
button
<button type="button" class=">btn btn-primary popover-hide
"
title
=
"
Popover title
"
data-container
=
"
body
"
data-toggle
=
"
popover
"
data-placement
=
"
top
"
data-content
=
"
Some content in the top Popover -- hide method
"
Top Popover
button
<button type="button" class=">btn btn-success popover-destroy
"
title
=
"
Popover title
"
data-container
=
"
body
"
data-toggle
=
"
popover
"
data-placement
=
"
bottom
"
data-content
=
"
Some content in the bottom Popover -- destroy method
"
Bottom Popover
button
<button type="button" class=">btn btn-warning popover-toggle
"
title
=
"
Popover title
"
data-container
=
"
body
"
data-toggle
=
"
popover
"
data-placement
=
"
right
"
data-content
=
"
Some content in the right Popover -- toggle method
"
Right Popover
button
<br>
<br>
<br>
<br>
<br>
<br>
<p class="popover-options">
<a href="" type="button" class=">btn btn-warning
"
title
=
"
Title
"
data-container
=
"
body
"
data-toggle
=
"
popover
"
data-content
=
"
Some content in the Popover -- options method
"
Popover
a
p
<script$(function(){$('.popover-show' )>.
popover
(
'
show
'
)
;
}
)
; $
(
function
(
)
{
$
(
'
.popover-hide
'
)
.
popover
(
'
hide
'
)
;
}
)
; $
(
function
(
)
{
$
(
'
.popover-destroy
'
)
.
popover
(
'
destroy
'
)
;
}
)
; $
(
function
(
)
{
$
(
'
.popover-toggle
'
)
.
popover
(
'
toggle
'
)
;
}
)
; $
(
function
(
)
{
$
(
"
.popover-options a
"
)
.
popover
(
{
html
:
true
}
)
;
}
)
;
script
div
Try it »
The result is as follows:
Events
The following table lists the events used in the popover (Popover) plugin. These events can be used as hooks in functions.
| Events | Description | Example |
|---|---|---|
| show.bs.popover | This event is fired immediately when the show instance method is called. |
$('#mypopover').on('show.bs.popover', function () { // do something... })| onSelect | event | shown.bs.popover | Fired when the popover becomes visible to the user (will wait for CSS transitions to complete). |
$('#mypopover').on('shown.bs.popover', function () {
// Perform some actions...
})
| onSelect | event | hide.bs.popover | Fired immediately when the hide instance method is called. |
$('#mypopover').on('hide.bs.popover', function () {
// Perform some actions...
})
| onSelect | event | hidden.bs.popover | Fired when the popover is hidden from the user (will wait for CSS transitions to complete). |
$('#mypopover').on('hidden.bs.popover', function () {
// Perform some actions...
})
|
Example
The following example demonstrates the events of the popover (Popover) plugin:
Example
<div clas="container" style=">padding: 100px 50px 10px;
"
<button type="button" class=">btn btn-primary popover-show
"
title
=
"
Popover title
"
data-container
=
"
body
"
data-toggle
=
"
popover
"
data-content
=
"
Some content in the left Popover -- show method
"
Left Popover
button
div
<script$(function(){$('.popover-show' )>.
popover
(
'
show
'
)
;
}
)
; $
(
function
(
)
{
$
(
'
.popover-show
'
)
.
on
(
'
shown.bs.popover
'
,
function
(
)
{
alert
(
"
Alert message when shown
"
)
;
}
)
}
)
;
script
div
Try it »
The result is as follows: