Twitter Bootstrap 模态窗口闪烁

发布于 2024-12-11 04:38:56 字数 1166 浏览 0 评论 0原文

我想使用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;
        });
    });

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>

所以问题是第一次单击按钮似乎工作得很好,第二次单击后模式窗口显示 1 秒左右然后消失。如果我将“切换”更改为“显示”,第二次单击后背景将不会完全淡出。我该如何调试这个?

I want to insert something inside the modal window using Ajax, so I tried to manually open the modal window. The code looks like this

    $(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;
        });
    });

The HTML is copied straight from bootstrap js page

<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>

So the problem is clicking the button the first time seems to work just fine, after the second click the modal window shows for 1 second or so and then disappears. If I change 'toggle' to 'show', after the second click the backdrop won't fade out completely. How can I debug this?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

残龙傲雪 2024-12-18 04:38:56

您当前执行操作的方式可能是导致闪烁的原因。本质上,您告诉浏览器的是:在显示 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;
    });
});

The way you are currently performing actions is likely the cause of the flickering. Essentially, what you are telling the browser is: update the content after the div has been shown. You are also telling jQuery to bind the onShow event EVERY time the link is clicked. That bind declaration should be made outside of the onClick event.

What you should try, is to change your modal content BEFORE displaying it, allowing the browser to adjust the DOM appropriately before displaying it, reducing (if not eliminating) the flicker.

Try something more like this:

$(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;
    });
});
甜宝宝 2024-12-18 04:38:56

    || 模态解决方案

就我而言,我在悬停时出现了口吃/闪烁/讨人喜欢的模式。解决方案是:不要将模态放在使用 z-index 的元素内部,例如 或 ,将模态的代码放在外部,如下所述: https://github.com/twbs/bootstrap/issues/25206

SOLUTION FOR MODALS IN <ul> || <ol>

In my case I had a stuttering/flickering/flattering modal on hover. The solution is: Don't place your modal inside of an element, that uses z-index, for example or , put your code for the modal outside as mentioned here: https://github.com/twbs/bootstrap/issues/25206

川水往事 2024-12-18 04:38:56
*if you get error like ... modal is not a function  so u can do like this also *

$("#open-modal").click(function () {
    // outhum credit
    $("#modal-from-dom").show();
    $(".modal").css("background", "#333333ab");
    return false;
  });
*if you get error like ... modal is not a function  so u can do like this also *

$("#open-modal").click(function () {
    // outhum credit
    $("#modal-from-dom").show();
    $(".modal").css("background", "#333333ab");
    return false;
  });
再见回来 2024-12-18 04:38:56

发生这种情况是因为 .list-gorup-item:hover 的 z-index: 1 导致新的堆叠上下文。您可以使用 z-index:1 来解决

.list-group-item, .list-group-item:hover{ z-index: 1 }

This occurs because the z-index: 1 of a .list-gorup-item:hover cause a new stacking context. you can resolved by using z-index:1

.list-group-item, .list-group-item:hover{ z-index: 1 }
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文