Bootstrap Modal Plugin

A 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. Child windows can provide information, interaction, etc.

If you want to reference this plugin's functionality separately, then you need to referencemodal.js. Or, asBootstrap Plugins Overviewmentioned in the chapter, you can referencebootstrap.jsor the compressed version ofbootstrap.min.js。

Usage

You can toggle the hidden content of the Modal plugin:

  • Via data attributes: Set attributes on a controller element (such as a button or link)data-toggle="modal", and setdata-target="#identifier"orhref="../index.html"to specify the particular modal to toggle (with id="identifier").
  • Via JavaScript: Using this technique, you can invoke a modal with id="identifier" with a simple one line of JavaScript:
    $('#identifier').modal(options)
    

Example

A static modal window example, as shown in the example below:

Example

<h2>Create Modal</h2> <!--Button triggers modal--> <button class="btn btn-primary btn-lg" data-toggle="modal" data-target="#myModal">Launch demo modal</button> <!--Modal--> <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">&times;</button> <h4 class="modal-title" id="myModalLabel">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">Submit changes</button> </div> </div><!-- /.modal-content --> </div><!-- /.modal --> </div>

Try it »

The result is as follows:

模态框(Modal)插件

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 in the <button> tag,data-target="#myModal"is the target of the modal you want to load on the page. You can create multiple modals on the page, and then create different triggers for each modal. Now, obviously, you cannot load multiple modals at the same time, but you can create multiple on the page to load at different times.
  • There are two things to note in the modal:
    1. First is.modal, used to identify the content of the <div> as a modal.
    2. Second is.fadeclass. When the modal is toggled, it causes the content to fade in and out.
  • aria-labelledby="myModalLabel", this attribute references the title of the modal.
  • Attributearia-hidden="true"is used to keep the modal window invisible until the trigger is activated (for example, clicking on the relevant button).
  • <div class="modal-header">, modal-header is a class that defines styles 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 in Bootstrap CSS, used to style the body of the modal window.
  • class="modal-footer", is a CSS class in Bootstrap CSS, used to style the footer of the modal window.
  • data-toggle="modal", HTML5 custom data attribute data-toggle is used to open the modal window.

Options

There are some options that can be used to customize the look and feel of the modal window. They are passed via data attributes or JavaScript. The following table lists these options:

Option NameType/Default ValueData Attribute NameDescription
backdropboolean or string 'static'
Default value: true
data-backdropSpecifies a static backdrop; when the user clicks outside the modal, the modal will not close.
keyboardboolean
Default value: true
data-keyboardCloses the modal when the Escape key is pressed. If set to false, the key has no effect.
showboolean
Default value: true
data-showShows the modal when initialized.
remotepath
Default value: false
data-remoteUsing jQuery.loadmethod, injects content into the modal body. If an href with a valid URL is added, the content will be loaded from it. As shown in the example below:
<a data-toggle="modal" href="remote.html" data-target="#modal" rel="noopener noreferrer">请点击我</a>

Methods

Below are some useful methods that can be used with modal().

MethodDescriptionExample
Options: .modal(options)Activates the content as a modal. Accepts an optional options object.
$('#identifier').modal({
keyboard: false
})
Toggle: .modal('toggle')Manually toggles the modal.
$('#identifier').modal('toggle')
Show: .modal('show')Manually opens the modal.
$('#identifier').modal('show')
Hide: .modal('hide')Manually hides the modal.
$('#identifier').modal('hide')

Example

The following example demonstrates the usage of the methods:

Example

<!--Modal--> <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 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">Submit changes</button> </div> </div><!-- /.modal-content --> </div><!-- /.modal-dialog --> </div> <!-- /.modal -->

Try it yourself »

The result is as follows:

模态框(Modal)插件方法

Just press the ESC key, and the modal window will exit.

Events

The following table lists the events used in the modal. These events can be used as hooks in functions.

EventDescriptionExample
show.bs.modalTriggered after the show method is called.
$('#identifier').on('show.bs.modal', function () {
  // 执行一些动作...
})
shown.bs.modalTriggered when the modal is visible to the user (will wait for CSS transitions to complete).
$('#identifier').on('shown.bs.modal', function () {
  // 执行一些动作...
})
hide.bs.modalTriggered when the hide instance method is called.
$('#identifier').on('hide.bs.modal', function () {
  // 执行一些动作...
})
hidden.bs.modalTriggered when the modal is completely hidden from the user.
$('#identifier').on('hidden.bs.modal', function () {
  // 执行一些动作...
})

Example

The following example demonstrates the usage of the events:

Example

<!--Modal--> <h2>Modal plugin events</h2> <!--Button triggers modal--> <button class="btn btn-primary btn-lg" data-toggle="modal" data-target="#myModal">Start demo modal</button> <!--Modal--> <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 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">Submit changes</button> </div> </div><!-- /.modal-content --> </div><!-- /.modal-dialog --> </div> <!-- /.modal --> <script> $(function() { $('#myModal').modal('hide') }); </script> <script>$(function() { $('#myModal').on('hide.bs.modal', function() { alert('Hey, I heard you like modals...'); }) });</script>

Try it yourself »

The result is as follows:

模态框(Modal)插件事件

As shown in the example above, if you click theClosebutton, that is, thehideevent, an alert message will be displayed.

Other extensions