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;
});
});
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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
您当前执行操作的方式可能是导致闪烁的原因。本质上,您告诉浏览器的是:在显示 div 后更新内容。您还告诉 jQuery 在每次单击链接时绑定 onShow 事件。该绑定声明应在 onClick 事件之外进行。
您应该尝试的是在显示模态内容之前更改它,允许浏览器在显示之前适当调整 DOM,从而减少(如果不能消除的话)闪烁。
尝试更多类似这样的事情:
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:
|| 模态解决方案
就我而言,我在悬停时出现了口吃/闪烁/讨人喜欢的模式。解决方案是:不要将模态放在使用 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
发生这种情况是因为 .list-gorup-item:hover 的 z-index: 1 导致新的堆叠上下文。您可以使用 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