Source模态
使用 Bootstrap 的 JavaScript 模态插件向您的站点添加对话框,用于灯箱、用户通知或完全自定义的内容。
这个怎么运作
在开始使用 Bootstrap 的模态组件之前,请务必阅读以下内容,因为我们的菜单选项最近发生了变化。
模态框是用 HTML、CSS 和 JavaScript 构建的。它们位于文档中的所有其他内容之上,并从中删除滚动,<body>
以便模态内容滚动。
单击模态“背景”将自动关闭模态。
Bootstrap 一次只支持一个模式窗口。不支持嵌套模式,因为我们认为它们是糟糕的用户体验。
模态使用position: fixed
,它的渲染有时可能有点特别。只要有可能,将您的模态 HTML 放在顶级位置,以避免来自其他元素的潜在干扰。.modal
在另一个固定元素中嵌套 a 时,您可能会遇到问题。
再一次,由于position: fixed
在移动设备上使用模态框有一些注意事项。有关详细信息,请参阅我们的浏览器支持文档 。
由于 HTML5 定义其语义的方式, autofocus
HTML 属性 在 Bootstrap 模式中无效。要达到相同的效果,请使用一些自定义 JavaScript:
复制
$ ( '#myModal' ). on ( 'shown.bs.modal' , function () {
$ ( '#myInput' ). trigger ( 'focus' )
})
继续阅读演示和使用指南。
例子
模态组件
下面是一个静态 模态示例(意味着它的position
和display
已被覆盖)。包括模态标题、模态正文(需要padding
)和模态页脚(可选)。我们要求您尽可能在模式标题中包含解除操作,或提供另一个明确的解除操作。
复制
<div class= "modal" tabindex= "-1" role= "dialog" >
<div class= "modal-dialog" role= "document" >
<div class= "modal-content" >
<div class= "modal-header" >
<h5 class= "modal-title" > Modal title</h5>
<button type= "button" class= "close" data-dismiss= "modal" aria-label= "Close" >
<span aria-hidden= "true" > × </span>
</button>
</div>
<div class= "modal-body" >
<p> Modal body text goes here.</p>
</div>
<div class= "modal-footer" >
<button type= "button" class= "btn btn-secondary" data-dismiss= "modal" > Close</button>
<button type= "button" class= "btn btn-primary" > Save changes</button>
</div>
</div>
</div>
</div>
现场演示
通过单击下面的按钮来切换工作模式演示。它将向下滑动并从页面顶部淡入。
启动演示模式
复制
<!-- Button trigger modal -->
<button type= "button" class= "btn btn-primary" data-toggle= "modal" data-target= "#exampleModal" >
Launch demo modal
</button>
<!-- Modal -->
<div class= "modal fade" id= "exampleModal" tabindex= "-1" role= "dialog" aria-labelledby= "exampleModalLabel" aria-hidden= "true" >
<div class= "modal-dialog" role= "document" >
<div class= "modal-content" >
<div class= "modal-header" >
<h5 class= "modal-title" id= "exampleModalLabel" > Modal title</h5>
<button type= "button" class= "close" data-dismiss= "modal" aria-label= "Close" >
<span aria-hidden= "true" > × </span>
</button>
</div>
<div class= "modal-body" >
...
</div>
<div class= "modal-footer" >
<button type= "button" class= "btn btn-secondary" data-dismiss= "modal" > Close</button>
<button type= "button" class= "btn btn-primary" > Save changes</button>
</div>
</div>
</div>
</div>
滚动长内容
当模式对于用户的视口或设备而言变得太长时,它们会独立于页面本身滚动。试试下面的演示,看看我们的意思。
Cras mattis consectetur purus sit amet fermentum. Cras justo odio, dapibus ac facilisis in, egestas eget quam. Morbi leo risus, porta ac consectetur ac, vestibulum at eros.
Praesent commodo cursus magna, vel scelerisque nisl consectetur et. Vivamus sagittis lacus vel augue laoreet rutrum faucibus dolor auctor.
Aenean lacinia bibendum nulla sed consectetur. Praesent commodo cursus magna, vel scelerisque nisl consectetur et. Donec sed odio dui. Donec ullamcorper nulla non metus auctor fringilla.
Cras mattis consectetur purus sit amet fermentum. Cras justo odio, dapibus ac facilisis in, egestas eget quam. Morbi leo risus, porta ac consectetur ac, vestibulum at eros.
Praesent commodo cursus magna, vel scelerisque nisl consectetur et. Vivamus sagittis lacus vel augue laoreet rutrum faucibus dolor auctor.
Aenean lacinia bibendum nulla sed consectetur. Praesent commodo cursus magna, vel scelerisque nisl consectetur et. Donec sed odio dui. Donec ullamcorper nulla non metus auctor fringilla.
Cras mattis consectetur purus sit amet fermentum. Cras justo odio, dapibus ac facilisis in, egestas eget quam. Morbi leo risus, porta ac consectetur ac, vestibulum at eros.
Praesent commodo cursus magna, vel scelerisque nisl consectetur et. Vivamus sagittis lacus vel augue laoreet rutrum faucibus dolor auctor.
Aenean lacinia bibendum nulla sed consectetur. Praesent commodo cursus magna, vel scelerisque nisl consectetur et. Donec sed odio dui. Donec ullamcorper nulla non metus auctor fringilla.
Cras mattis consectetur purus sit amet fermentum. Cras justo odio, dapibus ac facilisis in, egestas eget quam. Morbi leo risus, porta ac consectetur ac, vestibulum at eros.
Praesent commodo cursus magna, vel scelerisque nisl consectetur et. Vivamus sagittis lacus vel augue laoreet rutrum faucibus dolor auctor.
Aenean lacinia bibendum nulla sed consectetur. Praesent commodo cursus magna, vel scelerisque nisl consectetur et. Donec sed odio dui. Donec ullamcorper nulla non metus auctor fringilla.
Cras mattis consectetur purus sit amet fermentum. Cras justo odio, dapibus ac facilisis in, egestas eget quam. Morbi leo risus, porta ac consectetur ac, vestibulum at eros.
Praesent commodo cursus magna, vel scelerisque nisl consectetur et. Vivamus sagittis lacus vel augue laoreet rutrum faucibus dolor auctor.
Aenean lacinia bibendum nulla sed consectetur. Praesent commodo cursus magna, vel scelerisque nisl consectetur et. Donec sed odio dui. Donec ullamcorper nulla non metus auctor fringilla.
Cras mattis consectetur purus sit amet fermentum. Cras justo odio, dapibus ac facilisis in, egestas eget quam. Morbi leo risus, porta ac consectetur ac, vestibulum at eros.
Praesent commodo cursus magna, vel scelerisque nisl consectetur et. Vivamus sagittis lacus vel augue laoreet rutrum faucibus dolor auctor.
Aenean lacinia bibendum nulla sed consectetur. Praesent commodo cursus magna, vel scelerisque nisl consectetur et. Donec sed odio dui. Donec ullamcorper nulla non metus auctor fringilla.
启动演示模式
复制
<!-- Button trigger modal -->
<button type= "button" class= "btn btn-primary" data-toggle= "modal" data-target= "#exampleModalLong" >
Launch demo modal
</button>
<!-- Modal -->
<div class= "modal fade" id= "exampleModalLong" tabindex= "-1" role= "dialog" aria-labelledby= "exampleModalLongTitle" aria-hidden= "true" >
<div class= "modal-dialog" role= "document" >
<div class= "modal-content" >
<div class= "modal-header" >
<h5 class= "modal-title" id= "exampleModalLongTitle" > Modal title</h5>
<button type= "button" class= "close" data-dismiss= "modal" aria-label= "Close" >
<span aria-hidden= "true" > × </span>
</button>
</div>
<div class= "modal-body" >
...
</div>
<div class= "modal-footer" >
<button type= "button" class= "btn btn-secondary" data-dismiss= "modal" > Close</button>
<button type= "button" class= "btn btn-primary" > Save changes</button>
</div>
</div>
</div>
</div>
垂直居中
添加.modal-dialog-centered
到.modal-dialog
垂直居中模式。
Cras mattis consectetur purus sit amet fermentum. Cras justo odio, dapibus ac facilisis in, egestas eget quam. Morbi leo risus, porta ac consectetur ac, vestibulum at eros.
启动演示模式
复制
<!-- Button trigger modal -->
<button type= "button" class= "btn btn-primary" data-toggle= "modal" data-target= "#exampleModalCenter" >
Launch demo modal
</button>
<!-- Modal -->
<div class= "modal fade" id= "exampleModalCenter" tabindex= "-1" role= "dialog" aria-labelledby= "exampleModalCenterTitle" aria-hidden= "true" >
<div class= "modal-dialog modal-dialog-centered" role= "document" >
<div class= "modal-content" >
<div class= "modal-header" >
<h5 class= "modal-title" id= "exampleModalCenterTitle" > Modal title</h5>
<button type= "button" class= "close" data-dismiss= "modal" aria-label= "Close" >
<span aria-hidden= "true" > × </span>
</button>
</div>
<div class= "modal-body" >
...
</div>
<div class= "modal-footer" >
<button type= "button" class= "btn btn-secondary" data-dismiss= "modal" > Close</button>
<button type= "button" class= "btn btn-primary" > Save changes</button>
</div>
</div>
</div>
</div>
工具提示 和弹出框 可以根据需要放置在模式中。当模态框关闭时,其中的任何工具提示和弹出框也会自动关闭。
Popover in a modal
This button triggers a popover on click.
Tooltips in a modal
This link and that link have tooltips on hover.
启动演示模式
复制
<div class= "modal-body" >
<h5> Popover in a modal</h5>
<p> This <a href= "#" role= "button" class= "btn btn-secondary popover-test" title= "Popover title" data-content= "Popover body content is set in this attribute." > button</a> triggers a popover on click.</p>
<hr>
<h5> Tooltips in a modal</h5>
<p><a href= "#" class= "tooltip-test" title= "Tooltip" > This link</a> and <a href= "#" class= "tooltip-test" title= "Tooltip" > that link</a> have tooltips on hover.</p>
</div>
使用网格
通过嵌套.container-fluid
在.modal-body
. 然后,像在其他任何地方一样使用普通的网格系统类。
.col-md-4
.col-md-4 .ml-auto
.col-md-3 .ml-auto
.col-md-2 .ml-auto
Level 1: .col-sm-9
Level 2: .col-8 .col-sm-6
Level 2: .col-4 .col-sm-6
启动演示模式
复制
<div class= "modal-body" >
<div class= "container-fluid" >
<div class= "row" >
<div class= "col-md-4" > .col-md-4</div>
<div class= "col-md-4 ml-auto" > .col-md-4 .ml-auto</div>
</div>
<div class= "row" >
<div class= "col-md-3 ml-auto" > .col-md-3 .ml-auto</div>
<div class= "col-md-2 ml-auto" > .col-md-2 .ml-auto</div>
</div>
<div class= "row" >
<div class= "col-md-6 ml-auto" > .col-md-6 .ml-auto</div>
</div>
<div class= "row" >
<div class= "col-sm-9" >
Level 1: .col-sm-9
<div class= "row" >
<div class= "col-8 col-sm-6" >
Level 2: .col-8 .col-sm-6
</div>
<div class= "col-4 col-sm-6" >
Level 2: .col-4 .col-sm-6
</div>
</div>
</div>
</div>
</div>
</div>
改变模态内容
有一堆按钮都触发相同的模式,内容略有不同?使用event.relatedTarget
和HTMLdata-*
属性 (可能通过 jQuery )根据单击的按钮来改变模式的内容。
下面是一个现场演示,后面是示例 HTML 和 JavaScript。有关更多信息,请阅读模态事件文档 以获取有关relatedTarget
.
@mdo 的打开模式
@fat 的打开模式
@getbootstrap 的打开模式
复制
<button type= "button" class= "btn btn-primary" data-toggle= "modal" data-target= "#exampleModal" data-whatever= "@mdo" > Open modal for @mdo</button>
<button type= "button" class= "btn btn-primary" data-toggle= "modal" data-target= "#exampleModal" data-whatever= "@fat" > Open modal for @fat</button>
<button type= "button" class= "btn btn-primary" data-toggle= "modal" data-target= "#exampleModal" data-whatever= "@getbootstrap" > Open modal for @getbootstrap</button>
<div class= "modal fade" id= "exampleModal" tabindex= "-1" role= "dialog" aria-labelledby= "exampleModalLabel" aria-hidden= "true" >
<div class= "modal-dialog" role= "document" >
<div class= "modal-content" >
<div class= "modal-header" >
<h5 class= "modal-title" id= "exampleModalLabel" > New message</h5>
<button type= "button" class= "close" data-dismiss= "modal" aria-label= "Close" >
<span aria-hidden= "true" > × </span>
</button>
</div>
<div class= "modal-body" >
<form>
<div class= "form-group" >
<label for= "recipient-name" class= "col-form-label" > Recipient:</label>
<input type= "text" class= "form-control" id= "recipient-name" >
</div>
<div class= "form-group" >
<label for= "message-text" class= "col-form-label" > Message:</label>
<textarea class= "form-control" id= "message-text" ></textarea>
</div>
</form>
</div>
<div class= "modal-footer" >
<button type= "button" class= "btn btn-secondary" data-dismiss= "modal" > Close</button>
<button type= "button" class= "btn btn-primary" > Send message</button>
</div>
</div>
</div>
</div>
复制
$ ( '#exampleModal' ). on ( 'show.bs.modal' , function ( event ) {
var button = $ ( event . relatedTarget ) // Button that triggered the modal
var recipient = button . data ( 'whatever' ) // Extract info from data-* attributes
// If necessary, you could initiate an AJAX request here (and then do the updating in a callback).
// Update the modal's content. We'll use jQuery here, but you could use a data binding library or other methods instead.
var modal = $ ( this )
modal . find ( '.modal-title' ). text ( 'New message to ' + recipient )
modal . find ( '.modal-body input' ). val ( recipient )
})
更改动画
变量决定模态淡入动画前$modal-fade-transform
的变换状态,变量决定模态淡入动画结束时的变换。.modal-dialog
$modal-show-transform
.modal-dialog
例如,如果您想要放大动画,您可以设置$modal-fade-transform: scale(.8)
.
移除动画
对于只是出现而不是淡入查看.fade
的模态,请从模态标记中删除该类。
复制
<div class= "modal" tabindex= "-1" role= "dialog" aria-labelledby= "..." aria-hidden= "true" >
...
</div>
动态高度
如果模态框在打开时的高度发生变化,您应该调用$('#myModal').modal('handleUpdate')
以重新调整模态框的位置,以防出现滚动条。
可访问性
一定要添加role="dialog"
和aria-labelledby="..."
,引用模式标题,到,.modal
和本身。此外,您可以使用on 来描述您的模态对话框。role="document"
.modal-dialog
aria-describedby
.modal
嵌入 YouTube 视频
在模态中嵌入 YouTube 视频需要额外的 JavaScript,而不是 Bootstrap 来自动停止播放等等。有关更多信息,请参阅这篇有用的 Stack Overflow 帖子 。
可选尺寸
模态框有三种可选尺寸,可通过修饰符类放置在.modal-dialog
. 这些尺寸在某些断点处开始,以避免在较窄的视口上出现水平滚动条。
尺寸
班级
模态最大宽度
小的
.modal-sm
300px
默认
没有任何
500px
大的
.modal-lg
800px
特大号
.modal-xl
1140px
我们默认的不带修饰符类的模态构成了“中等”大小的模态。
超大模态
大模态
小模态
复制
<!-- Extra large modal -->
<button type= "button" class= "btn btn-primary" data-toggle= "modal" data-target= ".bd-example-modal-xl" > Extra large modal</button>
<div class= "modal fade bd-example-modal-xl" tabindex= "-1" role= "dialog" aria-labelledby= "myExtraLargeModalLabel" aria-hidden= "true" >
<div class= "modal-dialog modal-xl" >
<div class= "modal-content" >
...
</div>
</div>
</div>
<!-- Large modal -->
<button type= "button" class= "btn btn-primary" data-toggle= "modal" data-target= ".bd-example-modal-lg" > Large modal</button>
<div class= "modal fade bd-example-modal-lg" tabindex= "-1" role= "dialog" aria-labelledby= "myLargeModalLabel" aria-hidden= "true" >
<div class= "modal-dialog modal-lg" >
<div class= "modal-content" >
...
</div>
</div>
</div>
<!-- Small modal -->
<button type= "button" class= "btn btn-primary" data-toggle= "modal" data-target= ".bd-example-modal-sm" > Small modal</button>
<div class= "modal fade bd-example-modal-sm" tabindex= "-1" role= "dialog" aria-labelledby= "mySmallModalLabel" aria-hidden= "true" >
<div class= "modal-dialog modal-sm" >
<div class= "modal-content" >
...
</div>
</div>
</div>
用法
模态插件通过数据属性或 JavaScript 按需切换隐藏内容。它还添加.modal-open
到<body>
覆盖默认滚动行为并生成一个.modal-backdrop
以提供单击区域,以便在单击模态框外时关闭显示的模态框。
通过数据属性
无需编写 JavaScript 即可激活模式。在控制器元素上设置data-toggle="modal"
,如按钮,以及一个data-target="#foo"
或href="#foo"
以特定模式为目标进行切换。
复制
<button type= "button" data-toggle= "modal" data-target= "#myModal" > Launch modal</button>
通过 JavaScript
myModal
使用单行 JavaScript调用带有 id 的模式:
复制
$ ( '#myModal' ). modal ( options )
选项
选项可以通过数据属性或 JavaScript 传递。对于数据属性,将选项名称附加到 中data-
,如data-backdrop=""
.
姓名
类型
默认
描述
背景
布尔值或字符串'static'
真的
包括一个模态背景元素。或者,指定static
在单击时不关闭模式的背景。
键盘
布尔值
真的
按下退出键时关闭模态
重点
布尔值
真的
初始化时将焦点放在模式上。
节目
布尔值
真的
初始化时显示模态。
方法
.modal(options)
将您的内容激活为模式。接受一个可选选项object
。
复制
$ ( '#myModal' ). modal ({
keyboard : false
})
.modal('toggle')
手动切换模式。在模态框实际显示或隐藏之前(即在or事件发生之前)返回给调用者。 shown.bs.modal
hidden.bs.modal
复制
$ ( '#myModal' ). modal ( 'toggle' )
.modal('show')
手动打开一个模态。在模态实际显示之前(即在事件发生之前)返回给调用者。 shown.bs.modal
复制
$ ( '#myModal' ). modal ( 'show' )
.modal('hide')
手动隐藏模态。在模态框实际上被隐藏之前(即在事件发生之前)返回给调用者。 hidden.bs.modal
复制
$ ( '#myModal' ). modal ( 'hide' )
.modal('handleUpdate')
如果模态在打开时的高度发生变化(即出现滚动条时),手动重新调整模态的位置。
复制
$ ( '#myModal' ). modal ( 'handleUpdate' )
.modal('dispose')
销毁元素的模态。
活动
Bootstrap 的模态类公开了一些用于连接模态功能的事件。所有模态事件都在模态本身触发(即在<div class="modal">
)。
事件类型
描述
show.bs.modal
show
调用实例方法时立即触发此事件。如果由单击引起,则单击的元素可用作relatedTarget
事件的属性。
显示.bs.modal
当模式对用户可见时触发此事件(将等待 CSS 转换完成)。如果由单击引起,则单击的元素可用作relatedTarget
事件的属性。
hide.bs.modal
hide
调用实例方法时立即触发此事件。
hidden.bs.modal
当模态对用户隐藏完成时会触发此事件(将等待 CSS 转换完成)。
复制
$ ( '#myModal' ). on ( 'hidden.bs.modal' , function ( e ) {
// do something...
})