Bootstrap Modals keep adding padding-right to body after closed

后端 未结 30 1830
南方客
南方客 2020-12-12 12:36

I am using bootstrap and Parse framework to build a small webapp. But those Bootstrap modals keep adding padding-right to body after closed. How to solve this?

I tri

相关标签:
30条回答
  • 2020-12-12 12:58

    My solution does not require any additional CSS.

    As pointed by @Orland, one event is still happening when the other starts. My solution is about starting the second event (showing the adminPanel modal) only when the first one is finished (hiding the loginModal modal).

    You can accomplish that by attaching a listener to the hidden.bs.modal event of your loginModal modal like below:

    $('#loginModal').on('hidden.bs.modal', function (event) {
        $('#adminPanel').modal('show');
    }
    

    And, when necessary, just hide the loginModal modal.

    $('#loginModal').modal('hide');
    

    Of couse you can implement your own logic inside the listener in order to decide to show or not the adminPanel modal.

    You can get more info about Bootstrap Modal Events here.

    Good luck.

    0 讨论(0)
  • 2020-12-12 13:00

    I hope there is still someone who needs this answer. There is a function called resetScrollbar() in bootstrap.js, for some stupid reason the developers decided to add the scrollbar dimensions to the body's padding right. so technically if you just set right-padding to an empty string it will fix the problem

    0 讨论(0)
  • 2020-12-12 13:01

    This is what worked for me:

    body.modal-open {
        overflow: auto !important;
    }
    
    // Bootstrap uses JS to set the element style so you can
    // override those styles like this
    body.modal-open[style] {
        padding-right: 0px !important;
    }
    
    0 讨论(0)
  • 2020-12-12 13:02

    (Bootstrap v3.3.7) From my browser inspector i saw that this is directly set in the element style property, witch overrides class properties.

    I tryed to 'reset' the paddig-right value when the modal is showing with :

    $("#myModal").on('shown.bs.modal', function(){
        $(this).css({paddingRight:""});
    });
    

    But it comes back as soon as the window is resized :( (probablly from the pluggin script).

    This solution worked for me :

    var modal_needfix=true;
    $("#myModal").on('shown.bs.modal', function(){
        var modalscope=$(this);
        modalscope.css({paddingRight:""});
        if(modal_needfix){
            window.addEventListener("resize",function(){
                requestAnimationFrame(function(){// be sure to act after the pluggin script
                    modalscope.css({paddingRight:""});
                });
            });
            modal_needfix=false;
        }
    });
    
    0 讨论(0)
  • 2020-12-12 13:05

    With Bootstrap 4 this worked for me:

    $(selector).on('hide.bs.modal', function (e) {
        $('body').css('padding-right','0');
    });
    
    0 讨论(0)
  • 2020-12-12 13:05

    This can solve the problem

    .shop-modal.modal.fade.in {
        padding-right: 0px !important;
    }
    
    0 讨论(0)
提交回复
热议问题