彈出框控件類似于提示框,它在鼠標(biāo)點(diǎn)擊到元素后顯示,與提示框不同的是它可以顯示更多的內(nèi)容。
通過向元素添加 data-bs-toggle="popover" 來來創(chuàng)建彈出框。
title 屬性的內(nèi)容為彈出框的標(biāo)題,data-bs-content 屬性顯示了彈出框的文本內(nèi)容:
<button type="button" class="btn btn-primary" data-bs-toggle="popover" title="彈出框標(biāo)題" data-bs-content="彈出框內(nèi)容">多次點(diǎn)我</button>
注意: 彈出框要寫在 JavaScript 的初始化代碼里。
以下實例可以在文檔的任何地方使用彈出框:
<!DOCTYPE html>
<html>
<head>
<title>Bootstrap5 實例</title>
<meta charset="utf-8">
<meta name="viewport" content="width=device-width, initial-scale=1">
<link rel="external nofollow" target="_blank" rel="external nofollow" target="_blank" rel="external nofollow" target="_blank" rel="external nofollow" target="_blank" rel="stylesheet">
<script src="https://cdn.staticfile.org/twitter-bootstrap/5.1.1/js/bootstrap.bundle.min.js" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" ></script>
</head>
<body>
<div class="container mt-3">
<h3>彈出框?qū)嵗?lt;/h3>
<button type="button" class="btn btn-primary" data-bs-toggle="popover" title="彈出框標(biāo)題" data-bs-content="彈出框內(nèi)容">
多次點(diǎn)我
</button>
</div>
<script>
var popoverTriggerList = [].slice.call(document.querySelectorAll('[data-bs-toggle="popover"]'))
var popoverList = popoverTriggerList.map(function (popoverTriggerEl) {
return new bootstrap.Popover(popoverTriggerEl)
})
</script>
</body>
</html>
默認(rèn)情況下彈出框顯示在元素右側(cè)。
可以使用 data-bs-placement 屬性來設(shè)定彈出框顯示的方向: top, bottom, left 或 right:
<!DOCTYPE html>
<html>
<head>
<title>Bootstrap5 實例</title>
<meta charset="utf-8">
<meta name="viewport" content="width=device-width, initial-scale=1">
<link rel="external nofollow" target="_blank" rel="external nofollow" target="_blank" rel="external nofollow" target="_blank" rel="external nofollow" target="_blank" rel="stylesheet">
<script src="https://cdn.staticfile.org/twitter-bootstrap/5.1.1/js/bootstrap.bundle.min.js" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" ></script>
</head>
<body>
<div class="container mt-3">
<h3>指定彈出框的位置</h3>
<a href="#" title="標(biāo)題" data-bs-toggle="popover" data-bs-placement="top" data-bs-content="我是內(nèi)容部分">點(diǎn)我</a>
<a href="#" title="標(biāo)題" data-bs-toggle="popover" data-bs-placement="bottom" data-bs-content="我是內(nèi)容部分">點(diǎn)我</a>
<a href="#" title="標(biāo)題" data-bs-toggle="popover" data-bs-placement="left" data-bs-content="我是內(nèi)容部分">點(diǎn)我</a>
<a href="#" title="標(biāo)題" data-bs-toggle="popover" data-bs-placement="right" data-bs-content="我是內(nèi)容部分">點(diǎn)我</a>
</div>
<script>
var popoverTriggerList = [].slice.call(document.querySelectorAll('[data-bs-toggle="popover"]'))
var popoverList = popoverTriggerList.map(function (popoverTriggerEl) {
return new bootstrap.Popover(popoverTriggerEl)
})
</script>
</body>
</html>
默認(rèn)情況下,彈出框在再次點(diǎn)擊指定元素后就會關(guān)閉,你可以使用 data-bs-trigger="focus" 屬性來設(shè)置在鼠標(biāo)點(diǎn)擊元素外部區(qū)域來關(guān)閉彈出框:
<!DOCTYPE html>
<html>
<head>
<title>Bootstrap5 實例</title>
<meta charset="utf-8">
<meta name="viewport" content="width=device-width, initial-scale=1">
<link rel="external nofollow" target="_blank" rel="external nofollow" target="_blank" rel="external nofollow" target="_blank" rel="external nofollow" target="_blank" rel="stylesheet">
<script src="https://cdn.staticfile.org/twitter-bootstrap/5.1.1/js/bootstrap.bundle.min.js" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" ></script>
</head>
<body>
<div class="container mt-3">
<h3>取消彈出框</h3>
<a href="#" title="取消彈出框" data-bs-toggle="popover" data-bs-trigger="focus" data-bs-content="點(diǎn)擊文檔的其他地方關(guān)閉我">點(diǎn)我</a>
</div>
<script>
var popoverTriggerList = [].slice.call(document.querySelectorAll('[data-bs-toggle="popover"]'))
var popoverList = popoverTriggerList.map(function (popoverTriggerEl) {
return new bootstrap.Popover(popoverTriggerEl)
})
</script>
</body>
</html>
提示:如果你想實現(xiàn)在鼠標(biāo)移動到元素上顯示,移除后消失的效果,可以使用 data-bs-trigger 屬性,并設(shè)置值為 "hover":
<!DOCTYPE html>
<html>
<head>
<title>Bootstrap5 實例</title>
<meta charset="utf-8">
<meta name="viewport" content="width=device-width, initial-scale=1">
<link rel="external nofollow" target="_blank" rel="external nofollow" target="_blank" rel="external nofollow" target="_blank" rel="external nofollow" target="_blank" rel="stylesheet">
<script src="https://cdn.staticfile.org/twitter-bootstrap/5.1.1/js/bootstrap.bundle.min.js" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" ></script>
</head>
<body>
<div class="container mt-3">
<h3>彈出框?qū)嵗?lt;/h3>
<a href="#" title="標(biāo)題" data-bs-toggle="popover" data-bs-trigger="hover" data-bs-content="一些內(nèi)容">鼠標(biāo)移動到我這</a>
</div>
<script>
var popoverTriggerList = [].slice.call(document.querySelectorAll('[data-bs-toggle="popover"]'))
var popoverList = popoverTriggerList.map(function (popoverTriggerEl) {
return new bootstrap.Popover(popoverTriggerEl)
})
</script>
</body>
</html>
更多建議: