消息提示是轻量级通知,旨在模仿移动和桌面操作系统流行的推送通知。它们使用 Flex 布局构建,因此易于对齐和定位。
概述
使用消息提示插件时需要注意的事项:
- 出于性能原因,消息提示需要自行选择启用,因此你必须自己初始化它们。
- 如果你未指定
autohide: false,消息提示会自动隐藏。
此组件的动画效果取决于 prefers-reduced-motion 媒体查询。请参阅无障碍文档中的减少动态效果章节。
示例
基础
为了鼓励可扩展和可预测的消息提示,我们推荐使用头部和主体。消息提示头部使用 display: flex,借助我们的外边距和 Flex 布局工具类可以轻松对齐内容。
消息提示完全按你的需要灵活定制,所需标记非常少。至少,我们需要一个元素来包含你“消息提示化”的内容,并强烈建议添加一个关闭按钮。
<div class="toast" role="alert" aria-live="assertive" aria-atomic="true">
<div class="toast-header">
<img src="..." class="rounded me-2" alt="...">
<strong class="me-auto">Bootstrap</strong>
<small>11 mins ago</small>
<button type="button" class="btn-close" data-bs-dismiss="toast" aria-label="Close"></button>
</div>
<div class="toast-body">
你好,世界!这是一条消息提示消息。
</div>
</div>实际演示
点击下面的按钮,显示一个默认隐藏的消息提示(使用我们的工具类定位在右下角)。
你好,世界!这是一条消息提示消息。
<button type="button" class="btn-solid theme-primary" id="liveToastBtn">Show live toast</button>
<div class="toast-container position-fixed bottom-0 end-0 p-3">
<div id="liveToast" class="toast" role="alert" aria-live="assertive" aria-atomic="true">
<div class="toast-header">
<img src="..." class="rounded me-2" alt="...">
<strong class="me-auto">Bootstrap</strong>
<small>11 mins ago</small>
<button type="button" class="btn-close" data-bs-dismiss="toast" aria-label="Close"></button>
</div>
<div class="toast-body">
你好,世界!这是一条消息提示消息。
</div>
</div>
</div>
我们使用以下 JavaScript 触发实际演示消息提示:
const toastTrigger = document.getElementById('liveToastBtn')
const toastLiveExample = document.getElementById('liveToast')
if (toastTrigger) {
const toastBootstrap = Toast.getOrCreateInstance(toastLiveExample)
toastTrigger.addEventListener('click', () => {
toastBootstrap.show()
})
}
半透明
消息提示略微半透明,以融入其下方的背景。
<div class="toast" role="alert" aria-live="assertive" aria-atomic="true">
<div class="toast-header">
<img src="..." class="rounded me-2" alt="...">
<strong class="me-auto">Bootstrap</strong>
<small class="fg-2">11 mins ago</small>
<button type="button" class="btn-close" data-bs-dismiss="toast" aria-label="Close"></button>
</div>
<div class="toast-body">
你好,世界!这是一条消息提示消息。
</div>
</div>添加 .toast-translucent,通过背景模糊和饱和度滤镜增强半透明效果。
<div class="toast toast-translucent" role="alert" aria-live="assertive" aria-atomic="true">
<div class="toast-header">
<img src="..." class="rounded me-2" alt="...">
<strong class="me-auto">Bootstrap</strong>
<small class="fg-2">11 mins ago</small>
<button type="button" class="btn-close" data-bs-dismiss="toast" aria-label="Close"></button>
</div>
<div class="toast-body">
你好,世界!这是一条半透明消息提示消息。
</div>
</div>堆叠
你可以通过将消息提示包裹在消息提示容器中来堆叠它们,这会在垂直方向增加一些间距。
<div class="toast-container position-static">
<div class="toast" role="alert" aria-live="assertive" aria-atomic="true">
<div class="toast-header">
<img src="..." class="rounded me-2" alt="...">
<strong class="me-auto">Bootstrap</strong>
<small class="fg-2">just now</small>
<button type="button" class="btn-close" data-bs-dismiss="toast" aria-label="Close"></button>
</div>
<div class="toast-body">
See? Just like this.
</div>
</div>
<div class="toast" role="alert" aria-live="assertive" aria-atomic="true">
<div class="toast-header">
<img src="..." class="rounded me-2" alt="...">
<strong class="me-auto">Bootstrap</strong>
<small class="fg-2">2 seconds ago</small>
<button type="button" class="btn-close" data-bs-dismiss="toast" aria-label="Close"></button>
</div>
<div class="toast-body">
注意,消息提示会自动堆叠
</div>
</div>
</div>深色消息提示
为 .toast 元素添加 data-bs-theme="dark" 属性,将消息提示设置为深色模式。
<div class="toast" data-bs-theme="dark" role="alert" aria-live="assertive" aria-atomic="true">
<div class="toast-header">
<img src="..." class="rounded me-2" alt="...">
<strong class="me-auto">Bootstrap</strong>
<small class="opacity-60">2 seconds ago</small>
<button type="button" class="btn-close" data-bs-dismiss="toast" aria-label="Close"></button>
</div>
<div class="toast-body">
此消息提示默认处于深色模式。
<div class="hstack gap-2 mt-2">
<button type="button" class="btn-solid theme-primary btn-sm">采取操作</button>
<button type="button" class="btn-solid theme-secondary btn-sm" data-bs-dismiss="toast">Close</button>
</div>
</div>
</div>变体
消息提示可以使用我们的主题颜色工具类进行主题化。可应用于整个消息提示或消息提示内的各个元素。
<div class="toast theme-primary" role="alert" aria-live="assertive" aria-atomic="true">
<div class="toast-header">
<img src="..." class="rounded me-2" alt="...">
<strong class="me-auto">Bootstrap</strong>
<small class="opacity-60">2 seconds ago</small>
<button type="button" class="btn-close" data-bs-dismiss="toast" aria-label="Close"></button>
</div>
<div class="toast-body">
此消息提示是主色调主题的消息提示。
<div class="hstack gap-2 mt-2">
<button type="button" class="btn-solid btn-sm">采取操作</button>
<button type="button" class="btn-solid theme-secondary btn-sm" data-bs-dismiss="toast">关闭</button>
</div>
</div>
</div>
<div class="toast theme-accent" role="alert" aria-live="assertive" aria-atomic="true">
<div class="toast-header">
<img src="..." class="rounded me-2" alt="...">
<strong class="me-auto">Bootstrap</strong>
<small class="opacity-60">2 seconds ago</small>
<button type="button" class="btn-close" data-bs-dismiss="toast" aria-label="Close"></button>
</div>
<div class="toast-body">
此消息提示是强调色主题的消息提示。
<div class="hstack gap-2 mt-2">
<button type="button" class="btn-solid btn-sm">采取操作</button>
<button type="button" class="btn-solid theme-secondary btn-sm" data-bs-dismiss="toast">关闭</button>
</div>
</div>
</div>自定义内容
通过移除子组件、使用工具类调整它们或添加你自己的标记来自定义消息提示。这里我们通过移除默认的 .toast-header、从 Bootstrap Icons添加自定义隐藏图标,以及使用一些 Flex 布局工具类调整布局,创建了一个更简单的消息提示。
<div class="toast" role="alert" aria-live="assertive" aria-atomic="true">
<div class="d-flex justify-content-between">
<div class="toast-body">
你好,世界!这是一条消息提示消息。
</div>
<button type="button" class="btn-close me-3 m-auto" data-bs-dismiss="toast" aria-label="Close"></button>
</div>
</div>或者,你也可以向消息提示添加额外的控件和组件。
<div class="toast" role="alert" aria-live="assertive" aria-atomic="true">
<div class="toast-body">
这是一条自定义消息提示消息,包含水平线和按钮等额外内容。
<hr class="my-3" />
<div class="hstack gap-2">
<button type="button" class="btn-solid theme-primary btn-sm">采取操作</button>
<button type="button" class="btn-solid theme-secondary btn-sm" data-bs-dismiss="toast">关闭</button>
</div>
</div>
</div>即时
默认情况下,消息提示会淡入淡出。要禁用动画,请为消息提示添加 .toast-instant。随后 show 和 hide 方法会立即完成,因此 shown.bs.toast 和 hidden.bs.toast 会立即触发。
<div class="toast toast-instant" role="alert" aria-live="assertive" aria-atomic="true">
<div class="toast-header">
<img src="..." class="rounded me-2" alt="...">
<strong class="me-auto">Bootstrap</strong>
<small>11 mins ago</small>
<button type="button" class="btn-close" data-bs-dismiss="toast" aria-label="Close"></button>
</div>
<div class="toast-body">
此消息提示会立即出现并消失。
</div>
</div>定位
根据需要,使用自定义 CSS 放置消息提示。右上角常用于通知,顶部中间也是如此。如果你一次只显示一个消息提示,可以将定位样式直接放在 .toast 上。
<form>
<div class="mb-3">
<label for="selectToastPlacement">Toast placement</label>
<select class="form-control mt-2" id="selectToastPlacement">
<option value="" selected disabled>Select a position...</option>
<option value="top-0 start-0">Top left</option>
<option value="top-0 start-50 translate-middle-x">Top center</option>
<option value="top-0 end-0">Top right</option>
<option value="top-50 start-0 translate-middle-y">Middle left</option>
<option value="top-50 start-50 translate-middle">Middle center</option>
<option value="top-50 end-0 translate-middle-y">Middle right</option>
<option value="bottom-0 start-0">Bottom left</option>
<option value="bottom-0 start-50 translate-middle-x">Bottom center</option>
<option value="bottom-0 end-0">Bottom right</option>
</select>
</div>
</form>
<div aria-live="polite" aria-atomic="true" class="bg-1 position-relative bd-example-toasts rounded-3">
<div class="toast-container p-3" id="toastPlacement">
<div class="toast">
<div class="toast-header">
<img src="..." class="rounded me-2" alt="...">
<strong class="me-auto">Bootstrap</strong>
<small>11 mins ago</small>
</div>
<div class="toast-body">
你好,世界!这是一条消息提示消息。
</div>
</div>
</div>
</div>对于生成更多通知的系统,考虑使用包裹元素,以便它们可以轻松堆叠。
<div aria-live="polite" aria-atomic="true" class="position-relative">
<!-- Position it: -->
<!-- - `.toast-container` for spacing between toasts -->
<!-- - `top-0` & `end-0` to position the toasts in the upper right corner -->
<!-- - `.p-3` to prevent the toasts from sticking to the edge of the container -->
<div class="toast-container top-0 end-0 p-3">
<!-- Then put toasts within -->
<div class="toast" role="alert" aria-live="assertive" aria-atomic="true">
<div class="toast-header">
<img src="..." class="rounded me-2" alt="...">
<strong class="me-auto">Bootstrap</strong>
<small class="fg-2">just now</small>
<button type="button" class="btn-close" data-bs-dismiss="toast" aria-label="Close"></button>
</div>
<div class="toast-body">
See? Just like this.
</div>
</div>
<div class="toast" role="alert" aria-live="assertive" aria-atomic="true">
<div class="toast-header">
<img src="..." class="rounded me-2" alt="...">
<strong class="me-auto">Bootstrap</strong>
<small class="fg-2">2 seconds ago</small>
<button type="button" class="btn-close" data-bs-dismiss="toast" aria-label="Close"></button>
</div>
<div class="toast-body">
注意,消息提示会自动堆叠
</div>
</div>
</div>
</div>你还可以使用 Flex 布局工具类来水平或垂直对齐消息提示。
<!-- Flexbox container for aligning the toasts -->
<div aria-live="polite" aria-atomic="true" class="d-flex justify-content-center align-items-center w-100">
<!-- Then put toasts within -->
<div class="toast" role="alert" aria-live="assertive" aria-atomic="true">
<div class="toast-header">
<img src="..." class="rounded me-2" alt="...">
<strong class="me-auto">Bootstrap</strong>
<small>11 mins ago</small>
<button type="button" class="btn-close" data-bs-dismiss="toast" aria-label="Close"></button>
</div>
<div class="toast-body">
你好,世界!这是一条消息提示消息。
</div>
</div>
</div>抽屉和对话框
模态抽屉和对话框渲染在浏览器的顶层。请将消息提示容器及其消息提示放在打开的抽屉或对话框内。提高消息提示的 z-index 无法使页面级消息提示显示在其上方。请参阅对话框浮层示例。
可访问性
消息提示旨在作为对访客或用户的小型打断,因此为帮助屏幕阅读器和类似辅助技术用户,你应将消息提示包裹在 aria-live 区域中。实时区域的更改(例如注入/更新消息提示组件)会自动由屏幕阅读器播报,无需移动用户焦点或以其他方式打断用户。此外,包含 aria-atomic="true" 以确保整个消息提示始终作为单个(原子)单元播报,而不是仅播报更改的内容(如果你只更新消息提示内容的一部分,或在稍后的时间点显示相同的消息提示内容,这可能导致问题)。如果所需信息对流程很重要,例如表单中的错误列表,请使用提示组件而不是消息提示。
请注意,实时区域需要在生成或更新消息提示之前存在于标记中。如果你同时动态生成两者并将它们注入页面,辅助技术通常不会播报它们。
你还需要根据内容调整 role 和 aria-live 级别。如果是错误等重要消息,请使用 role="alert" aria-live="assertive",否则使用 role="status" aria-live="polite" 属性。
随着你显示的内容变化,请务必更新 delay 超时,以便用户有足够时间阅读消息提示。
<div class="toast" role="alert" aria-live="polite" aria-atomic="true" data-bs-delay="10000">
<div role="alert" aria-live="assertive" aria-atomic="true">...</div>
</div>
使用 autohide: false 时,你必须添加关闭按钮以允许用户关闭消息提示。
<div role="alert" aria-live="assertive" aria-atomic="true" class="toast" data-bs-autohide="false">
<div class="toast-header">
<img src="..." class="rounded me-2" alt="...">
<strong class="me-auto">Bootstrap</strong>
<small>11 mins ago</small>
<button type="button" class="btn-close" data-bs-dismiss="toast" aria-label="Close"></button>
</div>
<div class="toast-body">
你好,世界!这是一条消息提示消息。
</div>
</div>虽然在消息提示中技术上是可行的添加可聚焦/可操作的控件(例如额外的按钮或链接),但你应避免为自动隐藏的消息提示这样做。即使你为消息提示设置了很长的 delay 超时,键盘和辅助技术用户也可能难以及时到达消息提示以采取操作(因为消息提示显示时不会获得焦点)。如果你确实必须有更多控件,我们建议使用 autohide: false 的消息提示。
CSS
变量
Toasts use local CSS variables on .toast for real-time customization. Values for the CSS variables are generated from Sass maps unique to each component and applied to the aforementioned class.
// stylelint-disable custom-property-no-missing-var-function
// stylelint-disable-next-line scss/dollar-variable-default
$toast-tokens: defaults(
(
--toast-zindex: var(--z-toast),
--toast-padding-x: 1rem,
--toast-padding-y: .75rem,
--toast-spacing: #{$container-padding-x},
--toast-max-width: 350px,
--toast-font-size: var(--font-size-sm),
--toast-color: null,
--toast-bg: var(--bg-body),
--toast-border-width: var(--border-width),
--toast-border-color: var(--border-color-translucent),
--toast-border-radius: null,
--toast-box-shadow: var(--box-shadow),
--toast-header-color: var(--fg-3),
--toast-header-bg: var(--bg-1),
--toast-header-border-color: var(--border-color-translucent),
--toast-transition-property: "opacity, display",
--toast-transition-duration: .15s,
--toast-transition-timing: linear,
),
$toast-tokens
);
// stylelint-enable custom-property-no-missing-var-function
用法
通过 JavaScript 初始化消息提示:
const toastElList = document.querySelectorAll('.toast')
const toastList = [...toastElList].map(toastEl => new bootstrap.Toast(toastEl, option))
通过 data 属性
| 属性 | 描述 |
|---|---|
data-bs-dismiss="toast" | 在关闭控件上,激活时关闭消息提示。 |
data-bs-autohide | 消息提示是否在延迟后自动隐藏。 |
data-bs-delay | 隐藏前的毫秒数(启用自动隐藏时)。 |
触发器
Dismissal can be achieved with the data-bs-dismiss attribute on a button within the toast as demonstrated below:
<button type="button" class="btn-close" data-bs-dismiss="toast" aria-label="Close"></button>or on a button outside the toast using the additional data-bs-target as demonstrated below:
<button type="button" class="btn-close" data-bs-dismiss="toast" data-bs-target="#my-toast" aria-label="Close"></button>依赖
The toast plugin requires the following JavaScript files if you’re building Bootstrap’s JS from source:
| File | Description |
|---|---|
js/src/toast.ts | Main toast component |
js/src/base-component.ts | Base component class |
js/src/dom/data.ts | Element data store |
js/src/dom/event-handler.ts | Event handling utilities |
js/src/dom/manipulator.ts | Data attribute manipulation |
js/src/dom/selector-engine.ts | DOM selector utilities |
js/src/util/component-functions.ts | Shared component helpers |
js/src/util/config.ts | Configuration base class |
js/src/util/index.ts | Core utility functions |
选项
As options can be passed via data attributes or JavaScript, you can append an option name to data-bs-, as in data-bs-animation="{value}". Make sure to change the case type of the option name from “camelCase” to “kebab-case” when passing the options via data attributes. For example, use data-bs-custom-class="beautifier" instead of data-bs-customClass="beautifier".
All components support a reserved data attribute data-bs-config that can house simple component configuration as a JSON string. When an element has data-bs-config='{"delay":0, "title":123}' and data-bs-title="456" attributes, the final title value will be 456 and the separate data attributes will override values given on data-bs-config. In addition, existing data attributes are able to house JSON values like data-bs-delay='{"show":0,"hide":150}'.
The final configuration object is the merged result of data-bs-config, data-bs-, and js object where the latest given key-value overrides the others.
| 名称 | 类型 | 默认值 | 描述 |
|---|---|---|---|
autohide | boolean | true | 延迟后自动隐藏消息提示。 |
delay | number | 5000 | 隐藏消息提示前的延迟毫秒数。 |
方法
用于显示或隐藏组件的方法会返回一个 promise。 该 promise 在过渡结束时 resolve,因此 await 会在与 shown.bs.* 或 hidden.bs.* 监听器触发的同一时刻继续执行。这些方法仍会立即返回,绝不会阻塞调用方。对正处于过渡中的组件进行调用不会产生任何效果,并会立即 resolve。在我们的 JavaScript 文档中了解更多。
| 方法 | 描述 |
|---|---|
dispose | 隐藏元素的消息提示。你的消息提示将保留在 DOM 中但不再显示。 |
getInstance | 静态 方法,允许你获取与 DOM 元素关联的消息提示实例。 例如: const myToastEl = document.getElementById('myToastEl') const myToast = bootstrap.Toast.getInstance(myToastEl) 返回一个 Bootstrap 消息提示实例。 |
getOrCreateInstance | 静态 方法,允许你获取与 DOM 元素关联的消息提示实例,如果尚未初始化则创建一个新实例。const myToastEl = document.getElementById('myToastEl') const myToast = bootstrap.Toast.getOrCreateInstance(myToastEl) 返回一个 Bootstrap 消息提示实例。 |
hide | 隐藏元素的消息提示。返回一个 promise,在消息提示隐藏后解析(即在 hidden.bs.toast 事件触发后)。如果你将 autohide 设为 false,则必须手动调用此方法。 |
isShown | 根据消息提示的可见状态返回布尔值。 |
show | 显示元素的消息提示。返回一个 promise,在消息提示显示后解析(即在 shown.bs.toast 事件触发后)。你必须手动调用此方法,否则你的消息提示不会显示。 |
事件
| 事件 | 描述 |
|---|---|
hide.bs.toast | 在调用 hide 实例方法时立即触发此事件。 |
hidden.bs.toast | 当消息提示完成对用户的隐藏后触发此事件。 |
show.bs.toast | 在调用 show 实例方法时立即触发此事件。 |
shown.bs.toast | 当消息提示已对用户可见时触发此事件。 |
const myToastEl = document.getElementById('myToast')
myToastEl.addEventListener('hidden.bs.toast', () => {
// do something...
})