Bootstrap4中,信息提示框功能的實現(xiàn)非常容易。而且可以使用各種類實現(xiàn)提示框的顏色、動畫等樣式。
提示框顏色:
提示框可以使用 .alert 類, 后面加上 .alert-success, .alert-info, .alert-warning, .alert-danger, .alert-primary, .alert-secondary, .alert-light 或 .alert-dark 類來實現(xiàn)顏色的變化:
<!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" 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">
<h2>提示框</h2>
<p>提示框可以使用 .alert 類, 后面加上指定特定意義的顏色類來實現(xiàn):</p>
<div class="alert alert-success">
<strong>成功!</strong> 指定操作成功提示信息。
</div>
<div class="alert alert-info">
<strong>信息!</strong> 請注意這個信息。
</div>
<div class="alert alert-warning">
<strong>警告!</strong> 設(shè)置警告信息。
</div>
<div class="alert alert-danger">
<strong>錯誤!</strong> 失敗的操作
</div>
<div class="alert alert-primary">
? <strong>首選!</strong> 這是一個重要的操作信息。
</div>
<div class="alert alert-secondary">
? <strong>次要的!</strong> 顯示一些不重要的信息。
</div>
<div class="alert alert-dark">
? <strong>深灰色!</strong> 深灰色提示框。
</div>
<div class="alert alert-light">
? <strong>淺灰色!</strong>淺灰色提示框。
</div>
</div>
</body>
</html>
提示框添加鏈接
提示框中在鏈接的標簽上添加 alert-link 類來設(shè)置匹配提示框顏色的鏈接:
<!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" 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">
<h2>提示框添加鏈接</h2>
<p>提示框中在鏈接的標簽上添加 alert-link 類來設(shè)置匹配提示框顏色的鏈接:</p>
<div class="alert alert-success">
<strong>成功!</strong> 你應該認真閱讀 <a href="#" class="alert-link">這條信息</a>。
</div>
<div class="alert alert-info">
<strong>信息!</strong> 你應該認真閱讀 <a href="#" class="alert-link">這條信息</a>。
</div>
<div class="alert alert-warning">
<strong>警告!</strong> 你應該認真閱讀 <a href="#" class="alert-link">這條信息</a>。
</div>
<div class="alert alert-danger">
<strong>錯誤!</strong> 你應該認真閱讀 <a href="#" class="alert-link">這條信息</a>。
</div>
<div class="alert alert-primary">
<strong>首選!</strong> 你應該認真閱讀 <a href="#" class="alert-link">這條信息</a>。
</div>
<div class="alert alert-secondary">
<strong>次要的!</strong> 你應該認真閱讀 <a href="#" class="alert-link">這條信息</a>。
</div>
<div class="alert alert-dark">
<strong>深灰色!</strong>你應該認真閱讀 <a href="#" class="alert-link">這條信息</a>。
</div>
<div class="alert alert-light">
<strong>灰色!</strong> 你應該認真閱讀 <a href="#" class="alert-link">這條信息</a>。
</div>
</div>
</body>
</html>
關(guān)閉提示框
我們可以在提示框中的 div 中添加 .alert-dismissable 類,然后在關(guān)閉按鈕的鏈接上添加 class="close" 和 data-dismiss="alert" 類來設(shè)置提示框的關(guān)閉操作。
<!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" 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">
<h2>關(guān)閉提示框</h2>
<p>我們可以在提示框中的 div 中添加 .alert-dismissable 類,然后在關(guān)閉按鈕的鏈接上添加 class="close" 和 data-dismiss="alert" 類來設(shè)置提示框的關(guān)閉操作。</p>
<div class="alert alert-success alert-dismissable">
<button type="button" class="close" data-dismiss="alert">×</button>
<strong>成功!</strong> 指定操作成功提示信息。
</div>
<div class="alert alert-info alert-dismissable">
<button type="button" class="close" data-dismiss="alert">×</button>
<strong>信息!</strong> 請注意這個信息。
</div>
<div class="alert alert-warning alert-dismissable">
<button type="button" class="close" data-dismiss="alert">×</button>
<strong>警告!</strong> 設(shè)置警告信息。
</div>
<div class="alert alert-danger alert-dismissable">
<button type="button" class="close" data-dismiss="alert">×</button>
<strong>錯誤!</strong> 失敗的操作。
</div>
<div class="alert alert-primary alert-dismissable">
<button type="button" class="close" data-dismiss="alert">×</button>
<strong>首選!</strong> 這是一個重要的操作信息。
</div>
<div class="alert alert-secondary alert-dismissable">
<button type="button" class="close" data-dismiss="alert">×</button>
<strong>次要的!</strong> 顯示一些不重要的信息。
</div>
<div class="alert alert-dark alert-dismissable">
<button type="button" class="close" data-dismiss="alert">×</button>
<strong>深灰色!</strong> 深灰色提示框。
</div>
<div class="alert alert-light alert-dismissable">
<button type="button" class="close" data-dismiss="alert">×</button>
<strong>淺灰色!</strong>淺灰色提示框。
</div>
</div>
</body>
</html>
提示: × (×) 是 HTML 實體字符,來表示關(guān)閉操作,而不是字母 "x"。
提示框動畫
.fade 和 .show 類用于設(shè)置提示框在關(guān)閉時的淡出和淡入效果:
<!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" 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">
<h2>提示框動畫</h2>
<p>.fade 和 .show 類用于設(shè)置提示框在關(guān)閉時的淡出和淡入效果:</p>
<div class="alert alert-success alert-dismissable fade show">
<button type="button" class="close" data-dismiss="alert">×</button>
<strong>成功!</strong> 指定操作成功提示信息。
</div>
<div class="alert alert-info alert-dismissable fade show">
<button type="button" class="close" data-dismiss="alert">×</button>
<strong>信息!</strong> 請注意這個信息。
</div>
<div class="alert alert-warning alert-dismissable fade show">
<button type="button" class="close" data-dismiss="alert">×</button>
<strong>警告!</strong> 設(shè)置警告信息。
</div>
<div class="alert alert-danger alert-dismissable fade show">
<button type="button" class="close" data-dismiss="alert">×</button>
<strong>錯誤!</strong> 失敗的操作。
</div>
<div class="alert alert-primary alert-dismissable fade show">
<button type="button" class="close" data-dismiss="alert">×</button>
<strong>首選!</strong> 這是一個重要的操作信息。
</div>
<div class="alert alert-secondary alert-dismissable fade show">
<button type="button" class="close" data-dismiss="alert">×</button>
<strong>次要的!</strong> 顯示一些不重要的信息。
</div>
<div class="alert alert-dark alert-dismissable fade show">
<button type="button" class="close" data-dismiss="alert">×</button>
<strong>深灰色!</strong> 深灰色提示框。
</div>
<div class="alert alert-light alert-dismissable fade show">
<button type="button" class="close" data-dismiss="alert">×</button>
<strong>淺灰色!</strong>淺灰色提示框。
</div>
</div>
</body>
</html>
更多建議: