Bootstrap 简明教程
Bootstrap - Popovers
本章将讨论 Bootstrap 中的提示框。提示框通常含有触发元素的其他信息、内容或动作。
This chapter will discuss about popovers in Bootstrap. A popover typically contains additional information, context, or actions related to the triggering element.
提示框可能会包含文本、图片、链接、按钮或其他内容,具体取决于其目的和内容。Bootstrap 提供了内置的提示框组件,可以轻松集成到网络应用程序中。
The popover may contain text, images, links, buttons, or other content, depending on its purpose and context. Bootstrap provides built-in popover components that can be easily integrated into web applications.
Enable Popovers
按它们的 data-bs-toggle 属性初始化页面中的所有提示框。
Initialize all the popovers on a page, by their data-bs-toggle attribute
const popoverTriggerList = document.querySelectorAll('[data-bs-toggle="popover"]')
const popoverList = [...popoverTriggerList].map(popoverTriggerEl => new bootstrap.Popover(popoverTriggerEl))
Creating a Popover
向元素添加 data-bs-toggle="popover" 属性,以创建一个提示框。
Add the data-bs-toggle="popover" attribute to an element, to create a popover.
-
The data-bs-toggle attribute defines the popover.
-
The title attribute defines the title of the popover
-
The data-content attribute defines the content to be shown in the respective popover.
Example
让我们看看创建一个提示框的示例:
Let’s see an example of creating a popover:
你可以编辑并尝试运行此代码,使用 编辑和运行 选项。
You can edit and try running this code using the *Edit & Run *option.
<!DOCTYPE html>
<html lang="en">
<head>
<title>Bootstrap Popover</title>
<meta charset="UTF-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<link href="https://cdn.jsdelivr.net/npm/bootstrap@5.3.0-alpha3/dist/css/bootstrap.min.css" rel="stylesheet">
<link href="https://getbootstrap.com/docs/5.3/assets/css/docs.css" rel="stylesheet">
<script src="https://cdn.jsdelivr.net/npm/bootstrap@5.3.0-alpha3/dist/js/bootstrap.bundle.min.js"></script>
</head>
<body>
<h4>Bootstrap creation</h4><br><br>
<button type="button" class="btn btn-primary"
data-bs-toggle="popover" data-bs-placement="top"
data-bs-title="Popover"
data-bs-content="It is a new popover.">
Click to view popover
</button>
<script>
const popoverTriggerList = document.querySelectorAll('[data-bs-toggle="popover"]')
const popoverList = [...popoverTriggerList].map(popoverTriggerEl => new bootstrap.Popover(popoverTriggerEl))
</script>
</body>
</html>
Positioning of a Popover
有四个选项可用于提示框的位置: left, right, top and bottom 对齐。
There are four options available for the positioning of the popover: left, right, top and bottom aligned.
默认情况下,提示框将显示在元素的右侧。
By default, the popover will appear on the right of the element.
data-bs-placement 属性用于设置提示框的位置。
The data-bs-placement attribute is used to set the position of the popover.
Example
让我们看看设置提示框位置的示例:
Let’s see an example of setting the position of a popover:
你可以编辑并尝试运行此代码,使用 编辑和运行 选项。
You can edit and try running this code using the *Edit & Run *option.
<!DOCTYPE html>
<html lang="en">
<head>
<title>Bootstrap Popovers</title>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<link href="https://cdn.jsdelivr.net/npm/bootstrap@5.3.0-alpha3/dist/css/bootstrap.min.css" rel="stylesheet">
<link href="https://getbootstrap.com/docs/5.3/assets/css/docs.css" rel="stylesheet">
<script src="https://cdn.jsdelivr.net/npm/bootstrap@5.3.0-alpha3/dist/js/bootstrap.bundle.min.js"></script>
</head>
<body>
<h4>Positioning of Popover</h4>
<br><br><br>
<button type="button" class="btn btn-secondary" data-bs-container="body" data-bs-toggle="popover" data-bs-placement="top" data-bs-content="Top popover">
Popover on top
</button>
<button type="button" class="btn btn-secondary" data-bs-container="body" data-bs-toggle="popover" data-bs-placement="right" data-bs-content="Right popover">
Popover on right
</button>
<button type="button" class="btn btn-secondary" data-bs-container="body" data-bs-toggle="popover" data-bs-placement="bottom" data-bs-content="Bottom popover">
Popover on bottom
</button>
<button type="button" class="btn btn-secondary" data-bs-container="body" data-bs-toggle="popover" data-bs-placement="left" data-bs-content="Left popover">
Popover on left
</button>
<script>
const popoverTriggerList = document.querySelectorAll('[data-bs-toggle="popover"]')
const popoverList = [...popoverTriggerList].map(popoverTriggerEl => new bootstrap.Popover(popoverTriggerEl))
</script>
</body>
</html>
Custom popovers
可以自定义外观使用自定义类 data-bs-custom-class="custom-popover" 。
The appearance of popovers can be customized using a custom class data-bs-custom-class="custom-popover".
Example
让我们看看创建一个自定义提示框的示例:
Let’s see an example of creating a custom popover:
你可以编辑并尝试运行此代码,使用 编辑和运行 选项。
You can edit and try running this code using the *Edit & Run *option.
<!DOCTYPE html>
<html lang="en">
<head&>
<title>Bootstrap - Popovers</title>
<meta charset="UTF-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<link href="https://cdn.jsdelivr.net/npm/bootstrap@5.2.3/dist/css/bootstrap.min.css" rel="stylesheet">
<script src="https://cdn.jsdelivr.net/npm/bootstrap@5.2.3/dist/js/bootstrap.bundle.min.js"></script>
</head>
<body>
<h4>Custom Popover</h4><br><br>
<!-- Define custom container -->
<button type="button" class="btn btn-primary"
data-bs-toggle="popover" data-bs-placement="top"
data-bs-custom-class="custom-popover"
data-bs-title="Custom popover"
data-bs-content="It is a custom popover.">
Custom popover
</button>
<script>
const popoverTriggerList = document.querySelectorAll('[data-bs-toggle="popover"]')
const popoverList = [...popoverTriggerList].map(popoverTriggerEl => new bootstrap.Popover(popoverTriggerEl))
</script>
</body>
</html>
Dismissible Popover
提示框在再次单击同一元素时会关闭,这是默认设置。但可以使用属性 data-bs-trigger="focus" ,这样单击元素外部时会关闭提示框。
The popover gets closed when the same element is clicked again, by default. However, the attribute data-bs-trigger="focus" can be used, which will close the popover when clicking outside the element.
Example
让我们看看可隐藏提示框的示例:
Let’s see an example of dismissible popover:
你可以编辑并尝试运行此代码,使用 编辑和运行 选项。
You can edit and try running this code using the *Edit & Run *option.
<!DOCTYPE html>
<html lang="en">
<head>
<title>Bootstrap Dismissible Popover</title>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<link href="https://cdn.jsdelivr.net/npm/bootstrap@5.2.3/dist/css/bootstrap.min.css" rel="stylesheet">
<script src="https://cdn.jsdelivr.net/npm/bootstrap@5.2.3/dist/js/bootstrap.bundle.min.js"></script>
</head>
<body>
<div class="container mt-3">
<h4>Dismissed on next click - Popover</h4><br>
<a href="#" title="Dismissible popover" data-bs-toggle="popover" data-bs-trigger="focus" data-bs-content="Click anywhere in the document to close this popover">Click me</a>
</div>
<script>
const popoverTriggerList = document.querySelectorAll('[data-bs-toggle="popover"]')
const popoverList = [...popoverTriggerList].map(popoverTriggerEl => new bootstrap.Popover(popoverTriggerEl))
</script>
</body>
</html>
Hovering of a Popover
当鼠标指针移到元素上方时,你希望一个提示框显示在悬停时,使用 data-bs-trigger="hover" 属性。
When the mouse pointer is moved over an element and you want a popover to appear on hovering, use the data-bs-trigger="hover" attribute.
Example
我们来看看一个悬停弹出框的示例:
Let’s see an example of a hovering popover:
你可以编辑并尝试运行此代码,使用 编辑和运行 选项。
You can edit and try running this code using the *Edit & Run *option.
<!DOCTYPE html>
<html lang="en">
<head>
<title>Bootstrap Popover on hover</title>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<link href="https://cdn.jsdelivr.net/npm/bootstrap@5.2.3/dist/css/bootstrap.min.css" rel="stylesheet">
<script src="https://cdn.jsdelivr.net/npm/bootstrap@5.2.3/dist/js/bootstrap.bundle.min.js"></script>
</head>
<body>
<div class="container mt-3">
<h4>Hoverable Popover</h4><br>
<a href="#" title="Header" data-bs-toggle="popover" data-bs-trigger="hover" data-bs-content="Popover text">Hover over me</a>
</div>
<script>
const popoverTriggerList = document.querySelectorAll('[data-bs-toggle="popover"]')
const popoverList = [...popoverTriggerList].map(popoverTriggerEl => new bootstrap.Popover(popoverTriggerEl))
</script>
</body>
</html>
Popover on disabled elements
对于禁用元素上的弹出框,你可以选择 data-bs-trigger="hover focus" ,这样弹出框会作为立即的视觉反馈形式出现在你的用户面前,因为他们可能不会点击禁用元素。
For popovers on disabled elements, you may prefer data-bs-trigger="hover focus" so that the popover appears as immediate visual feedback to your users as they may not expect to click on a disabled element.
Example
我们再来看看禁用元素上弹出框的示例:
Let’s see an example of a popover on disabled element:
你可以编辑并尝试运行此代码,使用 编辑和运行 选项。
You can edit and try running this code using the *Edit & Run *option.
<!DOCTYPE html>
<html lang="en">
<head>
<title>Bootstrap Popovers</title>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<link href="https://cdn.jsdelivr.net/npm/bootstrap@5.2.3/dist/css/bootstrap.min.css" rel="stylesheet">
<script src="https://cdn.jsdelivr.net/npm/bootstrap@5.2.3/dist/js/bootstrap.bundle.min.js"></script>
</head>
<body>
<h4>Popover on Disabled Element</h4><br>
<span class="d-inline-block" tabindex="0" data-bs-toggle="popover" data-bs-trigger="hover focus" data-bs-content="Disabled popover">
<button class="btn btn-primary" type="button" disabled>Disabled button</button>
</span>
<script>
const popoverTriggerList = document.querySelectorAll('[data-bs-toggle="popover"]')
const popoverList = [...popoverTriggerList].map(popoverTriggerEl => new bootstrap.Popover(popoverTriggerEl))
</script>
</body>
</html>
Options
-
An option name can be appended to the class data-bs- and that option can be passed as an attribute, such as data-bs-boundary="{value}".
-
When passing the options via data attributes, make sure to change the case type to "kebab-case" from "camelCase", such as use data-bs-fallback-placements="[array]" instead of data-bs-fallbackPlacements="[array]".
Example
以下是一个作为属性添加到 .data-bs- 类的选项示例:
Here is an example of options added as an attribute to .data-bs- class:
你可以编辑并尝试运行此代码,使用 编辑和运行 选项。
You can edit and try running this code using the *Edit & Run *option.
<!DOCTYPE html>
<html lang="en">
<head&>
<title>Bootstrap Popovers - Options</title>
<meta charset="UTF-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<link href="https://cdn.jsdelivr.net/npm/bootstrap@5.2.3/dist/css/bootstrap.min.css" rel="stylesheet">
<script src="https://cdn.jsdelivr.net/npm/bootstrap@5.2.3/dist/js/bootstrap.bundle.min.js"></script>
</head>
<body>
<h4>Popover options</h4><br>
<button type="button" class="btn btn-lg btn-success" data-bs-toggle="popover" data-bs-title ="Title added through options and that overrides the title provided" title="Popover description" data-content="Popover desc">Click Me to view</button>
<br><br>
<script>
const popoverTriggerList = document.querySelectorAll('[data-bs-toggle="popover"]')
const popoverList = [...popoverTriggerList].map(popoverTriggerEl => new bootstrap.Popover(popoverTriggerEl))
</script>
</body>
</html>
下面给出的表格显示了 Bootstrap 提供的各种选项,可作为数据属性附加到类 .data-bs- 上。
The table given below shows the various options provided by Bootstrap, to be appended to the class .data-bs- as a data attribute.
Name |
Type |
Default |
Description |
allowList |
object |
Object that contains allowed attributes and tags. |
|
animation |
boolean |
true |
A CSS fade transition is applied to the popover. |
boundary |
string, element |
'clippingParents' |
By default, it’s 'clippingParents' and can accept an HTML Element reference (via JavaScript only). |
container |
string, element, false |
false |
Appends the popover to a specific element. |
customClass |
string, function |
'' |
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'. |
delay |
number, object |
0 |
Delay showing and hiding the popover(ms). Object structure is: delay: { "show": 500, "hide": 100 }. |
fallbackPlacements |
array |
['top', 'right', 'bottom', 'left'] |
Define fallback placements by providing a list of placements in array. |
html |
boolean |
false |
Allow HTML in the popover. |
offset |
array, string, function |
[0, 0] |
Offset of the popover relative to its target. Ex: data-bs-offset="10,20". |
placement |
string, function |
'top' |
Decides the position of the popover. |
popperConfig |
null, object, function |
null |
To change Bootstrap’s default Popper config. |
sanitize |
boolean |
true |
Enable or disable the sanitization. |
sanitizeFn |
null, function |
null |
You can supply your own sanitize function. |
selector |
string, false |
false |
With a selector, popover objects will be delegated to the specified targets. |
template |
string |
'' |
While creating a popover, use the base HTML. |
title |
string, element, function |
'' |
It refers to the title of the popover. |
trigger |
string |
'hover-focus' |
Shows how the popover is triggered: click, hover, focus, manual. |