对话框
基于原生 <dialog> 元素构建的现代组件,具有内置无障碍和背景支持,取代旧的模态框组件。
工作原理
对话框组件利用浏览器原生的 <dialog> 元素,提供内置的无障碍功能、焦点管理和背景处理,而无需自定义实现的复杂性。
原生对话框的主要特性:
- 通过
showModal()/show()实现模态或内联——modal: true(默认)会将对话框提升到浏览器的顶层,并带有背景和焦点捕获;modal: false则将其渲染为内联。 - 内置背景,使用
::backdrop伪元素(仅模态);设置backdrop: "static"可锁定外部点击,或设置backdrop: false隐藏它。 - Escape 键处理默认关闭对话框;设置
keyboard: false可禁用。 - 无障碍——在模态对话框中焦点被捕获,并在关闭时返回到触发元素,具有原生
<dialog>ARIA 语义。 - 打开和关闭动画——通过使用
.hiding类在关闭期间将对话框保留在顶层,绕过浏览器限制,使退出过渡(包括::backdrop)得到正确的动画处理。
当模态对话框打开时,Bootstrap 会阻止页面滚动。JavaScript 会补偿消失的滚动条空间,并在最后一个滚动锁定对话框或抽屉关闭时恢复原始间距。全宽的 .fixed-top、.fixed-bottom 和 .is-fixed 元素也会得到补偿;正常流和粘性内容遵循根内边距。
此组件的动画效果取决于 prefers-reduced-motion 媒体查询。请参阅无障碍文档中的减少动态效果章节。
示例
点击下面的按钮切换对话框。该对话框使用原生 showModal() API 实现真正的模态行为。
<button type="button" class="btn-solid theme-primary" data-bs-toggle="dialog" data-bs-target="#exampleDialog">
Open dialog
</button>
<dialog class="dialog" id="exampleDialog" aria-labelledby="exampleDialogLabel">
<div class="dialog-header">
<h1 class="dialog-title" id="exampleDialogLabel">Dialog title</h1>
<button type="button" class="btn-close" data-bs-dismiss="dialog" aria-label="Close"></button>
</div>
<div class="dialog-body">
<p>This is a native dialog element. It uses the browser’s built-in modal behavior for accessibility and focus management.</p>
</div>
<div class="dialog-footer">
<button type="button" class="btn-solid theme-secondary" data-bs-dismiss="dialog">Close</button>
<button type="button" class="btn-solid theme-primary">Save changes</button>
</div>
</dialog>无 JavaScript
支持的浏览器可以使用 Invoker Commands API打开和关闭对话框——无需 Bootstrap JavaScript。将 commandfor 设置为对话框的 id,使用 command="show-modal" 打开,使用 command="close" 关闭。
并非所有浏览器都支持 Invoker Commands API。如需保证支持,当你需要我们的插件选项、事件或动画关闭时,请使用上面的 data-bs-toggle 示例。
<button type="button" class="btn-solid theme-primary" commandfor="invokerDialog" command="show-modal">
Open dialog
</button>
<dialog class="dialog" id="invokerDialog" aria-labelledby="invokerDialogLabel">
<div class="dialog-header">
<h1 class="dialog-title" id="invokerDialogLabel">Invoker Commands</h1>
<button type="button" class="btn-close" commandfor="invokerDialog" command="close" aria-label="Close"></button>
</div>
<div class="dialog-body">
<p>This dialog opens with <code>command="show-modal"</code> and closes with <code>command="close"</code>. No Bootstrap JavaScript is involved.</p>
</div>
<div class="dialog-footer">
<button type="button" class="btn-solid theme-secondary" commandfor="invokerDialog" command="close">Close</button>
</div>
</dialog>深色对话框
要使对话框变为深色,请为 <dialog> 元素添加 data-bs-theme="dark"。
<button type="button" class="btn-solid theme-primary" data-bs-toggle="dialog" data-bs-target="#exampleDialogDark">
打开深色对话框
</button>
<dialog class="dialog" id="exampleDialogDark" aria-labelledby="exampleDialogDarkLabel" data-bs-theme="dark">
<div class="dialog-header">
<h1 class="dialog-title" id="exampleDialogDarkLabel">Dark dialog title</h1>
<button type="button" class="btn-close" data-bs-dismiss="dialog" aria-label="Close"></button>
</div>
<div class="dialog-body">
<p>This is a native dialog element, only it’s set to dark mode. It uses the browser’s built-in modal behavior for accessibility and focus management.</p>
</div>
<div class="dialog-footer">
<button type="button" class="btn-solid theme-secondary" data-bs-dismiss="dialog">Close</button>
<button type="button" class="btn-solid theme-primary">Save changes</button>
</div>
</dialog>静态背景
当 backdrop 设置为 static 时,点击对话框外部不会关闭它。点击下面的按钮试一试。
<button type="button" class="btn-solid theme-primary" data-bs-toggle="dialog" data-bs-target="#staticBackdropDialog" data-bs-backdrop="static">
打开静态背景遮罩对话框
</button>
<dialog class="dialog" id="staticBackdropDialog" aria-labelledby="staticBackdropDialogLabel">
<div class="dialog-header">
<h1 class="dialog-title" id="staticBackdropDialogLabel">Static backdrop</h1>
<button type="button" class="btn-close" data-bs-dismiss="dialog" aria-label="Close"></button>
</div>
<div class="dialog-body">
<p>I will not close if you click outside of me. Use the close button or press Escape.</p>
</div>
<div class="dialog-footer">
<button type="button" class="btn-solid theme-secondary" data-bs-dismiss="dialog">Close</button>
</div>
</dialog>叠加层
模态对话框使用浏览器的顶层。将吐司容器及其吐司放置在对话框内,使它们保持可见。工具提示和弹出框默认使用打开的对话框作为其容器。如果你设置了自定义 container,请选择对话框内的元素。
<button type="button" class="btn-solid theme-primary" data-bs-toggle="dialog" data-bs-target="#dialogWithComponents">
Open dialog
</button>
<dialog class="dialog not-prose" id="dialogWithComponents" aria-labelledby="dialogWithComponentsLabel">
<div class="dialog-header">
<h1 class="dialog-title" id="dialogWithComponentsLabel">Overlays inside dialogs</h1>
<button type="button" class="btn-close" data-bs-dismiss="dialog" aria-label="Close"></button>
</div>
<div class="dialog-body">
<p>These components work inside modal dialogs.</p>
<div class="d-flex gap-2 flex-wrap">
<button type="button" class="btn-solid theme-secondary" data-bs-toggle="tooltip" data-bs-title="Tooltip inside a dialog!">Tooltip</button>
<button type="button" class="btn-solid theme-secondary" data-bs-toggle="popover" data-bs-title="Popover title" data-bs-content="This popover is inside a dialog and renders in the top layer. We include some longer content here to demonstrate the placement and wrapping behavior." data-bs-placement="top">Popover</button>
<button type="button" class="btn-solid theme-secondary" id="dialogToastBtn">Show toast</button>
<button class="btn-solid theme-secondary" type="button" data-bs-toggle="menu" aria-expanded="false">Menu</button>
<div class="menu">
<a class="menu-item" href="#">Action</a>
<a class="menu-item" href="#">Another action</a>
<hr class="menu-divider" />
<div class="submenu">
<button class="menu-item" type="button">More options</button>
<div class="menu">
<a class="menu-item" href="#">Sub-action A</a>
<a class="menu-item" href="#">Sub-action B</a>
<div class="submenu">
<button class="menu-item" type="button">Even more</button>
<div class="menu">
<a class="menu-item" href="#">Deep action 1</a>
<a class="menu-item" href="#">Deep action 2</a>
</div>
</div>
</div>
</div>
</div>
</div>
</div>
<div class="dialog-footer">
<button type="button" class="btn-solid theme-secondary" data-bs-dismiss="dialog">Close</button>
</div>
<div class="toast-container position-fixed bottom-0 end-0 p-3">
<div class="toast" id="dialogToast" role="alert" aria-live="assertive" aria-atomic="true">
<div class="toast-body">
对话框内的吐司!
<div class="mt-2 pt-2 border-top">
<button type="button" class="btn-solid theme-primary btn-sm" data-bs-dismiss="toast">Close</button>
</div>
</div>
</div>
</div>
</dialog>动画
立即
默认情况下,对话框在打开和关闭时会淡入淡出动画。要禁用动画,请为 <dialog> 元素添加 .dialog-instant。
<button type="button" class="btn-solid theme-primary" data-bs-toggle="dialog" data-bs-target="#instantDialog">
Instant dialog
</button>
<dialog class="dialog dialog-instant" id="instantDialog" aria-labelledby="instantDialogLabel">
<div class="dialog-header">
<h1 class="dialog-title" id="instantDialogLabel">Instant dialog</h1>
<button type="button" class="btn-close" data-bs-dismiss="dialog" aria-label="Close"></button>
</div>
<div class="dialog-body">
<p>This dialog doesn’t animate and appears instantly on open and close.</p>
</div>
<div class="dialog-footer">
<button type="button" class="btn-solid theme-secondary" data-bs-dismiss="dialog">Close</button>
<button type="button" class="btn-solid theme-primary">Save changes</button>
</div>
</dialog>下滑
为 <dialog> 添加 .dialog-slide-down,打开时它将从视口顶部向下滑动。
<button type="button" class="btn-solid theme-primary" data-bs-toggle="dialog" data-bs-target="#slideDownDialog">
向下滑动对话框
</button>
<dialog class="dialog dialog-slide-down" id="slideDownDialog" aria-labelledby="slideDownDialogLabel">
<div class="dialog-header">
<h1 class="dialog-title" id="slideDownDialogLabel">向下滑动对话框</h1>
<button type="button" class="btn-close" data-bs-dismiss="dialog" aria-label="Close"></button>
</div>
<div class="dialog-body">
<p>This dialog slides down from the top of the viewport.</p>
</div>
<div class="dialog-footer">
<button type="button" class="btn-solid theme-secondary" data-bs-dismiss="dialog">Close</button>
<button type="button" class="btn-solid theme-primary">Save changes</button>
</div>
</dialog>上滑
为 <dialog> 添加 .dialog-slide-up,打开时它将从视口底部向上滑动。
<button type="button" class="btn-solid theme-primary" data-bs-toggle="dialog" data-bs-target="#slideUpDialog">
向上滑动对话框
</button>
<dialog class="dialog dialog-slide-up" id="slideUpDialog" aria-labelledby="slideUpDialogLabel">
<div class="dialog-header">
<h1 class="dialog-title" id="slideUpDialogLabel">向上滑动对话框</h1>
<button type="button" class="btn-close" data-bs-dismiss="dialog" aria-label="Close"></button>
</div>
<div class="dialog-body">
<p>This dialog slides up from the bottom of the viewport.</p>
</div>
<div class="dialog-footer">
<button type="button" class="btn-solid theme-secondary" data-bs-dismiss="dialog">Close</button>
<button type="button" class="btn-solid theme-primary">Save changes</button>
</div>
</dialog>可滚动
创建一个可滚动的对话框,滚动对话框主体,同时保持页眉和页脚固定。为 .dialog 元素添加 .dialog-scrollable。
<button type="button" class="btn-solid theme-primary" data-bs-toggle="dialog" data-bs-target="#scrollableBodyDialog">
启动可滚动正文对话框
</button>
<dialog class="dialog dialog-scrollable" id="scrollableBodyDialog" aria-labelledby="scrollableBodyDialogLabel">
<div class="dialog-header">
<h1 class="dialog-title" id="scrollableBodyDialogLabel" tabindex="-1" autofocus>Scrollable body</h1>
<button type="button" class="btn-close" data-bs-dismiss="dialog" aria-label="Close"></button>
</div>
<div class="dialog-body">
<p>This is some placeholder content to show the scrolling behavior for dialogs. We use repeated line breaks to demonstrate how content can exceed the dialog’s inner height, showing scrolling within the body while the header and footer remain fixed.</p>
<br/><br/><br/><br/><br/><br/><br/><br/><br/><br/>
<br/><br/><br/><br/><br/><br/><br/><br/><br/><br/>
<br/><br/><br/><br/><br/><br/><br/><br/><br/><br/>
<br/><br/><br/><br/><br/><br/><br/><br/><br/><br/>
<br/><br/><br/><br/><br/><br/><br/><br/><br/><br/>
<br/><br/><br/><br/><br/><br/><br/><br/><br/><br/>
<p>This content should appear at the bottom after you scroll.</p>
</div>
<div class="dialog-footer">
<button type="button" class="btn-solid theme-secondary" data-bs-dismiss="dialog">Close</button>
<button type="button" class="btn-solid theme-primary">Save changes</button>
</div>
</dialog>交换对话框
当切换触发器位于打开的对话框内时,点击它将会交换对话框——在关闭当前对话框之前打开新的对话框。这确保了背景在整个过渡过程中保持可见,不会有闪烁。当 data-bs-toggle="dialog" 触发器位于已打开的对话框内时,交换行为是自动的。目标是其自身打开的对话框的切换触发器不会做任何事。使用 data-bs-dismiss="dialog" 来关闭它。
<button type="button" class="btn-solid theme-primary" data-bs-toggle="dialog" data-bs-target="#swapDialog1">
打开第一个对话框
</button>
<dialog class="dialog" id="swapDialog1" aria-labelledby="swapDialog1Label">
<div class="dialog-header">
<h1 class="dialog-title" id="swapDialog1Label">First dialog</h1>
<button type="button" class="btn-close" data-bs-dismiss="dialog" aria-label="Close"></button>
</div>
<div class="dialog-body">
<p>Click below to swap to a second dialog. Notice the backdrop stays visible—no flash!</p>
</div>
<div class="dialog-footer">
<button type="button" class="btn-solid theme-secondary" data-bs-dismiss="dialog">Close</button>
<button type="button" class="btn-solid theme-primary" data-bs-toggle="dialog" data-bs-target="#swapDialog2">Go to second dialog</button>
</div>
</dialog>
<dialog class="dialog" id="swapDialog2" aria-labelledby="swapDialog2Label">
<div class="dialog-header">
<h1 class="dialog-title" id="swapDialog2Label">Second dialog</h1>
<button type="button" class="btn-close" data-bs-dismiss="dialog" aria-label="Close"></button>
</div>
<div class="dialog-body">
<p>This is the second dialog. You can swap back to the first, or close this one entirely.</p>
</div>
<div class="dialog-footer">
<button type="button" class="btn-solid theme-secondary" data-bs-dismiss="dialog">Close</button>
<button type="button" class="btn-solid theme-primary" data-bs-toggle="dialog" data-bs-target="#swapDialog1">Back to first dialog</button>
</div>
</dialog>非模态对话框
默认情况下,对话框使用浏览器原生的 showModal() 方法作为模态打开。你还可以通过将 modal 设置为 false,使用 show() 将对话框作为非模态打开。非模态对话框:
- 没有背景
- 不捕获焦点
- 不阻止与页面其余部分的交互
- 不在浏览器的顶层渲染
- 仍响应 Escape 键(如果
keyboard: true)
<button type="button" class="btn-solid theme-primary" data-bs-toggle="dialog" data-bs-target="#nonModalDialog" data-bs-modal="false">
打开非模态对话框
</button>
<dialog class="dialog" id="nonModalDialog" aria-labelledby="nonModalDialogLabel">
<div class="dialog-header">
<h1 class="dialog-title" id="nonModalDialogLabel">Non-modal dialog</h1>
<button type="button" class="btn-close" data-bs-dismiss="dialog" aria-label="Close"></button>
</div>
<div class="dialog-body">
<p>This dialog doesn’t block the page. You can still interact with content behind it.</p>
</div>
<div class="dialog-footer">
<button type="button" class="btn-solid theme-secondary" data-bs-dismiss="dialog">Close</button>
</div>
</dialog>如果使用 JavaScript 构建,可使用 modal 选项创建非模态对话框:
const dialog = new bootstrap.Dialog('#myDialog', { modal: false })
dialog.show()
可选尺寸
对话框有四种可选尺寸,可通过修饰符类放置在 .dialog 上使用。这些尺寸在某些断点处生效,以避免在较窄的视口上出现水平滚动条。
| 尺寸 | 类 | 对话框最大宽度 |
|---|---|---|
| 小 | .dialog-sm | 280px |
| 默认 | — | 500px |
| 大 | .dialog-lg | 800px |
| 特大 | .dialog-xl | 1140px |
| 超特大 | .dialog-2xl | 1440px |
<dialog class="dialog dialog-2xl">…</dialog>
<dialog class="dialog dialog-xl">…</dialog>
<dialog class="dialog dialog-lg">…</dialog>
<dialog class="dialog">…</dialog>
<dialog class="dialog dialog-sm">…</dialog>全屏对话框
使用 .dialog-fullscreen 使对话框覆盖整个视口。
<button type="button" class="btn-solid theme-primary" data-bs-toggle="dialog" data-bs-target="#exampleDialogFullscreen">
Fullscreen dialog
</button>
<dialog class="dialog dialog-fullscreen" id="exampleDialogFullscreen" aria-labelledby="exampleDialogFullscreenLabel">
<div class="dialog-header">
<h1 class="dialog-title" id="exampleDialogFullscreenLabel">Fullscreen dialog</h1>
<button type="button" class="btn-close" data-bs-dismiss="dialog" aria-label="Close"></button>
</div>
<div class="dialog-body">
<p>This dialog covers the entire viewport.</p>
</div>
<div class="dialog-footer">
<button type="button" class="btn-solid theme-secondary" data-bs-dismiss="dialog">Close</button>
</div>
</dialog>也提供响应式全屏变体。这些仅在特定断点以下使对话框全屏。
| 类 | 全屏低于 |
|---|---|
.dialog-fullscreen | 始终 |
.sm-down:dialog-fullscreen | 576px |
.md-down:dialog-fullscreen | 768px |
.lg-down:dialog-fullscreen | 1024px |
.xl-down:dialog-fullscreen | 1280px |
.2xl-down:dialog-fullscreen | 1536px |
<button type="button" class="btn-solid theme-primary" data-bs-toggle="dialog" data-bs-target="#exampleDialogFullscreenLg">
lg 以下全屏
</button>
<dialog class="dialog lg-down:dialog-fullscreen" id="exampleDialogFullscreenLg" aria-labelledby="exampleDialogFullscreenLgLabel">
<div class="dialog-header">
<h1 class="dialog-title" id="exampleDialogFullscreenLgLabel">lg 以下全屏</h1>
<button type="button" class="btn-close" data-bs-dismiss="dialog" aria-label="Close"></button>
</div>
<div class="dialog-body">
<p>This dialog is fullscreen below the <code>lg</code> breakpoint.</p>
</div>
<div class="dialog-footer">
<button type="button" class="btn-solid theme-secondary" data-bs-dismiss="dialog">Close</button>
</div>
</dialog>无障碍
为每个对话框提供一个无障碍名称。将 aria-labelledby 设置为其可见标题的 ID。当对话框没有可见标题时,使用 aria-label。
在对话框中保留一个可见的关闭按钮。对于破坏性操作,在更安全的选择(如取消)上使用 autofocus。对于较长内容,考虑使用带有 tabindex="-1" 和 autofocus 的标题。
CSS
变量
// stylelint-disable-next-line scss/dollar-variable-default
$dialog-tokens: defaults(
(
--dialog-padding: 1rem,
--dialog-width: 500px,
--dialog-margin: 1.75rem,
--dialog-color: var(--fg-body),
--dialog-bg: var(--bg-body),
--dialog-border-color: var(--border-color-translucent),
--dialog-border-width: var(--border-width),
--dialog-border-radius: var(--radius-7),
--dialog-inner-border-radius: calc(var(--dialog-border-radius) - var(--dialog-border-width)),
--dialog-box-shadow: var(--box-shadow-xl),
--dialog-transition-property: "opacity, transform, visibility",
--dialog-transition-duration: .3s,
--dialog-transition-timing: var(--transition-timing-overlay),
--dialog-backdrop-bg: light-dark(rgb(0 0 0 / 50%), rgb(0 0 0 / 65%)),
--dialog-backdrop-blur: 8px,
--dialog-header-padding: 1rem,
--dialog-header-border-color: var(--border-color-translucent),
--dialog-header-border-width: var(--border-width),
--dialog-footer-padding: 1rem,
--dialog-footer-border-color: var(--border-color-translucent),
--dialog-footer-border-width: var(--border-width),
--dialog-footer-gap: .5rem,
),
$dialog-tokens
);
Sass 映射
对话框尺寸在 $dialog-sizes Sass 映射中定义。每个尺寸都指定对话框的 width。
$dialog-sizes: ();
// stylelint-disable-next-line scss/dollar-variable-default
$dialog-sizes: defaults(
(
sm: 280px,
lg: 800px,
xl: 1140px,
2xl: 1440px,
),
$dialog-sizes
);
JavaScript 行为
通过数据属性
无需编写 JavaScript 即可切换对话框。在控制器元素(如按钮)上设置 data-bs-toggle="dialog",同时设置 data-bs-target="#foo" 以定位要切换的特定对话框。
| 属性 | 描述 |
|---|---|
data-bs-toggle="dialog" | 从触发元素打开或切换对话框。 |
data-bs-target | 要显示或隐藏的对话框元素的 CSS 选择器。 |
data-bs-dismiss="dialog" | 在对话框内的控件上,激活时关闭对话框。 |
<button type="button" data-bs-toggle="dialog" data-bs-target="#myDialog">
Launch dialog
</button>
关闭
可以通过在对话框内按钮上使用 data-bs-dismiss 属性来实现关闭:
<button type="button" data-bs-dismiss="dialog">Close</button>
通过 JavaScript
用一行 JavaScript 创建对话框:
const myDialog = new bootstrap.Dialog('#myDialog')
依赖
The dialog plugin requires the following JavaScript files if you’re building Bootstrap’s JS from source:
| File | Description |
|---|---|
js/src/dialog.ts | Main dialog component |
js/src/base-component.ts | Base component class |
js/src/dialog-base.ts | Base dialog component |
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 |
js/src/util/scroll-lock.ts | Page scroll locking and scrollbar compensation |
选项
选项可以通过数据属性或 JavaScript 传递。对于数据属性,将选项名称附加到 data-bs- 之后,例如 data-bs-backdrop="static"。
| 名称 | 类型 | 默认值 | 描述 |
|---|---|---|---|
backdrop | boolean 或 'static' | true | 对于模态对话框,点击背景会关闭对话框。指定 static 以获得点击时不会关闭对话框的背景。对非模态对话框无影响。 |
keyboard | boolean | true | 按 Escape 键时关闭对话框。 |
modal | boolean | true | 当为 true 时,使用 showModal() 作为模态打开对话框,带有背景、焦点捕获和顶层渲染。当为 false 时,使用 show() 作为非模态对话框打开,没有背景或焦点捕获。 |
方法
用于显示或隐藏组件的方法会返回一个 promise。 该 promise 在过渡结束时 resolve,因此 await 会在与 shown.bs.* 或 hidden.bs.* 监听器触发的同一时刻继续执行。这些方法仍会立即返回,绝不会阻塞调用方。对正处于过渡中的组件进行调用不会产生任何效果,并会立即 resolve。在我们的 JavaScript 文档中了解更多。
传递选项
将你的内容激活为对话框。接受可选的选项对象。
const myDialog = new bootstrap.Dialog('#myDialog', {
keyboard: false
})
| 方法 | 描述 |
|---|---|
show | 打开对话框。返回一个 promise,在对话框显示后 resolves(即在 shown.bs.dialog 事件触发之后)。 |
hide | 隐藏对话框。返回一个 promise,在对话框隐藏后 resolves(即在 hidden.bs.dialog 事件触发之后)。 |
toggle | 切换对话框。返回一个 promise,在对话框显示或隐藏后 resolves(即在 shown.bs.dialog 或 hidden.bs.dialog 事件触发之后)。 |
handleUpdate | 为与模态框保持 API 一致而提供。原生对话框自行处理其定位。 |
dispose | 销毁元素的对话框。 |
getInstance | 静态方法,允许你获取与 DOM 元素关联的对话框实例。 |
getOrCreateInstance | 静态方法,允许你获取与 DOM 元素关联的对话框实例,如果尚未初始化则创建一个新实例。 |
事件
Bootstrap 在管理打开的对话框时使用其背景和键盘选项。当对话框关闭时,它会恢复作者的 closedby 值。原生 close() 方法和带有 method="dialog" 的表单可以立即关闭对话框。监听 hide.bs.dialog 以阻止 Bootstrap 隐藏。
Bootstrap 的对话框类公开了几个事件,用于挂钩对话框功能。
| 事件 | 描述 |
|---|---|
show.bs.dialog | 当调用 show 实例方法时立即触发。 |
shown.bs.dialog | 当对话框已对用户可见时触发(将等待 CSS 过渡完成)。 |
hide.bs.dialog | 当调用 hide 实例方法时立即触发。 |
hidden.bs.dialog | 在 Bootstrap 隐藏完成之后或浏览器关闭已初始化的对话框之后触发。 |
hidePrevented.bs.dialog | 当对话框已显示、其背景为 static,并执行了点击对话框外部或按下 Escape 键(且 keyboard 设置为 false)时触发。 |
cancel.bs.dialog | 当 Bootstrap 处理原生关闭请求或非模态对话框中的 Escape 时触发。使用 hide.bs.dialog 以阻止 Bootstrap 隐藏。 |
const myDialog = document.getElementById('myDialog')
myDialog.addEventListener('hidden.bs.dialog', event => {
// do something...
})