模态是在其父窗口上分层的子窗口.通常,目的是显示来自单独源的内容,该源可以在不离开父窗口的情况下进行一些交互.子窗口可以提供信息,交互等.
如果要单独包含此插件功能,则需要 modal.js .另外,如 Bootstrap插件概述一章所述,您可以包含 bootstrap.js 或缩小 bootstrap.min.js .
用法
您可以切换模态插件的隐藏内容 :
通过数据属性 : 在控制器元素(如按钮或链接)上设置属性 data-toggle ="modal"以及 data-target ="#identifier"或 href ="#identifier"以特定模式(使用id ="identifier")进行切换.
通过JavaScript : 使用这种技术,您可以使用一行JavaScript来调用id ="identifier"的模态;
$('#identifier').modal(options)
示例
静态模态窗口示例显示在以下示例中 :
Example of creating Modals with Twitter Bootstrap
This Modal title
Add some text here
上述代码的详细信息:
1、要调用模态窗口,您需要具有某种触发器。您可以使用按钮或链接。这里我们使用了一个按钮。
2、如果您查看上面的代码,您将看到在
3、在模态和减号中有两个要注意的类;
4、第一个是.modal,它只是将
5、第二个是.fade类。切换模态时,会导致内容淡入淡出。
6、aria-labelledby ="myModalLabel",属性引用模态标题。
7、属性aria-hidden ="true"用于保持模态窗口不可见,直到触发器出现(如单击相关按钮)。
8、
9、class ="close",是一个CSS类关闭,它为模态窗口的"关闭"按钮设置样式。
10、data-dismiss ="modal",是一个自定义的HTML5数据属性。在这里它用于关闭模态窗口。
11、class ="modal-body",是一个CSS类的Bootstrap CSS,用于设置模态窗口主体的样式。
12、class ="modal-footer",是一个CSS类的Bootstrap CSS,用于设置模态窗口页脚的样式。
13、data-toggle ="modal",HTML5自定义数据属性数据切换用于打开模态窗口。
Options
有些选项可以通过数据属性或JavaScript传递,以自定义模态窗口的外观。 下表列出了选项:
Option Name | Type/Default Value | Data attribute name | Description |
---|---|---|---|
backdrop | boolean or the string 'static' Default: true | data-backdrop | Specify static for a backdrop, if you don’t want the modal to be closed when the user clicks outside of the modal. |
keyboard | boolean Default: true | data-keyboard | Closes the modal when escape key is pressed; set to false to disable. |
show | boolean Default: true | data-show | Shows the modal when initialized. |
remote | path Default: false | data-remote | Using the jQuery .load method, inject content into the modal body. If an href with a valid URL is added, it will load that content. An example of this is shown below − Click me |
Methods
Here are some useful methods that can be used with modal().
Method | Description | Example |
---|---|---|
Options :.modal(options) | Activates your content as a modal. Accepts an optional options object. | $('#identifier').modal({ keyboard: false}) |
Toggle :.modal('toggle') | Manually toggles a modal. | $('#identifier').modal('toggle') |
Show :.modal('show') | Manually opens a modal. | $('#identifier').modal('show') |
Hide:.modal('hide') | Manually hides a modal. | $('#identifier').modal('hide') |
Example
T以下示例演示了方法的用法:
Example of using methods of Modal Plugin
This Modal title
Press ESC button to exit.
只需单击Esc按钮,即可退出模态窗口。
Events
下表列出了使用模态的事件。 这些事件可用于挂钩函数。
Event | Description | Example |
---|---|---|
show.bs.modal | Fired after the show method is called. | $('#identifier').on('show.bs.modal', function () { // do something…}) |
shown.bs.modal | Fired when the modal has been made visible to the user (will wait for CSS transitions to complete). | $('#identifier').on('shown.bs.modal', function () { // do something…}) |
hide.bs.modal | Fired when the hide instance method has been called. | $('#identifier').on('hide.bs.modal', function () { // do something…}) |
hidden.bs.modal | Fired when the modal has finished being hidden from the user. | $('#identifier').on('hidden.bs.modal', function () { // do something…}) |
Example
以下示例演示了事件的用法:
Example of using events of Modal Plugin
This Modal title
Click on close button to check Event functionality.
如上图所示,如果您点击关闭按钮即隐藏事件,则会显示警告信息.