彈出框控件類似于提示框,它在鼠標(biāo)點(diǎn)擊到元素后顯示,與提示框不同的是它可以顯示更多的內(nèi)容。
通過(guò)向元素添加 data-toggle="popover" 來(lái)來(lái)創(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() 方法。
以下示例可以在文檔的任何地方使用彈出框:
<!DOCTYPE html> <html> <head> <title>Bootstrap 示例</title> <meta charset="utf-8"> <meta name="viewport" content="width=device-width, initial-scale=1"> <link rel="stylesheet" href="https://cdn.staticfile.org/twitter-bootstrap/4.3.1/css/bootstrap.min.css"> <script src="https://cdn.staticfile.org/jquery/3.2.1/jquery.min.js"></script> <script src="https://cdn.staticfile.org/popper.js/1.15.0/umd/popper.min.js"></script> <script src="https://cdn.staticfile.org/twitter-bootstrap/4.3.1/js/bootstrap.min.js"></script> </head> <body> <div class="container"> <h2>彈出框示例</h2> <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>測(cè)試看看 ?/?
默認(rèn)情況下彈出框顯示在元素右側(cè)。
可以使用 data-placement 屬性來(lái)設(shè)定彈出框顯示的方向: top, bottom, left 或 right:
<!DOCTYPE html> <html> <head> <title>Bootstrap 示例</title> <meta charset="utf-8"> <meta name="viewport" content="width=device-width, initial-scale=1"> <link rel="stylesheet" href="https://cdn.staticfile.org/twitter-bootstrap/4.3.1/css/bootstrap.min.css"> <script src="https://cdn.staticfile.org/jquery/3.2.1/jquery.min.js"></script> <script src="https://cdn.staticfile.org/popper.js/1.15.0/umd/popper.min.js"></script> <script src="https://cdn.staticfile.org/twitter-bootstrap/4.3.1/js/bootstrap.min.js"></script> </head> <body> <div class="container"> <h2>彈出框示例</h2> <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>測(cè)試看看 ?/?
按鈕中使用:
<!DOCTYPE html> <html> <head> <title>Bootstrap 示例</title> <meta charset="utf-8"> <meta name="viewport" content="width=device-width, initial-scale=1"> <link rel="stylesheet" href="https://cdn.staticfile.org/twitter-bootstrap/4.3.1/css/bootstrap.min.css"> <script src="https://cdn.staticfile.org/jquery/3.2.1/jquery.min.js"></script> <script src="https://cdn.staticfile.org/popper.js/1.15.0/umd/popper.min.js"></script> <script src="https://cdn.staticfile.org/twitter-bootstrap/4.3.1/js/bootstrap.min.js"></script> </head> <body> <div class="container"> <h2>彈出框示例</h2> <br><br><br><br><br><br> <button type="button" class="btn btn-secondary" data-container="body" data-toggle="popover" data-placement="top" data-content="Vivamus sagittis lacus vel augue laoreet rutrum faucibus."> Popover on top </button> <button type="button" class="btn btn-secondary" data-container="body" data-toggle="popover" data-placement="right" data-content="Vivamus sagittis lacus vel augue laoreet rutrum faucibus."> Popover on right </button> <button type="button" class="btn btn-secondary" data-container="body" data-toggle="popover" data-placement="bottom" data-content="Vivamus sagittis lacus vel augue laoreet rutrum faucibus."> Popover on bottom </button> <button type="button" class="btn btn-secondary" data-container="body" data-toggle="popover" data-placement="left" data-content="Vivamus sagittis lacus vel augue laoreet rutrum faucibus."> Popover on left </button> </div> <script> $(document).ready(function(){ $('[data-toggle="popover"]').popover(); }); </script> </body> </html>測(cè)試看看 ?/?
默認(rèn)情況下,彈出框在再次點(diǎn)擊指定元素后就會(huì)關(guān)閉,你可以使用 data-trigger="focus" 屬性來(lái)設(shè)置在鼠標(biāo)點(diǎn)擊元素外部區(qū)域來(lái)關(guān)閉彈出框:
<!DOCTYPE html> <html> <head> <title>Bootstrap 示例</title> <meta charset="utf-8"> <meta name="viewport" content="width=device-width, initial-scale=1"> <link rel="stylesheet" href="https://cdn.staticfile.org/twitter-bootstrap/4.3.1/css/bootstrap.min.css"> <script src="https://cdn.staticfile.org/jquery/3.2.1/jquery.min.js"></script> <script src="https://cdn.staticfile.org/popper.js/1.15.0/umd/popper.min.js"></script> <script src="https://cdn.staticfile.org/twitter-bootstrap/4.3.1/js/bootstrap.min.js"></script> </head> <body> <div class="container"> <h2>彈出框示例</h2> <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>測(cè)試看看 ?/?
提示:如果你想實(shí)現(xiàn)在鼠標(biāo)移動(dòng)到元素上顯示,移除后消失的效果,可以使用 data-trigger 屬性,并設(shè)置值為 "hover":
<!DOCTYPE html> <html> <head> <title>Bootstrap 示例</title> <meta charset="utf-8"> <meta name="viewport" content="width=device-width, initial-scale=1"> <link rel="stylesheet" href="https://cdn.staticfile.org/twitter-bootstrap/4.3.1/css/bootstrap.min.css"> <script src="https://cdn.staticfile.org/jquery/3.2.1/jquery.min.js"></script> <script src="https://cdn.staticfile.org/popper.js/1.15.0/umd/popper.min.js"></script> <script src="https://cdn.staticfile.org/twitter-bootstrap/4.3.1/js/bootstrap.min.js"></script> </head> <body> <div class="container"> <h2>彈出框示例</h2> <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>測(cè)試看看 ?/?