Twitter Bootstrap模态窗口闪烁

she*_*nyl 11 jquery modal-dialog twitter-bootstrap

我想使用Ajax在模态窗口中插入一些东西,所以我尝试手动打开模态窗口.代码看起来像这样

    $(function(){
        $('#modal-from-dom').modal({
            backdrop: true,
            keyboard: true
        });
        $('#modalbutton').click(function(){

            $('#my-modal').bind('show', function () {
                // do sth with the modal
            });
            $('#modal-from-dom').modal('toggle');

            return false;
        });
    });
Run Code Online (Sandbox Code Playgroud)

HTML直接从bootstrap js页面复制

<div id="modal-from-dom" class="modal hide fade">
    <div class="modal-header">
      <a href="#" class="close">×</a>
      <h3>Modal Heading</h3>
    </div>
    <div class="modal-body">
      <p>One fine body…</p>
    </div>
    <div class="modal-footer">
      <a href="#" class="btn primary">Primary</a>
      <a href="#" class="btn secondary">Secondary</a>
    </div>
</div>
<button id="modalbutton" class="btn">Launch Modal</button>
Run Code Online (Sandbox Code Playgroud)

所以问题是第一次点击按钮似乎工作正常,在第二次点击模式窗口显示1秒左右然后消失.如果我将"切换"更改为"显示",则在第二次单击后,背景将不会完全淡出.我该怎么调试呢?

Neo*_*tis 3

您当前执行操作的方式可能是导致闪烁的原因。本质上,您告诉浏览器的是:在显示 div 后更新内容。您还告诉 jQuery 在每次单击链接时绑定 onShow 事件。该绑定声明应在 onClick 事件之外进行。

您应该尝试的是在显示模态内容之前更改它,允许浏览器在显示之前适当调整 DOM,从而减少(如果不能消除的话)闪烁。

尝试更多类似这样的事情:

$(function(){
    $('#modal-from-dom').modal({
        backdrop: true,
        keyboard: true
    });
    $('#modalbutton').click(function(){

        // do what you need to with the modal content here

        // then show it after the changes have been made

        $('#modal-from-dom').modal('toggle');
        return false;
    });
});
Run Code Online (Sandbox Code Playgroud)