Bootstrap4 提示框

2018-06-01 15:34 更新

提示框是一個小小的彈窗,只有當(dāng)鼠標移動到元素上時才會顯示,鼠標移出后消失。在Bootstrap4中,提示框功能的實現(xiàn)其實也很簡單。

創(chuàng)建提示框

通過向元素添加 data-toggle="tooltip" 來來創(chuàng)建提示框。

title 屬性的內(nèi)容為提示框顯示的內(nèi)容:

<a href="#" data-toggle="tooltip" title="我是提示內(nèi)容!">鼠標移動到我這</a>

注意: 提示框要寫在 jQuery 的初始化代碼里: 然后在指定的元素上調(diào)用 tooltip() 方法。

以下實例可以在文檔的任何地方使用提示框:

實例:

<!DOCTYPE html>
<html>
<head>
  <title>Bootstrap4 實例</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" >
  <script src="https://cdn.bootcss.com/jquery/3.2.1/jquery.min.js" 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" ></script>
  <script src="https://cdn.bootcss.com/bootstrap/4.1.0/js/bootstrap.min.js" rel="external nofollow"  rel="external nofollow" ></script>
</head>
<body>
<div class="container">
  <h3>提示框?qū)嵗?lt;/h3><br>
  <a href="#" data-toggle="tooltip" title="我是提示內(nèi)容!">鼠標移動到我這</a>
</div>
<script>
$(document).ready(function(){
    $('[data-toggle="tooltip"]').tooltip();   
});
</script>
</body>
</html>

指定提示框的位置

默認情況下提示框會顯示在元素上方,但是我們可以使用 data-placement 屬性來設(shè)定提示框顯示的方向: 上top, 下bottom, 左left 或 右right:

實例:

<!DOCTYPE html>
<html>
<head>
  <title>Bootstrap4 實例</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" >
  <script src="https://cdn.bootcss.com/jquery/3.2.1/jquery.min.js" 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" ></script>
  <script src="https://cdn.bootcss.com/bootstrap/4.1.0/js/bootstrap.min.js" rel="external nofollow"  rel="external nofollow" ></script>
</head>
<body>
<div class="container">
  <h3>提示框?qū)嵗?lt;/h3><br>
  <a href="#" data-toggle="tooltip" data-placement="top" title="我是提示內(nèi)容!">鼠標移動到我這</a>
  <a href="#" data-toggle="tooltip" data-placement="bottom" title="我是提示內(nèi)容!">鼠標移動到我這</a>
  <a href="#" data-toggle="tooltip" data-placement="left" title="我是提示內(nèi)容!">鼠標移動到我這</a>
  <a href="#" data-toggle="tooltip" data-placement="right" title="我是提示內(nèi)容!">鼠標移動到我這</a>
</div>
<script>
$(document).ready(function(){
    $('[data-toggle="tooltip"]').tooltip();   
});
</script>
</body>
</html>


以上內(nèi)容是否對您有幫助:
在線筆記
App下載
App下載

掃描二維碼

下載編程獅App

公眾號
微信公眾號

編程獅公眾號