- Bootstrap 教程
- Bootstrap - 首頁
- Bootstrap - 概述
- Bootstrap - 環境搭建
- Bootstrap - 從右到左 (RTL)
- Bootstrap - CSS 變數
- Bootstrap - 顏色模式
- Bootstrap 佈局
- Bootstrap - 斷點
- Bootstrap - 容器
- Bootstrap - 網格系統
- Bootstrap - 列
- Bootstrap - 間距
- Bootstrap - 實用程式
- Bootstrap - CSS 網格
- Bootstrap 元件
- Bootstrap - 手風琴
- Bootstrap - 警報
- Bootstrap - 徽章
- Bootstrap - 麵包屑
- Bootstrap - 按鈕
- Bootstrap - 按鈕組
- Bootstrap - 卡片
- Bootstrap - 走馬燈
- Bootstrap - 關閉按鈕
- Bootstrap - 摺疊
- Bootstrap - 下拉選單
- Bootstrap - 列表組
- Bootstrap - 模態框
- Bootstrap - 導航欄
- Bootstrap - 導航和選項卡
- Bootstrap - 側邊欄
- Bootstrap - 分頁
- Bootstrap - 佔位符
- Bootstrap - 氣泡提示
- Bootstrap - 進度條
- Bootstrap - 滾動監聽
- Bootstrap - 載入動畫
- Bootstrap - 吐司提示框
- Bootstrap - 工具提示
- Bootstrap 表單
- Bootstrap - 表單
- Bootstrap - 表單控制元件
- Bootstrap - 選擇框
- Bootstrap - 複選框和單選框
- Bootstrap - 範圍滑塊
- Bootstrap - 輸入組
- Bootstrap - 浮動標籤
- Bootstrap - 佈局
- Bootstrap - 驗證
- Bootstrap 輔助類
- Bootstrap - 清除浮動
- Bootstrap - 顏色和背景
- Bootstrap - 彩色連結
- Bootstrap - 焦點環
- Bootstrap - 圖示連結
- Bootstrap - 定位
- Bootstrap - 寬高比
- Bootstrap - 堆疊
- Bootstrap - 拉伸連結
- Bootstrap - 文字截斷
- Bootstrap - 垂直線
- Bootstrap - 視覺隱藏
- Bootstrap 實用程式
- Bootstrap - 背景
- Bootstrap - 邊框
- Bootstrap - 顏色
- Bootstrap - 顯示
- Bootstrap - Flexbox
- Bootstrap - 浮動
- Bootstrap - 互動
- Bootstrap - 連結
- Bootstrap - 物件適配
- Bootstrap - 不透明度
- Bootstrap - 溢位
- Bootstrap - 定位
- Bootstrap - 陰影
- Bootstrap - 尺寸
- Bootstrap - 間距
- Bootstrap - 文字
- Bootstrap - 垂直對齊
- Bootstrap - 可見性
- Bootstrap 演示
- Bootstrap - 網格演示
- Bootstrap - 按鈕演示
- Bootstrap - 導航演示
- Bootstrap - 部落格演示
- Bootstrap - 滑塊演示
- Bootstrap - 走馬燈演示
- Bootstrap - 頁首演示
- Bootstrap - 頁尾演示
- Bootstrap - 英雄區演示
- Bootstrap - 特色區演示
- Bootstrap - 側邊欄演示
- Bootstrap - 下拉選單演示
- Bootstrap - 列表組演示
- Bootstrap - 模態框演示
- Bootstrap - 徽章演示
- Bootstrap - 麵包屑演示
- Bootstrap - 超大標題演示
- Bootstrap - 粘性頁尾演示
- Bootstrap - 相簿演示
- Bootstrap - 登入演示
- Bootstrap - 定價演示
- Bootstrap - 結賬演示
- Bootstrap - 產品演示
- Bootstrap - 封面演示
- Bootstrap - 儀表盤演示
- Bootstrap - 粘性頁尾導航欄演示
- Bootstrap - 砌體佈局演示
- Bootstrap - 初始模板演示
- Bootstrap - 相簿 RTL 演示
- Bootstrap - 結賬 RTL 演示
- Bootstrap - 走馬燈 RTL 演示
- Bootstrap - 部落格 RTL 演示
- Bootstrap - 儀表盤 RTL 演示
- Bootstrap 有用資源
- Bootstrap - 問答
- Bootstrap - 快速指南
- Bootstrap - 有用資源
- Bootstrap - 討論
Bootstrap - 吐司提示框
本章討論吐司提示框元件。吐司提示框類似於警報訊息,輕量級且可自定義。吐司提示框是為使用者提供響應式且不顯眼的通知的有用工具。
Bootstrap 中的吐司提示框用於在螢幕底部或頂部顯示非阻塞通知。
它們可以提供反饋或提醒使用者某些事件或操作,而不會打斷他們當前的任務。
吐司提示框可以包含文字、影像或任何其他 HTML 內容,並且可以自定義以適合網站或應用程式的設計。
它們也可以由使用者關閉,或者在自動消失之前設定持續時間。
您必須自己初始化吐司提示框,因為出於效能原因,它們是可選的。
如果您未指定autohide: false,則吐司提示框會自動隱藏。
吐司提示框元件的動畫效果取決於prefers-reduced-motion媒體查詢。
建議為吐司提示框新增標題和正文,使其更具擴充套件性和可預測性。
您需要一個單獨的元素來包含您的吐司提示框,並且必須有一個關閉按鈕。
基本吐司提示框
為了建立一個基本的吐司提示框,您需要使用.toast類,並新增.toast-header來提供標題,以及.toast-body來新增內容。
讓我們來看一個基本吐司提示框的示例
示例
您可以使用編輯和執行選項編輯並嘗試執行此程式碼。
<!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="stylesheet" href="https://cdn.jsdelivr.net/npm/bootstrap@4.6.2/dist/css/bootstrap.min.css">
<link href="https://cdn.jsdelivr.net/npm/bootstrap@5.3.0-alpha3/dist/css/bootstrap.min.css" rel="stylesheet">
<script src="https://cdn.jsdelivr.net/npm/bootstrap@5.3.0-alpha3/dist/js/bootstrap.bundle.min.js"></script>
</head>
<body>
<div class="container">
<h3>Toast Example</h3>
<p>A toast is like an alert box that is shown.</p>
<div class="toast fade show" role="alert" aria-live="assertive" aria-atomic="true">
<div class="toast-header">
<small>A toast without an event</small>
<button type="button" class="btn-close" data-bs-dismiss="toast" aria-label="Close"></button>
</div>
<div class="toast-body">
Toast is shown without any event like on a click of a button.
</div>
</div>
</div>
</body>
</html>
過去,.hide類會自動新增以使用display:none完全隱藏吐司提示框,而不是使用opacity:0。現在,不再需要這樣做。
以下 JavaScript 程式碼用於觸發吐司提示框
const toastTrigger = document.getElementById('liveToastBtn')
const toastLiveExample = document.getElementById('liveToast')
if (toastTrigger) {
const toastBootstrap = bootstrap.Toast.getOrCreateInstance(toastLiveExample)
toastTrigger.addEventListener('click', () => {
toastBootstrap.show()
})
}
或
$(document).ready(function() {
$('#liveToast').click(function() {
$('.toast').toast({
animation: false,
delay: 3000
});
$('.toast').toast('show');
});
});
在您的 html 中新增以下連結
<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.4.0/jquery.min.js"></script>
即時吐司提示框
以下是在您的頁面上可以檢視的即時吐司提示框的示例
示例
您可以使用編輯和執行選項編輯並嘗試執行此程式碼。
<!DOCTYPE html>
<html lang="en">
<head>
<title>Bootstrap - Toasts</title>
<meta charset="UTF-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<link href="https://cdn.jsdelivr.net/npm/bootstrap@5.3.0-alpha3/dist/css/bootstrap.min.css" rel="stylesheet">
<script src="https://cdn.jsdelivr.net/npm/bootstrap@5.3.0-alpha3/dist/js/bootstrap.bundle.min.js"></script>
<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.4.0/jquery.min.js"></script>
</head>
<body class="container mx-auto mt-2">
<h4>View Live toast</h4>
<button type="button" class="btn btn-success" id="liveToast">View toast live</button>
<div class="toast-container position-fixed bottom-0 end-0 p-4">
<div id="liveToast" class="toast" role="alert" aria-live="assertive" aria-atomic="true">
<div class="toast-header">
<strong class="me-auto">Live Toast</strong>
<small>Now</small>
<button type="button" class="btn-close" data-bs-dismiss="toast" aria-label="Close"></button>
</div>
<div class="toast-body">
This is a live toast and placed at the bottom of the page.
</div>
</div>
</div>
<script>
$(document).ready(function() {
$('#liveToast').click(function() {
$('.toast').toast({
animation: false,
delay: 3000
});
$('.toast').toast('show');
});
});
</script>
</body>
</html>
半透明吐司提示框
吐司提示框有點半透明,並與它們出現的頁面融合。
讓我們來看一個例子
示例
您可以使用編輯和執行選項編輯並嘗試執行此程式碼。
<!DOCTYPE html>
<html lang="en">
<head>
<title>Bootstrap - Toasts</title>
<meta charset="UTF-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<link href="https://cdn.jsdelivr.net/npm/bootstrap@5.3.0-alpha3/dist/css/bootstrap.min.css" rel="stylesheet">
<script src="https://cdn.jsdelivr.net/npm/bootstrap@5.3.0-alpha3/dist/js/bootstrap.bundle.min.js"></script>
<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.4.0/jquery.min.js"></script>
</head>
<body class=" container mx-auto mt-2">
<h4>Translucent toast</h4>
<button type="button" class="btn btn-success" id="viewToast">Click for toast</button>
<div class="toast-container position-top top-0">
<div id="viewToast" class="toast" role="alert" aria-live="assertive" aria-atomic="true">
<div class="toast-header">
<strong class="me-auto">Translucent Toast</strong>
<small class="text-body-secondary">First toast</small>
<button type="button" class="btn-close" data-bs-dismiss="toast" aria-label="Close"></button>
</div>
<div class="toast-body">
This is a translucent toast and placed at the top of the page.
</div>
</div>
</div>
<script>
$(document).ready(function() {
$('#viewToast').click(function() {
$('.toast').toast({
animation: false,
delay: 3000
});
$('.toast').toast('show');
});
});
</script>
</body>
</html>
吐司提示框堆疊
吐司提示框可以透過包裝它們來堆疊在吐司提示框容器中。堆疊時,吐司提示框之間會新增垂直空間。
讓我們來看一個例子
示例
您可以使用編輯和執行選項編輯並嘗試執行此程式碼。
<!DOCTYPE html>
<html lang="en">
<head>
<title>Bootstrap - Toasts</title>
<meta charset="UTF-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<link href="https://cdn.jsdelivr.net/npm/bootstrap@5.3.0-alpha3/dist/css/bootstrap.min.css" rel="stylesheet">
<script src="https://cdn.jsdelivr.net/npm/bootstrap@5.3.0-alpha3/dist/js/bootstrap.bundle.min.js"></script>
<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.4.0/jquery.min.js"></script>
</head>
<body class=" container mx-auto mt-2">
<h4>Stacking of toasts</h4>
<button type="button" class="btn btn-success" id="viewToast">View stacked toasts</button>
<!-- First Toast -->
<div class="toast-container position-top top-0">
<div id="viewToast" class="toast" role="alert" aria-live="assertive" aria-atomic="true">
<div class="toast-header">
<strong class="me-auto">Toast 1</strong>
<small class="text-body-secondary">First toast</small>
<button type="button" class="btn-close" data-bs-dismiss="toast" aria-label="Close"></button>
</div>
<div class="toast-body text-bg-warning">
This is toast 1 and is placed at the top of the page.
</div>
</div>
<!-- Second Toast -->
<div id="viewToast" class="toast" role="alert" aria-live="assertive" aria-atomic="true">
<div class="toast-header">
<strong class="me-auto">Toast 2</strong>
<small class="text-body-secondary">Second toast</small>
<button type="button" class="btn-close" data-bs-dismiss="toast" aria-label="Close"></button>
</div>
<div class="toast-body text-bg-info">
This is toast 2 and is placed below toast 1.
</div>
</div>
</div>
<script>
$(document).ready(function() {
$('#viewToast').click(function() {
$('.toast').toast({
animation: false,
delay: 3000
});
$('.toast').toast('show');
});
});
</script>
</body>
</html>
自定義內容
吐司提示框可以透過移除子元件、新增一些實用程式甚至新增您自己的標記來進行自定義。
您可以新增來自 Bootstrap 圖示的自定義圖示,或者移除.toast-header,向內容中新增按鈕等。
讓我們來看一個自定義吐司提示框的示例,其中在吐司提示框正文中添加了兩個按鈕
示例
您可以使用編輯和執行選項編輯並嘗試執行此程式碼。
<!DOCTYPE html>
<html lang="en">
<head>
<title>Bootstrap - Toasts</title>
<meta charset="UTF-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<link href="https://cdn.jsdelivr.net/npm/bootstrap@5.3.0-alpha3/dist/css/bootstrap.min.css" rel="stylesheet">
<script src="https://cdn.jsdelivr.net/npm/bootstrap@5.3.0-alpha3/dist/js/bootstrap.bundle.min.js"></script>
<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.4.0/jquery.min.js"></script>
</head>
<body class=" container mx-auto mt-2">
<h4>Customization of toasts</h4>
<!-- Button to trigger the toasts -->
<button type="button" class="btn btn-primary" id="myBtn">View customized toast</button>
<div class="toast-container">
<div class="toast">
<div class="toast-header bg-secondary-subtle">
<strong>Thanks</strong>
</div>
<div class="toast-body text-bg-secondary">Buttons are added to the toast.</div>
<button type="button" class="btn btn-success btn-sm">Submit</button>
<button type="button" class="btn btn-danger btn-sm" data-bs-dismiss="toast" aria-label="Close">Cancel</button>
</div>
</div>
<script>
$(document).ready(function() {
$('#myBtn').click(function() {
$('.toast').toast({
animation: false,
delay: 3000
});
$('.toast').toast('show');
});
});
</script>
</body>
</html>
顏色方案
讓我們來看一個將顏色方案新增到吐司提示框的示例
示例
您可以使用編輯和執行選項編輯並嘗試執行此程式碼。
<!DOCTYPE html>
<html lang="en">
<head>
<title>Bootstrap - Toasts</title>
<meta charset="UTF-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<link href="https://cdn.jsdelivr.net/npm/bootstrap@5.3.0-alpha3/dist/css/bootstrap.min.css" rel="stylesheet">
<script src="https://cdn.jsdelivr.net/npm/bootstrap@5.3.0-alpha3/dist/js/bootstrap.bundle.min.js"></script>
<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.4.0/jquery.min.js"></script>
</head>
<body class=" container mx-auto mt-2">
<h4>Color scheme</h4>
<p>Color scheme added to the toast</p>
<!-- Button to trigger the toasts -->
<button type="button" class="btn btn-primary" id="myBtn">Click for toast</button>
<div class="toast-container">
<div class="toast">
<div class="toast-header bg-warning-subtle">
<strong>Toast Header</strong>
</div>
<div class="toast-body text-bg-success">Color scheme is added to the header and body of the toast.</div>
</div>
</div>
<script>
$(document).ready(function() {
$('#myBtn').click(function() {
$('.toast').toast({
animation: false,
delay: 3000
});
$('.toast').toast('show');
});
});
</script>
</body>
</html>
吐司提示框的位置
吐司提示框位置功能用於設定吐司提示框在網頁上的位置。以下是吐司提示框位置的可用選項
.position-absolute - 用於相對於其最近的已定位祖先定位元素。
.top-* - 設定吐司提示框頂部對齊的位置。
.bottom-* - 設定吐司提示框底部對齊的位置。
.start-* - 設定吐司提示框起始對齊的位置。
.end-* - 設定吐司提示框結束對齊的位置。
位置類的取值範圍從0到50。
讓我們來看一個位置類的示例
示例
您可以使用編輯和執行選項編輯並嘗試執行此程式碼。
<!DOCTYPE html>
<html lang="en">
<head>
<title>Bootstrap - Toasts</title>
<meta charset="UTF-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<link href="https://cdn.jsdelivr.net/npm/bootstrap@5.3.0-alpha3/dist/css/bootstrap.min.css" rel="stylesheet">
<script src="https://cdn.jsdelivr.net/npm/bootstrap@5.3.0-alpha3/dist/js/bootstrap.bundle.min.js"></script>
<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.4.0/jquery.min.js"></script>
</head>
<body class=" container mx-auto mt-2">
<h4>Placement - Toasts</h4>
<p>Set the position of the toasts on webpage</p>
<!-- Button to trigger the toasts -->
<button type="button" class="btn btn-primary" id="myBtn">Click for toast</button>
<!--Top left -->
<div class="toast-container top-0 start-0">
<div class="toast">
<div class="toast-header">
<strong class="me-auto">Toast 1</strong>
<small class="text-body-secondary">Toast top left </small>
<button type="button" class="btn-close" data-bs-dismiss="toast" aria-label="Close"></button>
</div>
<div class="toast-body">
Position at Top left.
</div>
</div>
</div>
<!--Top center-->
<div class="toast-container top-0 start-50 translate-middle-x">
<div class="toast">
<div class="toast-header">
<strong class="me-auto">Toast 2</strong>
<small class="text-body-secondary">Toast at top center</small>
<button type="button" class="btn-close" data-bs-dismiss="toast" aria-label="Close"></button>
</div>
<div class="toast-body">
Position at Top Center.
</div>
</div>
</div>
<!--Top right -->
<div class="toast-container top-0 end-0">
<div class="toast">
<div class="toast-header">
<strong class="me-auto">Toast 3</strong>
<small class="text-body-secondary">Toast at top right</small>
<button type="button" class="btn-close" data-bs-dismiss="toast" aria-label="Close"></button>
</div>
<div class="toast-body">
Position at Top Right.
</div>
</div>
</div>
<!--Middle left-->
<div class="toast-container top-50 start-0 translate-middle-y">
<div class="toast">
<div class="toast-header">
<strong class="me-auto">Toast 4</strong>
<small class="text-body-secondary">Toast at middle left</small>
<button type="button" class="btn-close" data-bs-dismiss="toast" aria-label="Close"></button>
</div>
<div class="toast-body">
Position at middle left.
</div>
</div>
</div>
<!--Middle left-->
<div class="toast-container top-50 start-50 translate-middle">
<div class="toast">
<div class="toast-header">
<strong class="me-auto">Toast 5</strong>
<small class="text-body-secondary">Toast at middle center</small>
<button type="button" class="btn-close" data-bs-dismiss="toast" aria-label="Close"></button>
</div>
<div class="toast-body">
Position at middle center.
</div>
</div>
</div>
<!--Middle right-->
<div class="toast-container top-50 end-0 translate-middle-y">
<div class="toast">
<div class="toast-header">
<strong class="me-auto">Toast 6</strong>
<small class="text-body-secondary">Toast at middle right</small>
<button type="button" class="btn-close" data-bs-dismiss="toast" aria-label="Close"></button>
</div>
<div class="toast-body">
Position at middle right.
</div>
</div>
</div>
<!--Bottom left-->
<div class="toast-container bottom-0 start-0">
<div class="toast">
<div class="toast-header">
<strong class="me-auto">Toast 7</strong>
<small class="text-body-secondary">Toast at bottom left</small>
<button type="button" class="btn-close" data-bs-dismiss="toast" aria-label="Close"></button>
</div>
<div class="toast-body">
Position at bottom left.
</div>
</div>
</div>
<!--Bottom center-->
<div class="toast-container bottom-0 start-50 translate-middle-x">
<div class="toast">
<div class="toast-header">
<strong class="me-auto">Toast 8</strong>
<small class="text-body-secondary">Toast at bottom center</small>
<button type="button" class="btn-close" data-bs-dismiss="toast" aria-label="Close"></button>
</div>
<div class="toast-body">
Position at bottom center.
</div>
</div>
</div>
<!--Bottom right-->
<div class="toast-container bottom-0 end-0">
<div class="toast">
<div class="toast-header">
<strong class="me-auto">Toast 9</strong>
<small class="text-body-secondary">Toast at bottom right</small>
<button type="button" class="btn-close" data-bs-dismiss="toast" aria-label="Close"></button>
</div>
<div class="toast-body">
Position at bottom right.
</div>
</div>
</div>
<script>
$(document).ready(function() {
$('#myBtn').click(function() {
$('.toast').toast({
animation: false,
delay: 3000
});
$('.toast').toast('show');
});
});
</script>
</body>
</html>
在處理連續生成許多通知的系統時,您可以使用一個包裝元素來堆疊這些通知。請參閱以下示例
示例
您可以使用編輯和執行選項編輯並嘗試執行此程式碼。
<!DOCTYPE html>
<html lang="en">
<head>
<title>Bootstrap - Toasts</title>
<meta charset="UTF-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<link href="https://cdn.jsdelivr.net/npm/bootstrap@5.3.0-alpha3/dist/css/bootstrap.min.css" rel="stylesheet">
<script src="https://cdn.jsdelivr.net/npm/bootstrap@5.3.0-alpha3/dist/js/bootstrap.bundle.min.js"></script>
<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.4.0/jquery.min.js"></script>
</head>
<body class="p-3 m-0 border-0 bd-example m-0 border-0 bd-example-toasts p-0">
<div aria-live="polite" aria-atomic="true" class="position-relative">
<div class="toast-container top-0 start-0 p-3">
<!-- Toasts within the container -->
<div class="toast fade show" role="alert" aria-live="assertive" aria-atomic="true">
<div class="toast-header">
<strong class="me-auto">Toast 1</strong>
<small class="text-body-secondary">Toast on top</small>
<button type="button" class="btn-close" data-bs-dismiss="toast" aria-label="Close"></button>
</div>
<div class="toast-body">
Toast at the top of the container.
</div>
</div>
<div class="toast fade show" role="alert" aria-live="assertive" aria-atomic="true">
<div class="toast-header">
<strong class="me-auto">Toast 2</strong>
<small class="text-body-secondary">Second toast</small>
<button type="button" class="btn-close" data-bs-dismiss="toast" aria-label="Close"></button>
</div>
<div class="toast-body">
Second toast in the stack.
</div>
</div>
<div class="toast fade show" role="alert" aria-live="assertive" aria-atomic="true">
<div class="toast-header">
<strong class="me-auto">Toast 3</strong>
<small class="text-body-secondary">Third toast</small>
<button type="button" class="btn-close" data-bs-dismiss="toast" aria-label="Close"></button>
</div>
<div class="toast-body">
Another toast in the stack.
</div>
</div>
<div class="toast fade show" role="alert" aria-live="assertive" aria-atomic="true">
<div class="toast-header">
<strong class="me-auto">Toast 4</strong>
<small class="text-body-secondary">Last toast</small>
<button type="button" class="btn-close" data-bs-dismiss="toast" aria-label="Close"></button>
</div>
<div class="toast-body">
Fourth toast at the bottom of the stack.
</div>
</div>
</div>
</div>
</body>
</html>
為了水平和/或垂直對齊吐司提示框,請使用 Flexbox 實用程式。讓我們來看一個例子
示例
您可以使用編輯和執行選項編輯並嘗試執行此程式碼。
<!DOCTYPE html>
<html lang="en">
<head>
<title>Bootstrap - Toasts</title>
<meta charset="UTF-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<link href="https://cdn.jsdelivr.net/npm/bootstrap@5.3.0-alpha3/dist/css/bootstrap.min.css" rel="stylesheet">
<script src="https://cdn.jsdelivr.net/npm/bootstrap@5.3.0-alpha3/dist/js/bootstrap.bundle.min.js"></script>
<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.4.0/jquery.min.js"></script>
</head>
<body class="p-3 m-0 border-0 bd-example m-0 border-0 bd-example-toasts d-flex">
<!-- Adding a flexbox container for alignment of the toasts -->
<div aria-live="polite" aria-atomic="true" class="d-flex justify-content-center align-items-center w-100">
<!-- Then put toasts within the flexbox container-->
<div class="toast fade show" role="alert" aria-live="assertive" aria-atomic="true">
<div class="toast-header bg-danger-subtle">
<strong class="me-auto">Toast within flexbox</strong>
<button type="button" class="btn-close" data-bs-dismiss="toast" aria-label="Close"></button>
</div>
<div class="toast-body">
This is a toast that is placed within a flexbox container and justified center.
</div>
</div>
</div>
</body>
</html>
可訪問性
為了使吐司提示框能夠被使用螢幕閱讀器和/或類似輔助技術的使用者訪問,您應該將吐司提示框包裝在aria-live區域中。
螢幕閱讀器會自動識別對活動區域的更改,而無需設定使用者的焦點。
透過包含aria-atomic="true",確保將整個吐司提示框識別為一個單獨的(原子)單元。
當向用戶顯示的資訊很重要時,請使用警報元件而不是吐司提示框。
活動區域應在生成或更新吐司提示框之前出現在標記中。
根據內容,您需要調整role和aria-live屬性;例如
如果發生錯誤,請使用
role="alert" aria-live="assertive"否則,請使用
role="status" aria-live="polite"
您必須更新延遲超時,以便使用者能夠閱讀吐司提示框,因為顯示的內容會動態更改。
必須向吐司提示框新增一個關閉按鈕,以便使用者在使用autohide: false時關閉吐司提示框。
讓我們來看一個例子
示例
您可以使用編輯和執行選項編輯並嘗試執行此程式碼。
<!DOCTYPE html>
<html lang="en">
<head>
<title>Bootstrap - Toasts</title>
<meta charset="UTF-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<link href="https://cdn.jsdelivr.net/npm/bootstrap@5.3.0-alpha3/dist/css/bootstrap.min.css" rel="stylesheet">
<script src="https://cdn.jsdelivr.net/npm/bootstrap@5.3.0-alpha3/dist/js/bootstrap.bundle.min.js"></script>
<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.4.0/jquery.min.js"></script>
</head>
<body class=" container mx-auto mt-2">
<h4>Accessibility - Toasts</h4>
<p>Make the toasts accessible according to the value of role and aria-live</p>
<!-- Button to trigger the toasts -->
<button type="button" class="btn btn-primary" id="myBtn">Click for toast</button>
<div role="alert" aria-live="assertive" aria-atomic="true" class="toast" data-bs-autohide="false">
<div class="toast">
<div class="toast-header">
<strong class="me-auto">Toast 1</strong>
<small class="text-body-secondary">First toast</small>
<button type="button" class="btn-close" data-bs-dismiss="toast" aria-label="Close"></button>
</div>
<div class="toast-body">
A toast that is like an alert.
</div>
</div>
</div>
<div role="status" aria-live="polite" aria-atomic="true" class="toast" data-bs-autohide="false">
<div class="toast">
<div class="toast-header">
<strong class="me-auto">Toast 2</strong>
<small class="text-body-secondary">Second toast</small>
<button type="button" class="btn-close" data-bs-dismiss="toast" aria-label="Close"></button>
</div>
<div class="toast-body">
Toast where role=status and aria-live=polite.
</div>
</div>
</div>
<script>
$(document).ready(function() {
$('#myBtn').click(function() {
$('.toast').toast({
animation: false,
delay: 3000
});
$('.toast').toast('show');
});
});
</script>
</body>
</html>