简体   繁体   English

Bootstrap modal 使滚动条在关闭后消失

[英]Bootstrap modal makes scrollbar disappear after closing

I read a lot of solutions / hacks on this topic on StackOverflow but none of them seem to work for me.我在 StackOverflow 上阅读了很多关于这个主题的解决方案/技巧,但它们似乎都不适合我。

I am using a modal for logging in in Meteor (eg. using Facebook login service).我正在使用登录 Meteor 的模式(例如,使用 Facebook 登录服务)。 And a callback is triggered when the login is successful.并在登录成功时触发回调。

I put this in my callback to close the modal - $('#modalSignIn').modal('toggle');我把它放在我的回调中以关闭模式 - $('#modalSignIn').modal('toggle');

Everything works fine except that after close, there is no scrollbar on the page.一切正常,除了关闭后,页面上没有滚动条。

One solution I got was from here -我得到的一个解决方案是从这里-

.modal-open {
    overflow: scroll;
}

This works partially because I have the scrollbar even when the modal closes.这部分工作是因为即使模式关闭我也有滚动条。 However, there seems to be about 15px padding on the right (where the previous scrollbar should be.) On repeating this opening and closing, the padding keeps adding up.但是,右侧似乎有大约 15px 的填充(前一个滚动条应该在的位置)。在重复此打开和关闭时,填充不断增加。

EDIT:编辑:

Here is my Nav template -这是我的导航模板 -

<template name="_navMenu">
    {{#if isLoggedIn}}
        <li class="dropdown">
          <a href="#" class="dropdown-toggle" data-toggle="dropdown" role="button" aria-expanded="false">My Profile <span class="caret"></span></a>
          <ul class="dropdown-menu" role="menu">
            <li><a href="#">My Profile</a></li>
            <li><a href="#">Edit Profile</a></li>
            <li class="divider"></li>
            <li><a href="#" id="logout-button">Logout</a></li>
          </ul>
        </li>
    {{else}}
        <li><a href="#" data-toggle="modal" data-target="#modalSignUp">SIGN UP</a></li>
        <li><a href="#" data-toggle="modal" data-target="#modalSignIn">LOG IN</a></li>
        <li><button class="btn btn-primary nav-button visible-xs-inline-block">Text Here</button></li>

        <!-- Modal -->
        <div class="modal fade" style="overflow-y: scroll;" id="modalSignIn" tabindex="-1" role="dialog" aria-labelledby="SignInLabel" aria-hidden="true">
          <div class="modal-dialog">
            <div class="modal-content">
              <div class="row">
                  <div class="col-xs-8 col-xs-offset-2">
                    {{> atFormModal state="signIn"}}
                  </div>
              </div>
            </div>
          </div>
        </div>
    {{/if}}
</template>

I had the same problem.我有同样的问题。 Bootstrap add modal-open class in the body , but does not remove when the modal is closed. Bootstrap 在body添加modal-open类,但在模态关闭时不会删除。 I solved just adding in callback:我解决了添加回调:

$('body').removeClass('modal-open');

you can try this fix你可以试试这个修复

$(document).on('hidden.bs.modal', '.modal', function () {
    $('.modal:visible').length && $(document.body).addClass('modal-open');
});

将此添加到您的正文标签style="overflow:auto;"

 $('body').removeClass("modal-open");

// Bootstrap 在 body 中添加 modal-open 类,但在 modal 关闭时不会移除。

Solve the scroll bar issue by adding the following to your body CSS rules.通过将以下内容添加到您的正文 CSS 规则来解决滚动条问题。

I had this same issue and after reading all the answers above they did not solve my issue so I decide to think and I arrive at the fact that forcing the CSS rule will ensure the desired result.我遇到了同样的问题,在阅读了上面的所有答案后,他们并没有解决我的问题,所以我决定思考并得出一个事实,即强制 CSS 规则将确保获得所需的结果。

body { padding-right: 0!important }

这为我做到了:

$("body").css("overflow", "auto");

Inspect your body and html tags, you'll find one of them gained a style of overflow:hidden检查你的身体和 html 标签,你会发现其中一个获得了溢出的风格:隐藏

just add the style to your css file:只需将样式添加到您的 css 文件中:

body {
overflow: auto !important;
}

or或者

html {
overflow: auto !important;
}

I noticed that it adds also a padding-right style, so you might want to remove that as well我注意到它还添加了一个 padding-right 样式,所以你可能也想删除它

I found the problem and wanted to post it if anyone makes the same mistake - I had the modals in an {{#if }} statement which was causing them to be removed once the user logs in. I just took the modals out of the if and it works fine now -我发现了这个问题,如果有人犯了同样的错误,我想发布它 - 我在{{#if }}语句中使用了模态,这导致它们在用户登录后被删除。我只是将模态从如果它现在工作正常 -

<template name="_navMenu">
    {{#if isLoggedIn}}
        <li class="dropdown">
          <a href="#" class="dropdown-toggle" data-toggle="dropdown" role="button" aria-expanded="false">My Profile <span class="caret"></span></a>
          <ul class="dropdown-menu" role="menu">
            <li><a href="#">My Profile</a></li>
            <li><a href="#">Edit Profile</a></li>
            <li class="divider"></li>
            <li><a href="#" id="logout-button">Logout</a></li>
          </ul>
        </li>
    {{else}}
        <li><a href="#">HOW IT WORKS</a></li>
        <li><a href="#" data-toggle="modal" data-target="#modalSignUp">SIGN UP</a></li>
        <li><a href="#" data-toggle="modal" data-target="#modalSignIn">LOG IN</a></li>
        <li><button class="btn btn-primary nav-button visible-xs-inline-block">Text Here</button></li>
    {{/if}}

    <!-- Modal -->
    <div class="modal fade" style="overflow-y: scroll;" id="modalSignIn" tabindex="-1" role="dialog" aria-labelledby="SignInLabel" aria-hidden="true">
      <div class="modal-dialog">
        <div class="modal-content">
          <div class="row">
              <div class="col-xs-8 col-xs-offset-2">
                {{> atFormModal state="signIn"}}
              </div>
          </div>
        </div>
      </div>
    </div>

</template>

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

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