将一个不可关闭的 Twitter Bootstrap Modal 转换为可关闭的

7

我正在使用Twitter Bootstrap创建一个无法关闭的模态框,这是故意的。但是,十秒钟后,我希望用户能够通过按下“Esc”键或在模态框外单击来关闭模态框。这可以实现吗?以下是示例代码:

<!DOCTYPE html>
<html lang="en">
    <head>
        <meta charset="utf-8">
        <title></title>
        <link href="http://netdna.bootstrapcdn.com/twitter-bootstrap/2.3.1/css/bootstrap-combined.min.css" rel="stylesheet">
    </head>
    <body>
        <!-- Button to trigger modal -->
        <a href="#myModal" role="button" class="btn" data-toggle="modal">Launch demo modal</a>

        <!-- Modal -->
        <div id="myModal" class="modal hide fade" tabindex="-1" role="dialog" aria-labelledby="myModalLabel" aria-hidden="true" data-backdrop="static" data-keyboard="false">
            <div class="modal-header">
                <h3 id="myModalLabel">A Modal That Can't Be Closed</h3>
            </div>
            <div class="modal-body">
                <p>There is no way to close this modal. I would like to make it so that after ten seconds, pressing the escape key or clicking outside the modal causes the modal to close.</p>
            </div>
        </div>

        <script src="http://ajax.googleapis.com/ajax/libs/jquery/1.9.1/jquery.min.js"></script>
        <script src="http://netdna.bootstrapcdn.com/twitter-bootstrap/2.3.1/js/bootstrap.min.js"></script>
        <script>
            $('#myModal').on('shown', function() {
                setTimeout(function() {
                    alert('The modal has been open for 10 seconds.');
                }, 10000);
            });
        </script>
    </body>
</html>

我创建了一个功能请求,以添加一种在创建后更新模态选项的方法。 https://github.com/twbs/bootstrap/issues/35664 - tvanc
2个回答

11

不幸的是,最清晰的方法涉及调用模态框对象的一些方法,尽管这些方法在技术上是公共的,但它们并不一定是公共API的一部分。这意味着,如果你把这段代码投入生产,你必须小心检查升级时 bootstrap-modal.js 实现的变化。以下是如何操作:

JS

$('#myModal').on('shown', function() {
  // hold a ref to the modal object
  var mdl = $(this).data('modal')

  setTimeout(function() {
    console.log('The modal has been open for 10 seconds.');

    // enable keyboard escaping
    mdl.options.keyboard = true
    mdl.escape()

    // but re-disable it for future show events ;)
    mdl.options.keyboard = false

    // replace refocus handler with hide() invocation
    mdl.$backdrop.off('click')
    mdl.$backdrop.click($.proxy(mdl.hide, mdl))

  }, 10000); // ten seconds
});

Plunk

示例中相关代码位于 script.js,默认为3秒。


成功了!谢谢!我也感谢你提醒代码可能会在未来版本的Twitter Bootstrap中出现问题。 - Nick
4
好的回答。此外,对于Bootstrap 3,需要使用data('bs.modal')。 - Alexandros V
1
有人能够让 Bootstrap 3 模态框外的点击事件正常工作吗? - bbodenmiller
@bbodenmiller:对于Bootstrap 3,您需要添加mdl.options.backdrop = true;以使其正常工作。 - Jacob van Lingen

1
这是我的解决方案:http://jsfiddle.net/g5bDC/1/
只需添加一个禁用的按钮(此处为页脚),并在 X 秒后启用它。
HTML 代码如下:
<div class="modal-footer"> 
    <a href="#" class="btn" disabled=disabled id="closeBtn" aria-hidden="true">Close</a>
</div>

JS :

$('#myModal').on('shown', function () {
    setTimeout(function () {
        $('#closeBtn').removeAttr('disabled').attr('data-dismiss', 'modal');
    }, 5000);
});

编辑: 根据MartinHN的说法,这是更新版本:http://jsfiddle.net/g5bDC/2/

现在JS是:

$('#myModal').on('show', function() {
    $('#closeBtn').attr('disabled', 'disabled');
});

$('#myModal').on('shown', function() {
    setTimeout(function() {
        $('#closeBtn').removeAttr('disabled').attr('data-dismiss', 'modal');
    }, 2000);
});

2
一个小的改进是在调用setTimeout之前设置disabled属性,这样如果您在不刷新页面的情况下再次启动模态框,它就可以正常工作。 - MartinHN
1
谢谢,但我特别要寻找的是“Esc”键和在模态框以外点击的功能。 - Nick

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接