简体   繁体   English

关闭第一个后加载模态(Bootstrap 3)

[英]Loading a modal after closing a first (Bootstrap 3)

I have a modal on the page ( .lockdown ) that has two close buttons. 我在页面上有一个模式( .lockdown ),该模式具有两个关闭按钮。 One of these ( .btn-typoedit ) simply closes the modal, however the other ( .btn-newcontact ) closes the modal and then immediately opens another ( #change-contact ). 其中一个( .btn-typoedit )仅关闭模式,但是另一个( .btn-newcontact )关闭模式,然后立即打开另一个( #change-contact )。 However, when this second modal closes, the whole page "shifts" slightly and I get space for a second scroll bar appear. 但是,当第二个模式关闭时,整个页面会稍微“移动”,并且我获得了第二个滚动条的空间。

This is the Javascript code I am using: 这是我正在使用的Javascript代码:

$(document).ready(function() {
    $('.btn-newcontact').on('click', function() {
        $('#change-contact').modal('show');
    });
});

And a JSFddle to see the glitch in action (open the first modal, then click "NEW"): https://jsfiddle.net/z904fzcm/ 还有一个JSFddle可以看到实际的故障(打开第一个模态,然后单击“ NEW”): https ://jsfiddle.net/z904fzcm/

Looking through the Bootstrap documentation, I can set an event to run on .hidden.bs.modal , but I'm not quite sure what's even causing the glitch. 查看Bootstrap文档,我可以将一个事件设置为在.hidden.bs.modal上运行,但我不确定是什么引起了故障。

Add overflow hidden to the body 添加隐藏到身体的溢出

   $(document).ready(function() {
    $('.btn-newcontact').on('click', function() {

    $('#change-contact').modal('show');
      $('body').css({'overflow':'hidden'});
    });
});

demo: https://jsfiddle.net/pf9juunm/ 演示: https : //jsfiddle.net/pf9juunm/

a safer solution will be to use a class to toggle the state: 一个更安全的解决方案是使用一个类来切换状态:

$(document).ready(function() {
  $('.btn-newcontact').on('click', function(e) {
    $('#change-contact').addClass('active');
    $('.lockdown').modal('hide');
  });


  $('.lockdown').on('hidden.bs.modal', function() {
    if ($('#change-contact.active').length) $('#change-contact').modal('show');
    $('#change-contact').removeClass('active');
  });
});

https://jsfiddle.net/pf9juunm/1/ https://jsfiddle.net/pf9juunm/1/

This might be a glitch from Bootstrap modal. 这可能是Bootstrap模式的故障。

The workarounds can be removing the animation by removing the fade or set a timeout (~500ms) before calling .lockdown 解决方法是在调用.lockdown之前通过消除fade或设置超时时间( .lockdown )来消除动画。

You could also do this body { padding-right: 0 !important } 您也可以做这个body { padding-right: 0 !important }

demo: https://jsfiddle.net/z904fzcm/1/ 演示: https : //jsfiddle.net/z904fzcm/1/

It adds a padding in the body that's why it is shifting slightly. 它在主体中添加了填充物,这就是为什么它会稍微移动的原因。 Try to remove that padding with jQuery. 尝试使用jQuery删除该填充。

You can try removeAttr to remove style attribute from body. 您可以尝试removeAttr从正文中删除样式属性。

$('body').removeAttr('style');

Watching the way the code changed as modals open/close, I managed to deduce the following: 看着代码随着模态打开/关闭而改变的方式,我设法得出以下结论:

  • As a modal opens, the body receives the class .modal-open and receives the CSS padding-right: 17px . 模态打开时, body接收类.modal-open并接收CSS padding-right: 17px
  • When a modal closes, the above class and CSS are removed. 当模式关闭时,将删除上述类和CSS。
  • Because the code to close one modal and open another were within the same .on('click') declaration, the .modal-open class and CSS were removed but never re-added for the second modal. 因为关闭一个模式并打开另一个模式的代码在同一.on('click')声明中,所以.modal-open类和CSS被删除,但从未为第二个模式重新添加。

In order to combat this I watch for a variable I named changeContact , set to 0 on page load, and then I change to 1 when the relevant button is clicked. 为了解决这个问题,我观察了一个名为changeContact的变量,在页面加载时将其设置为0 ,然后在单击相关按钮时将其更改为1 If that variable is 1 when checking Bootstrap's useful hidden.bs.modal event, I then open the second modal. 如果在检查Bootstrap的有用的hidden.bs.modal事件时该变量为1 ,则我打开第二个模式。 This separates the code to two separate functions, meaning the CSS is modified correctly. 这会将代码分为两个独立的函数,这意味着CSS可以正确修改。

$(document).ready(function()
{
    var changeContact = 0;

    $('.btn-lockdown').on('click', function() {
        changeContact = 0;
    });

    $('.btn-newcontact').on('click', function() {
        $('.lockdown').modal('hide');
        changeContact = 1;
    });

    $('.lockdown').on('hidden.bs.modal', function() {
        if (changeContact) {
            $('#change-contact').modal('show');
        }
    });
});

See it in action on JSFiddle 在JSFiddle上看到它的实际效果

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM