简体   繁体   English

引导模式删除滚动条

[英]bootstrap modal removes scroll bar

When I trigger a modal view in my page it triggers the scroll bar to disappear.当我在页面中触发模态视图时,它会触发滚动条消失。 It's an annoying effect because the background page starts moving when the modal moves in / disappears.这是一个令人讨厌的效果,因为当模态移入/消失时背景页面开始移动。 Is there a cure for that effect?有没有治愈这种效果的方法?

This is a feature, class modal-open gets added to the HTML body when you show the modal, and removed when you hide it.这是一个功能,当您显示模态时,类modal-open会被添加到 HTML body ,并在您隐藏它时被移除。

This makes the scrollbar disappear since the bootstrap css says这使得滚动条消失,因为引导 css 说

.modal-open {
    overflow: hidden;
}

You can override this by specifying您可以通过指定覆盖它

.modal-open {
    overflow: scroll;
}

in your own css.你自己的css中。

I think that inherit is better than scroll because when you open modal, it will always open with scroll, but when you don't have any scrolls you will get the same problem.我认为继承滚动更好,因为当你打开模态时,它总是会随着滚动而打开,但是当你没有任何滚动时,你会遇到同样的问题。 So I just do this:所以我只是这样做:

.modal-open {
  overflow: inherit;
}

Its better to use overflow-y:scroll and remove padding from body, bootstrap modal added padding to page body.最好使用 overflow-y:scroll 并从正文中删除填充,引导模式向页面正文添加填充。

.modal-open {
  overflow:hidden;
  overflow-y:scroll;
  padding-right:0 !important;
}

IE browser Compatible: IE browser doing same thing by default. IE浏览器兼容: IE浏览器默认做同样的事情。

I used我用了

.modal-open {
  overflow-y: scroll;
}

In this case you avoids to display the horizontal scroll bar在这种情况下,您避免显示水平滚动条

Thank God I came accross this post!感谢上帝,我看到了这篇文章! I was pulling my hair out trying to figure how to get my scrollbars back when closing the window using my own close link.我正在拉我的头发,试图弄清楚如何在使用我自己的关闭链接关闭窗口时恢复我的滚动条。 I tried the suggestions for CSS but it just wasnt working properly.我尝试了 CSS 的建议,但它无法正常工作。 After reading看完后

class modal-open gets added to the HTML body when you show the modal, and removed when you hide it.当您显示模态时,类 modal-open 会被添加到 HTML 正文中,并在您隐藏它时被移除。 -- @flup -- @flup

I came up with a solution using jquery, so incase anyone else has the same issue and the above suggestions dont work --我想出了一个使用 jquery 的解决方案,所以如果其他人有同样的问题并且上述建议不起作用 -

<script>
            jQuery(document).ready(function () {
jQuery('.closeit').click(function () {
 jQuery('body').removeClass('modal-open');
                });
            });
        </script>

I was just playing with this 'feature' of Bootstrap modals.我只是在玩 Bootstrap 模态的这个“功能”。 Seems the .modal class has overflow-y:auto;似乎.modal类有overflow-y:auto; So the modal wrapper gets his own scrollbar when the modal itself becomes to high.因此,当模态本身变高时,模态包装器会获得自己的滚动条。

If you always want a scrollbar (designers often do) First set the body如果你总是想要一个滚动条(设计师经常做的)首先设置 body

body {
    overflow-y:scroll;
}

Then handle .modal-open然后处理.modal-open

.modal-open .modal {
    overflow-y:scroll; /* Force a navbar on .modal */
}

.modal-open .topnavbar-wrapper {
    padding-right:17px; /* Noticed that right aligned navbar content got overlapped by the .modal scrollbar */
}

Leave scrollbar disabling on body untouched in this case在这种情况下,保持身体上的滚动条禁用不变

All other answers on this page kept making my content jump.此页面上的所有其他答案不断使我的内容跳跃。

Heads up!当心!

Allthough this solution worked for me all the time, yesterday I had a problem using this fix when the modal is draggable and to large to fit the screen (vertically).尽管此解决方案一直对我有用,但昨天我在使用此修复程序时遇到了问题,当模态可拖动并且大到适合屏幕(垂直)时。 It might have something to do with position:sticky elements I added?它可能与position:sticky我添加的position:sticky元素?

The only answer that worked for me is the following:对我有用的唯一答案如下:

.modal-open {
  padding-right: 0 !important;
}
html {
  overflow-y: scroll !important;
}

Better if you will create your own css file to customize a certain part of your bootsrap.如果您将创建自己的 css 文件来自定义引导程序的某个部分,那就更好了。

Do not Alter the css file of bootstrap because it will change everything in your bootstrap once you use it in other parts of your page.不要修改 bootstrap 的 css 文件,因为一旦你在页面的其他部分使用它,它会改变你的 bootstrap 中的所有内容。

If your page is somewhat long, it will automatically provide a scroll bar.如果你的页面有点长,它会自动提供一个滚动条。 And when the modal opened, it will hide the scroll bar because that's what bootstrap do as a default.当模态打开时,它将隐藏滚动条,因为这是引导程序默认执行的操作。

To avoid hiding it when modal is opened, just override it by putting the css code (below) on your own css file.为避免在模式打开时隐藏它,只需通过将 css 代码(如下)放在您自己的 css 文件中来覆盖它。

.modal-open {
    overflow: scroll;
}

just a vice versa...反之亦然...

.modal-open {
    overflow: hidden;
}

Additionally, if modal popup needs to scroll with content inside and the parent needs to remain still, add the following to your Custom.css (overriding css)此外,如果模态弹出窗口需要随内容滚动而父级需要保持静止,请将以下内容添加到您的 Custom.css(覆盖 css)

.modal-body {
    max-height: calc(100vh - 210px);
    overflow-y: auto;
}
.modal-open {
    overflow: hidden;
    overflow-y: scroll;
    padding-right: 0 !important;
}

the bootstrap modal adds a padding once it opens so you can try this code in your own css引导模式打开后会添加一个填充,因此您可以在自己的 css 中尝试此代码

.modal-open {
    padding: 0 !important;
}

This is probably caused that 1st modal (when closed) remove modal-open class from body element and second one wont add it back when trigger modal-open.这可能是由于第一个模态(关闭时)从 body 元素中删除了 modal-open 类,而第二个在触发 modal-open 时不会将其添加回来。

In this case I would use event to check if modal has been closed/hidden fully and open another在这种情况下,我将使用事件来检查模态是否已完全关闭/隐藏并打开另一个

let modal1 = $('.modal1);
let modal2 = $('.modal2);
$modal1.on('hidden.bs.modal', function (e) {
   $modal2.modal('show');
});

This will wait till 1st modal is closed to make sure modal-open is removed from body and re-added it when open.这将等到第一个模态关闭以确保 modal-open 从主体中移除并在打开时重新添加它。

I just had this problem myself, and finding this question quickly helped me understand what was causing the behaviour.我自己也遇到了这个问题,发现这个问题很快帮助我了解了导致这种行为的原因。 So thanks.那谢谢啦。

However, I find these CSS workarounds a bit inelegant.但是,我发现这些 CSS 变通方法有点不雅观。 Unless, that is, you specifically want to keep the scrollbars (although I can't think of a reason for that).除非,也就是说,您特别想保留滚动条(尽管我想不出原因)。

Essentially Bootstrap is applying padding-right to certain elements to compensate for removing the scrollbar.本质上,Bootstrap 正在对某些元素应用 padding-right 以补偿移除滚动条。

Therefore, all you need to do is to put an extra wrapper with zero padding-right around your problematic element (and move the class that's being targeted up to it).因此,您需要做的就是在有问题的元素周围放置一个带有零填充的额外包装器(并将目标类移动到它上面)。

ie change from this...即从这个改变...

<div class="fixed-top p-1 bg-dark">
    ...this content will move as padding will be modified...
</div>

... to this... ……到这……

<div class="fixed-top p-0 bg-dark">
    <div class="p-1">
        ...this content won't move...
    </div>
</div>

In my case, bootstrap adds margin-left: -17px to my sidebar element by opening the modal.就我而言,引导程序通过打开模态将margin-left: -17px到我的侧边栏元素。 My solution on this, is to add m-0 class to sidebar.我对此的解决方案是将m-0类添加到侧边栏。

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

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