Bootstrap-Modal模态框插件

模态框(Modal)是覆盖在父窗体上的子窗体。通常,目的是显示来自一个单独的源的内容,可以在不离开父窗体的情况下有一些互动。子窗体可提供信息、交互等。

目录

Bootstrap-Modal模态框插件

模态框(Modal)是覆盖在父窗体上的子窗体。通常,目的是显示来自一个单独的源的内容,可以在不离开父窗体的情况下有一些互动。子窗体可提供信息、交互等。

基本代码清单

<!-- 模态框示例 -->
<button  class="btn btn-primary btn-sm" data-toggle="modal" data-target="#identifier">
    开始演示模态框
</button>
<!-- 模态框(Modal) -->
<div class="modal fade"  role="dialog" aria-labelledby="myModalLabel" aria-hidden="true">
    <div class="modal-dialog modal-sm">
        <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" ></h4>
            </div>
            <div class="modal-body">
                在这里添加一些文本
            </div>
            <div class="modal-footer">
                <button type="button" class="btn btn-default" data-dismiss="modal">关闭</button>
                <button type="button" class="btn btn-primary">提交更改</button>
            </div>
        </div><!-- /.modal-content -->
    </div><!-- /.modal -->
</div>
<!-- 模态框示例 -->

相关class属性:

  • .modal:用来把 <div> 的内容识别为模态框。 .fade:当模态框被切换时,它会引起内容淡入淡出。如果不需要模态框弹出时的动画效果(淡入淡出效果),删掉 .fade 类即可。

  • aria-labelledby="myModalLabel":引用模态框的标题。

  • aria-hidden="true"用于保持模态窗口不可见,直到触发器被触发为止(比如点击在相关的按钮上)。

  • class="modal-dialog modal-lg":大尺寸模态框;可选小尺寸:modal-dialog modal-sm

  • data-dismiss="modal":自定义的 HTML5 data 属性。在这里它被用于关闭模态窗口。

  • Bootstrap CSS 的一个 CSS class:

    modal-content:模态窗的全局定义。

    modal-header:模态窗的头部定义。

    modal-body:模态窗口的主体设置样式。

    modal-footer:模态窗口的底部设置样式。

其他选项

data-backdrop="static"

取值:boolean或字符串 \'static\'。默认值:true。

作用:指定一个静态的背景,当用户点击模态框外部时不会关闭模态框。


data-keyboard="true"

取值:boolean。默认值:true。

作用:键盘上的 esc 键被按下时关闭模态框。


data-show=“true”

取值:boolean。默认值:true。

作用:模态框初始化之后就立即显示出来。


相关方法:

.modal(options)

将页面中的某块内容作为模态框激活。接受可选参数 object

$(\'#identifier\').modal({
  keyboard: false,
  backdrop:\'static\'
})

.modal(\'toggle\')

切换模态框。在模态框显示或隐藏之前返回到主调函数中(也就是,在触发 shown.bs.modalhidden.bs.modal 事件之前)。

$(\'#identifier\').modal(\'toggle\');

.modal(\'show\')

手动打开模态框。在模态框显示之前返回到主调函数中(也就是,在触发 shown.bs.modal 事件之前)。

$(\'#identifier\').modal(\'show\');

.modal(\'hide\')

手动隐藏模态框。在模态框隐藏之前返回到主调函数中(也就是,在触发 hidden.bs.modal 事件之前)。

$(\'#identifier\').modal(\'hide\');

.modal(\'handleUpdate\')

重新调整模式的位置以与滚动条相对,以防出现滚动条,这会使模态窗跳转到左侧。仅当模态的高度在打开时发生变化时才需要。

$(\'#identifier\').modal(\'handleUpdate\');

相关事件:

show.bs.modal

show 方法调用之后立即触发该事件。如果是通过点击某个作为触发器的元素,则此元素可以通过事件的 relatedTarget 属性进行访问。

shown.bs.modal

此事件在模态框已经显示出来(并且同时在 CSS 过渡效果完成)之后被触发。如果是通过点击某个作为触发器的元素,则此元素可以通过事件的 relatedTarget 属性进行访问。

hide.bs.modal

hide 方法调用之后立即触发该事件。

hidden.bs.modal

此事件在模态框被隐藏(并且同时在 CSS 过渡效果完成)之后被触发。

loaded.bs.modal

远端的数据源加载完数据之后触发该事件。

调用事件示例:

$(\'#myModal\').on(\'hidden.bs.modal\', function (e) {
  // do something...
})

根据按钮改变模态框内容

有一堆按钮都触发相同的模式,只是内容略有不同?使用event.relatedTarget和[HTML data-*属性]根据所单击的按钮来更改模式的内容。有关以下内容的详细信息relatedTarget

例如发送邮件:

给予idmybuttonid的按钮设置自定义属性data-user的值为@ZhangSan

$(\'#mybuttonid\').data(\'user\',\'@ZhangSan\');
$(\'#identifier\').on(\'show.bs.modal\', function (event) {
    var button = $(event.relatedTarget); // Button that triggered the modal
    var recipient = button.data(\'user\');// 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);
})