Bootstrap Modal (Modal) Plugin
A modal (Modal) is a child window that overlays the parent window. Typically, the purpose is to display content from a separate source, allowing some interaction without leaving the parent window. A child window can provide information, interaction, etc.
If you want to reference the plugin's functionality separately, you need to include modal.js. Or, as Bootstrap Plugins Overview chapter mentions, you can include bootstrap.js or the compressed version bootstrap.min.js。
Usage
You can toggle the hidden content of the modal (Modal) plugin:
- Via data attributes: set an attribute on the control element (such as a button or link) data-toggle="modal", and at the same time set data-target="#identifier" or href="#identifier" to specify the particular modal to toggle (with id="identifier").
- via JavaScript: using this technique, you can call a modal with id="identifier" through a simple line of JavaScript:
$('#identifier').modal(options)
Example
An example of a static modal window, as shown below:
Example
<
h2
Create a Modal (Modal)
h2
<button class=">btn btn-primary btn-lg
"
data-toggle
=
"
modal
"
data-target
=
"
#myModal
"
Start Demo of Modal
button
<div class=">modal fade
"
id
=
"
myModal
"
tabindex
=
"
-1
"
role
=
"
dialog
"
aria-labelledby
=
"
myModalLabel
"
aria-hidden
=
"
true
"
<div class="modal-dialog">
<div class="modal-content">
<div class="modal-header">
<button type="button" class="close" data-dismiss="modal" aria-hidden="true" ×button>
<h4 class="modal-title" id="myModalLabel">Modal (Modal) Title
h4
div
<div class="modal-body">Add some text here
div
<div class="modal-footer">
<button type="button" class=">btn btn-default
"
data-dismiss
=
"
modal
"
Close
button
<button type="button" class=">btn btn-primary
"
Commit changes
button
div
div
div
div
Try it »
The result is as follows:
Code explanation:
- To use a modal window, you need some kind of trigger. You can use a button or a link. Here we use a button.
- If you look carefully at the code above, you will find that in the
- There are two points to note in a modal:
- aria-labelledby="myModalLabel", this attribute references the title of the modal.
- property aria-hidden="true" used to keep the modal window invisible until the trigger is activated (such as clicking on the associated button).
-
, modal-header is the class that defines the style for the header of the modal window.
- class="close", close is a CSS class used to style the close button of the modal window.
- data-dismiss="modal", is a custom HTML5 data attribute. Here it is used to close the modal window.
- class="modal-body", is a CSS class of Bootstrap CSS, used to style the body of the modal window.
- class="modal-footer", is a CSS class of Bootstrap CSS, used to style the footer of the modal window.
- data-toggle="modal", the HTML5 custom data attribute data-toggle is used to open the modal window.
Option
There are some options that can be used to customize the appearance and feel of the modal window (Modal Window), which are passed through data attributes or JavaScript. The following table lists these options:
| Option name | Type / Default value | Data attribute name | Description |
|---|---|---|---|
| backdrop | boolean or string 'static' | ||
| Default value: true | data-backdrop | Specifies a static backdrop; the modal will not close when the user clicks outside of it. | |
| keyboard | boolean | ||
| Default value: true | data-keyboard | Closes the modal when the escape key is pressed; when set to false, the key has no effect. | |
| show | boolean | ||
| Default value: true | data-show | Shows the modal when initialized. | |
| remote | path | ||
| Default value: false | data-remote | Using jQuery .load method to inject content into the body of the modal. If an href with a valid URL is added, its content will be loaded. As shown in the example below: |
Method
Below are some useful methods that can be used with modal().
| Method | Description | Example |
|---|---|---|
| Options: .modal(options) | Activates the content as a modal. Accepts an optional options object. |
$('#identifier').modal({ keyboard: false })| onSelect | event | Toggle: .modal('toggle') | Manually toggles the modal. |
$('#identifier').modal('toggle')
| onSelect | event | Show: .modal('show') | Manually opens the modal. |
$('#identifier').modal('show')
| onSelect | event | Hide: .modal('hide') | Manually hides the modal. |
$('#identifier').modal('hide')
|
Example
The following example demonstrates the use of the methods:
Example
<div class=">modal fade
"
id
=
"
myModal
"
tabindex
=
"
-1
"
role
=
"
dialog
"
aria-labelledby
=
"
myModalLabel
"
aria-hidden
=
"
true
"
<div class="modal-dialog">
<div class="modal-content">
<div class="modal-header">
<button type="button" class="close" data-dismiss="modal" aria-hidden="true" ×button>
<h4 class="modal-title" id="myModalLabel">Modal (Modal) Title
h4
div
<div class="modal-body">Press the ESC button to exit.
div
<div class="modal-footer">
<button type="button" class=">btn btn-default
"
data-dismiss
=
"
modal
"
Close
button
<button type="button" class=">btn btn-primary
"
Commit changes
button
div
div
div
div
Try it »
The result is as follows:
Just press the ESC key and the modal window will exit.
Events
The following table lists the events used in modals. These events can be used as hooks in functions.
| Events | Description | Example |
|---|---|---|
| show.bs.modal | Fired after the show method is called. |
$('#identifier').on('show.bs.modal', function () { // do something... })| onSelect | event | shown.bs.modal | Fired when the modal becomes visible to the user (will wait for CSS transitions to complete). |
$('#identifier').on('shown.bs.modal', function () {
// Perform some actions...
})
| onSelect | event | hide.bs.modal | Fired when the hide instance method is called. |
$('#identifier').on('hide.bs.modal', function () {
// Perform some actions...
})
| onSelect | event | hidden.bs.modal | Fired when the modal has completely hidden from the user. |
$('#identifier').on('hidden.bs.modal', function () {
// Perform some actions...
})
|
Example
The following example demonstrates the use of the events:
Example
<
h2
Modal (Modal) Plugin Events
h2
<button class=">btn btn-primary btn-lg
"
data-toggle
=
"
modal
"
data-target
=
"
#myModal
"
Start Demo of Modal
button
<div class=">modal fade
"
id
=
"
myModal
"
tabindex
=
"
-1
"
role
=
"
dialog
"
aria-labelledby
=
"
myModalLabel
"
aria-hidden
=
"
true
"
<div class="modal-dialog">
<div class="modal-content">
<div class="modal-header">
<button type="button" class="close" data-dismiss="modal" aria-hidden="true" ×button>
<h4 class="modal-title" id="myModalLabel">Modal (Modal) Title
h4
div
<div class="modal-body">Click the close button to check the event functionality.
div
<div class="modal-footer">
<button type="button" class=">btn btn-default
"
data-dismiss
=
"
modal
"
Close
button
<button type="button" class=">btn btn-primary
"
Commit changes
button
div
div
div
div
<
script
$(function() { $('#myModal').modal('hide') });
script
<
script
$(function() { $('#myModal').on('hide.bs.modal', function() { alert('Hey, I heard you like modals...'); }) });
script
Try it »
The result is as follows:
As shown in the example above, if you click the Close button, namely the hide event, an alert message will be displayed.
If you want to reference the plugin's functionality separately, you need to include modal.js. Or, as