彈出框控件和提示框有點(diǎn)類似,用戶用鼠標(biāo)點(diǎn)擊元素后,就會(huì)顯示。不過和提示框不同的是,彈出框能夠顯示的內(nèi)容更多。
通過向元素添加 data-toggle="popover" 來來創(chuàng)建彈出框。
title 屬性的內(nèi)容為彈出框的標(biāo)題,data-content 屬性顯示了彈出框的文本內(nèi)容:
<a href="#" data-toggle="popover" title="彈出框標(biāo)題" data-content="彈出框內(nèi)容">多次點(diǎn)我</a>
注意: 彈出框要寫在 jQuery 的初始化代碼里: 然后在指定的元素上調(diào)用 popover() 方法。
以下實(shí)例可以在文檔的任何地方使用彈出框:
實(shí)例:
<!DOCTYPE html>
<html>
<head>
<title>Bootstrap4 實(shí)例</title>
<meta charset="utf-8">
<meta name="viewport" content="width=device-width, initial-scale=1">
<link rel="stylesheet" rel="external nofollow" target="_blank" rel="external nofollow" target="_blank" rel="external nofollow" target="_blank" rel="external nofollow" target="_blank" >
<script src="https://cdn.bootcss.com/jquery/3.2.1/jquery.min.js" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" ></script>
<script src="https://cdn.bootcss.com/popper.js/1.12.5/umd/popper.min.js" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" ></script>
<script src="https://cdn.bootcss.com/bootstrap/4.1.0/js/bootstrap.min.js" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" ></script>
</head>
<body>
<div class="container">
<h3>彈出框?qū)嵗?lt;/h3>
<a href="#" data-toggle="popover" title="彈出框標(biāo)題" data-content="彈出框內(nèi)容">多次點(diǎn)我</a>
</div>
<script>
$(document).ready(function(){
$('[data-toggle="popover"]').popover();
});
</script>
</body>
</html>
默認(rèn)情況下彈出框顯示在元素右側(cè)。
可以使用 data-placement 屬性來設(shè)定彈出框顯示的方向: 上:top, 下:bottom, 左:left 或 右:right:
實(shí)例:
<!DOCTYPE html>
<html>
<head>
<title>Bootstrap4 實(shí)例</title>
<meta charset="utf-8">
<meta name="viewport" content="width=device-width, initial-scale=1">
<link rel="stylesheet" rel="external nofollow" target="_blank" rel="external nofollow" target="_blank" rel="external nofollow" target="_blank" rel="external nofollow" target="_blank" >
<script src="https://cdn.bootcss.com/jquery/3.2.1/jquery.min.js" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" ></script>
<script src="https://cdn.bootcss.com/popper.js/1.12.5/umd/popper.min.js" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" ></script>
<script src="https://cdn.bootcss.com/bootstrap/4.1.0/js/bootstrap.min.js" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" ></script>
</head>
<body>
<div class="container">
<h3>彈出框?qū)嵗?lt;/h3> <br><br><br><br><br><br>
<a href="#" title="Header" data-toggle="popover" data-placement="top" data-content="Content">點(diǎn)我</a>
<a href="#" title="Header" data-toggle="popover" data-placement="bottom" data-content="Content">點(diǎn)我</a>
<a href="#" title="Header" data-toggle="popover" data-placement="left" data-content="Content">點(diǎn)我</a>
<a href="#" title="Header" data-toggle="popover" data-placement="right" data-content="Content">點(diǎn)我</a>
</div>
<script>
$(document).ready(function(){
$('[data-toggle="popover"]').popover();
});
</script>
</body>
</html>
默認(rèn)情況下,彈出框在再次點(diǎn)擊指定元素后就會(huì)關(guān)閉,你可以使用 data-trigger="focus" 屬性來設(shè)置在鼠標(biāo)點(diǎn)擊元素外部區(qū)域來關(guān)閉彈出框:
實(shí)例:
<!DOCTYPE html>
<html>
<head>
<title>Bootstrap4 實(shí)例</title>
<meta charset="utf-8">
<meta name="viewport" content="width=device-width, initial-scale=1">
<link rel="stylesheet" rel="external nofollow" target="_blank" rel="external nofollow" target="_blank" rel="external nofollow" target="_blank" rel="external nofollow" target="_blank" >
<script src="https://cdn.bootcss.com/jquery/3.2.1/jquery.min.js" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" ></script>
<script src="https://cdn.bootcss.com/popper.js/1.12.5/umd/popper.min.js" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" ></script>
<script src="https://cdn.bootcss.com/bootstrap/4.1.0/js/bootstrap.min.js" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" ></script>
</head>
<body>
<div class="container">
<h3>彈出框?qū)嵗?lt;/h3> <br>
<a href="#" title="取消彈出框" data-toggle="popover" data-trigger="focus" data-content="點(diǎn)擊文檔的其他地方關(guān)閉我">點(diǎn)我</a>
</div>
<script>
$(document).ready(function(){
$('[data-toggle="popover"]').popover();
});
</script>
</body>
</html>
提示:如果你想實(shí)現(xiàn)在鼠標(biāo)移動(dòng)到元素上顯示,移除后消失的效果,可以使用 data-trigger 屬性,并設(shè)置值為 "hover":
實(shí)例:
<!DOCTYPE html>
<html>
<head>
<title>Bootstrap4 實(shí)例</title>
<meta charset="utf-8">
<meta name="viewport" content="width=device-width, initial-scale=1">
<link rel="stylesheet" rel="external nofollow" target="_blank" rel="external nofollow" target="_blank" rel="external nofollow" target="_blank" rel="external nofollow" target="_blank" >
<script src="https://cdn.bootcss.com/jquery/3.2.1/jquery.min.js" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" ></script>
<script src="https://cdn.bootcss.com/popper.js/1.12.5/umd/popper.min.js" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" ></script>
<script src="https://cdn.bootcss.com/bootstrap/4.1.0/js/bootstrap.min.js" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" ></script>
</head>
<body>
<div class="container">
<h3>彈出框?qū)嵗?lt;/h3> <br>
<a href="#" title="Header" data-toggle="popover" data-trigger="hover" data-content="一些內(nèi)容">鼠標(biāo)移動(dòng)到我這</a>
</div>
<script>
$(document).ready(function(){
$('[data-toggle="popover"]').popover();
});
</script>
</body>
</html>
更多建議: