Bootstrap5 模態(tài)框

2023-06-28 15:19 更新

模態(tài)框(Modal)是覆蓋在父窗體上的子窗體。通常,目的是顯示來自一個單獨的源的內(nèi)容,可以在不離開父窗體的情況下有一些互動。子窗體可提供信息交互等。

如何創(chuàng)建模態(tài)框

以下實例創(chuàng)建了一個簡單的模態(tài)框效果 :

<!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="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"  rel="external nofollow"  rel="external nofollow"  rel="external nofollow"  rel="external nofollow" ></script>
</head>
<body>

<div class="container mt-3">
  <h3>模態(tài)框實例</h3>
  <p>點擊按鈕打開模態(tài)框</p>
  
  <button type="button" class="btn btn-primary" data-bs-toggle="modal" data-bs-target="#myModal">
    打開模態(tài)框
  </button>
</div>

<!-- 模態(tài)框 -->
<div class="modal" id="myModal">
  <div class="modal-dialog">
    <div class="modal-content">

      <!-- 模態(tài)框頭部 -->
      <div class="modal-header">
        <h4 class="modal-title">模態(tài)框標題</h4>
        <button type="button" class="btn-close" data-bs-dismiss="modal"></button>
      </div>

      <!-- 模態(tài)框內(nèi)容 -->
      <div class="modal-body">
        模態(tài)框內(nèi)容..
      </div>

      <!-- 模態(tài)框底部 -->
      <div class="modal-footer">
        <button type="button" class="btn btn-danger" data-bs-dismiss="modal">關閉</button>
      </div>

    </div>
  </div>
</div>

</body>
</html>

添加動畫

使用 .fade 類可以設置模態(tài)框彈出或關閉的效果:

<!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="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"  rel="external nofollow"  rel="external nofollow"  rel="external nofollow"  rel="external nofollow" ></script>
</head>
<body>

<div class="container mt-3">
  <h3>模態(tài)框實例</h3>
  <p>點擊按鈕打開模態(tài)框</p>
  
  <button type="button" class="btn btn-primary" data-bs-toggle="modal" data-bs-target="#myModal">
    打開模態(tài)框
  </button>
</div>

<!-- 模態(tài)框 -->
<div class="modal fade" id="myModal">
  <div class="modal-dialog">
    <div class="modal-content">

      <!-- 模態(tài)框頭部 -->
      <div class="modal-header">
        <h4 class="modal-title">模態(tài)框標題</h4>
        <button type="button" class="btn-close" data-bs-dismiss="modal"></button>
      </div>

      <!-- 模態(tài)框內(nèi)容 -->
      <div class="modal-body">
        模態(tài)框內(nèi)容..
      </div>

      <!-- 模態(tài)框底部 -->
      <div class="modal-footer">
        <button type="button" class="btn btn-danger" data-bs-dismiss="modal">關閉</button>
      </div>

    </div>
  </div>
</div>

</body>
</html>

模態(tài)框尺寸

我們可以通過添加 .modal-sm 類來創(chuàng)建一個小模態(tài)框,.modal-lg 類可以創(chuàng)建一個大模態(tài)框。

尺寸類放在 <div>元素的 .modal-dialog 類后 :

小模態(tài)框

<!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="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"  rel="external nofollow"  rel="external nofollow"  rel="external nofollow"  rel="external nofollow" ></script>
</head>
<body>

<div class="container mt-3">
  <h3>模態(tài)框實例</h3>
  <p>點擊按鈕打開模態(tài)框</p>
  
  <button type="button" class="btn btn-primary" data-bs-toggle="modal" data-bs-target="#myModal">
    打開模態(tài)框
  </button>
</div>

<!-- 模態(tài)框 -->
<div class="modal fade" id="myModal">
  <div class="modal-dialog modal-sm">
    <div class="modal-content">

      <!-- 模態(tài)框頭部 -->
      <div class="modal-header">
        <h4 class="modal-title">模態(tài)框標題</h4>
        <button type="button" class="btn-close" data-bs-dismiss="modal"></button>
      </div>

      <!-- 模態(tài)框內(nèi)容 -->
      <div class="modal-body">
        模態(tài)框內(nèi)容..
      </div>

      <!-- 模態(tài)框底部 -->
      <div class="modal-footer">
        <button type="button" class="btn btn-danger" data-bs-dismiss="modal">關閉</button>
      </div>

    </div>
  </div>
</div>

</body>
</html>

大模態(tài)框

<!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="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"  rel="external nofollow"  rel="external nofollow"  rel="external nofollow"  rel="external nofollow" ></script>
</head>
<body>

<div class="container mt-3">
  <h3>模態(tài)框實例</h3>
  <p>點擊按鈕打開模態(tài)框</p>
  
  <button type="button" class="btn btn-primary" data-bs-toggle="modal" data-bs-target="#myModal">
    打開模態(tài)框
  </button>
</div>

<!-- 模態(tài)框 -->
<div class="modal fade" id="myModal">
  <div class="modal-dialog modal-lg">
    <div class="modal-content">

      <!-- 模態(tài)框頭部 -->
      <div class="modal-header">
        <h4 class="modal-title">模態(tài)框標題</h4>
        <button type="button" class="btn-close" data-bs-dismiss="modal"></button>
      </div>

      <!-- 模態(tài)框內(nèi)容 -->
      <div class="modal-body">
        模態(tài)框內(nèi)容..
      </div>

      <!-- 模態(tài)框底部 -->
      <div class="modal-footer">
        <button type="button" class="btn btn-danger" data-bs-dismiss="modal">關閉</button>
      </div>

    </div>
  </div>
</div>

</body>
</html>

 超大模態(tài)框

<!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="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"  rel="external nofollow"  rel="external nofollow"  rel="external nofollow"  rel="external nofollow" ></script>
</head>
<body>

<div class="container mt-3">
  <h3>模態(tài)框實例</h3>
  <p>點擊按鈕打開模態(tài)框</p>
  
  <button type="button" class="btn btn-primary" data-bs-toggle="modal" data-bs-target="#myModal">
    打開模態(tài)框
  </button>
</div>

<!-- 模態(tài)框 -->
<div class="modal fade" id="myModal">
  <div class="modal-dialog modal-xl">
    <div class="modal-content">

      <!-- 模態(tài)框頭部 -->
      <div class="modal-header">
        <h4 class="modal-title">模態(tài)框標題</h4>
        <button type="button" class="btn-close" data-bs-dismiss="modal"></button>
      </div>

      <!-- 模態(tài)框內(nèi)容 -->
      <div class="modal-body">
        模態(tài)框內(nèi)容..
      </div>

      <!-- 模態(tài)框底部 -->
      <div class="modal-footer">
        <button type="button" class="btn btn-danger" data-bs-dismiss="modal">關閉</button>
      </div>

    </div>
  </div>
</div>

</body>
</html>

全屏幕顯示

使用 .modal-fullscreen 類可以讓模態(tài)框全屏幕顯示:

<!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="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"  rel="external nofollow"  rel="external nofollow"  rel="external nofollow"  rel="external nofollow" ></script>
</head>
<body>

<div class="container mt-3">
  <h3>模態(tài)框實例</h3>
  <p>點擊按鈕打開模態(tài)框</p>
  
  <button type="button" class="btn btn-primary" data-bs-toggle="modal" data-bs-target="#myModal">
    打開模態(tài)框
  </button>
</div>

<!-- 模態(tài)框 -->
<div class="modal fade" id="myModal">
  <div class="modal-dialog modal-fullscreen">
    <div class="modal-content">

      <!-- 模態(tài)框頭部 -->
      <div class="modal-header">
        <h4 class="modal-title">模態(tài)框標題</h4>
        <button type="button" class="btn-close" data-bs-dismiss="modal"></button>
      </div>

      <!-- 模態(tài)框內(nèi)容 -->
      <div class="modal-body">
        模態(tài)框內(nèi)容..
      </div>

      <!-- 模態(tài)框底部 -->
      <div class="modal-footer">
        <button type="button" class="btn btn-danger" data-bs-dismiss="modal">關閉</button>
      </div>

    </div>
  </div>
</div>

</body>
</html>

使用 .modal-fullscreen-*-* 類可以控制在什么尺寸下全屏幕顯示:

描述
.modal-fullscreen-sm-down 576px 以下尺寸全屏幕顯示
.modal-fullscreen-md-down 768px 以下尺寸全屏幕顯示
.modal-fullscreen-lg-down 992px 以下尺寸全屏幕顯示
.modal-fullscreen-xl-down 1200px 以下尺寸全屏幕顯示
.modal-fullscreen-xxl-down 1400px 以下尺寸全屏幕顯示

模態(tài)框居中顯示

使用 .modal-dialog-centered 類可以設置模態(tài)框水平和垂直方向都居中顯示:

<!DOCTYPE html>
<html lang="en">
<head>
  <title>Bootstrap Example</title>
  <meta charset="utf-8">
  <meta name="viewport" content="width=device-width, initial-scale=1">
  <link  rel="external nofollow" target="_blank"  rel="stylesheet">
  <script src="https://cdn.jsdelivr.net/npm/bootstrap@5.1.1/dist/js/bootstrap.bundle.min.js" rel="external nofollow" ></script>
</head>
<body>

<div class="container mt-3">
  <h3>水平和垂直方向都居中顯示</h3>
  <p>點擊按鈕打開模態(tài)框</p>
  
  <button type="button" class="btn btn-primary" data-bs-toggle="modal" data-bs-target="#myModal">
    打開模態(tài)框
  </button>
</div>
 
<!-- 模態(tài)框 -->
<div class="modal" id="myModal">
  <div class="modal-dialog modal-dialog-centered">
    <div class="modal-content">
 
      <!-- 模態(tài)框頭部 -->
      <div class="modal-header">
        <h4 class="modal-title">模態(tài)框標題</h4>
        <button type="button" class="btn-close" data-bs-dismiss="modal"></button>
      </div>
 
      <!-- 模態(tài)框內(nèi)容 -->
      <div class="modal-body">
        模態(tài)框內(nèi)容..
      </div>
 
      <!-- 模態(tài)框底部 -->
      <div class="modal-footer">
        <button type="button" class="btn btn-danger" data-bs-dismiss="modal">關閉</button>
      </div>
 
    </div>
  </div>
</div>


</body>
</html>

模態(tài)框滾動條

默認情況下模態(tài)框如果包含很多內(nèi)容,頁面會自動生成一個滾動,模態(tài)框隨著頁面的滾動而滾動:

<!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="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"  rel="external nofollow"  rel="external nofollow"  rel="external nofollow"  rel="external nofollow" ></script>
</head>
<body>

<div class="container mt-3">
  <h3>模態(tài)框滾動條實例</h3>
  <p>點擊按鈕打開模態(tài)框</p>
  
  <button type="button" class="btn btn-primary" data-bs-toggle="modal" data-bs-target="#myModal">
    打開模態(tài)框
  </button>
</div>

<!-- 模態(tài)框 -->
<div class="modal" id="myModal">
  <div class="modal-dialog">
    <div class="modal-content">

      <!-- 模態(tài)框頭部 -->
      <div class="modal-header">
        <h4 class="modal-title">模態(tài)框標題</h4>
        <button type="button" class="btn-close" data-bs-dismiss="modal"></button>
      </div>

      <!-- 模態(tài)框內(nèi)容 -->
      <div class="modal-body">
        模態(tài)框內(nèi)容..<br />
		模態(tài)框內(nèi)容..<br />
		模態(tài)框內(nèi)容..<br />
		模態(tài)框內(nèi)容..<br />
		模態(tài)框內(nèi)容..<br />
		模態(tài)框內(nèi)容..<br />
		模態(tài)框內(nèi)容..<br />
		模態(tài)框內(nèi)容..<br />
		模態(tài)框內(nèi)容..<br />
		模態(tài)框內(nèi)容..<br />
		模態(tài)框內(nèi)容..<br />
		模態(tài)框內(nèi)容..<br />
		模態(tài)框內(nèi)容..<br />
		模態(tài)框內(nèi)容..<br />
		模態(tài)框內(nèi)容..<br />
		模態(tài)框內(nèi)容..<br />
		模態(tài)框內(nèi)容..<br />
		模態(tài)框內(nèi)容..<br />
		模態(tài)框內(nèi)容..<br />
		模態(tài)框內(nèi)容..<br />
		模態(tài)框內(nèi)容..<br />
		模態(tài)框內(nèi)容..<br />
		模態(tài)框內(nèi)容..<br />
		模態(tài)框內(nèi)容..<br />
      </div>

      <!-- 模態(tài)框底部 -->
      <div class="modal-footer">
        <button type="button" class="btn btn-danger" data-bs-dismiss="modal">關閉</button>
      </div>

    </div>
  </div>
</div>

</body>
</html>

如果我們只想在模態(tài)框里頭設置一個滾動條,可以使用 .modal-dialog-scrollable 類:

<!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="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"  rel="external nofollow"  rel="external nofollow"  rel="external nofollow"  rel="external nofollow" ></script>
</head>
<body>

<div class="container mt-3">
  <h3>模態(tài)框滾動條實例</h3>
  <p>點擊按鈕打開模態(tài)框</p>
  
  <button type="button" class="btn btn-primary" data-bs-toggle="modal" data-bs-target="#myModal">
    打開模態(tài)框
  </button>
</div>

<!-- 模態(tài)框 -->
<div class="modal" id="myModal">
  <div class="modal-dialog modal-dialog-scrollable">
    <div class="modal-content">

      <!-- 模態(tài)框頭部 -->
      <div class="modal-header">
        <h4 class="modal-title">模態(tài)框標題</h4>
        <button type="button" class="btn-close" data-bs-dismiss="modal"></button>
      </div>

      <!-- 模態(tài)框內(nèi)容 -->
      <div class="modal-body">
        模態(tài)框內(nèi)容..<br />
		模態(tài)框內(nèi)容..<br />
		模態(tài)框內(nèi)容..<br />
		模態(tài)框內(nèi)容..<br />
		模態(tài)框內(nèi)容..<br />
		模態(tài)框內(nèi)容..<br />
		模態(tài)框內(nèi)容..<br />
		模態(tài)框內(nèi)容..<br />
		模態(tài)框內(nèi)容..<br />
		模態(tài)框內(nèi)容..<br />
		模態(tài)框內(nèi)容..<br />
		模態(tài)框內(nèi)容..<br />
		模態(tài)框內(nèi)容..<br />
		模態(tài)框內(nèi)容..<br />
		模態(tài)框內(nèi)容..<br />
		模態(tài)框內(nèi)容..<br />
		模態(tài)框內(nèi)容..<br />
		模態(tài)框內(nèi)容..<br />
		模態(tài)框內(nèi)容..<br />
		模態(tài)框內(nèi)容..<br />
		模態(tài)框內(nèi)容..<br />
		模態(tài)框內(nèi)容..<br />
		模態(tài)框內(nèi)容..<br />
		模態(tài)框內(nèi)容..<br />
      </div>

      <!-- 模態(tài)框底部 -->
      <div class="modal-footer">
        <button type="button" class="btn btn-danger" data-bs-dismiss="modal">關閉</button>
      </div>

    </div>
  </div>
</div>

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

掃描二維碼

下載編程獅App

公眾號
微信公眾號

編程獅公眾號