css - 加载 twitter bootstrap 模式对话框时,如何防止正文滚动条和移动?

标签 css twitter-bootstrap twitter-bootstrap-3 bootstrap-modal

当我打开 Twitter Bootstrap 模式对话框时,背景会导致滚动条并移动内容。

为了避免滚动条,我使用了这个 css:

.modal {
    overflow: hidden;
}

但我无法避免转变。

问候, 马尔科

我正在使用 Bootstrap 版本 3

最佳答案

马可

我刚刚遇到了同样的问题。似乎 Bootstrap 3.0.0 添加了一个类到 <body> , modal-open ,当模态首次显示时。此类添加 margin-right: 15px到正文以说明滚动条,该滚动条位于较长的页面上。这很好,除了当滚动条不在主体上时页面较短。在没有滚动条的情况下,margin-right 会导致 body 在模式打开时向左移动。

我能够通过添加一些简短的 Javascript 和一点 CSS 来解决这个问题:

CSS:

/* override bootstrap 3 class to remove scrollbar from modal backdrop
   when not necessary */
.modal {
  overflow-y: auto;
}
/* custom class to override .modal-open */
.modal-noscrollbar {
    margin-right: 0 !important;
}

JS:

(function($) {

$(document)
    .on( 'hidden.bs.modal', '.modal', function() {
        $(document.body).removeClass( 'modal-noscrollbar' );
    })
    .on( 'show.bs.modal', '.modal', function() {
        // Bootstrap adds margin-right: 15px to the body to account for a
        // scrollbar, but this causes a "shift" when the document isn't tall
        // enough to need a scrollbar; therefore, we disable the margin-right
        // when it isn't needed.
        if ( $(window).height() >= $(document).height() ) {
            $(document.body).addClass( 'modal-noscrollbar' );
        }
    });

})(window.jQuery);

这些组合允许 margin-right 滚动条修复适用于长页面,但对于较短的页面禁用(当文档高度 <= 窗口高度时)。希望对您有所帮助!


Bootstrap 3.0.1+(测试到 3.1.1)是另一回事。尝试以下操作:

CSS:

/* override bootstrap 3 class to remove scrollbar from modal backdrop
   when not necessary */
.modal {
  overflow-y: auto;
}
/* custom class to add space for scrollbar */
.modal-scrollbar {
    margin-right: 15px;
}

JS:

(function($) {

$(document)
    .on( 'hidden.bs.modal', '.modal', function() {
        $(document.body).removeClass( 'modal-scrollbar' );
    })
    .on( 'show.bs.modal', '.modal', function() {
        // Bootstrap's .modal-open class hides any scrollbar on the body,
        // so if there IS a scrollbar on the body at modal open time, then
        // add a right margin to take its place.
        if ( $(window).height() < $(document).height() ) {
            $(document.body).addClass( 'modal-scrollbar' );
        }
    });

})(window.jQuery);

编辑:

鉴于 Mac 消除了占用窗口渲染宽度的滚动条,如果您不介意某些功能检测,这里有一个更便携的解决方案 (3.0.1+)。引用:http://davidwalsh.name/detect-scrollbar-width

CSS:

.scrollbar-measure {
    height: 100px;
    overflow: scroll;
    position: absolute;
    top: -9999px;
}

JS:

window.jQuery(function() {
  // detect browser scroll bar width
  var scrollDiv = $('<div class="scrollbar-measure"></div>')
        .appendTo(document.body)[0],
      scrollBarWidth = scrollDiv.offsetWidth - scrollDiv.clientWidth;

  $(document)
    .on('hidden.bs.modal', '.modal', function(evt) {
      // use margin-right 0 for IE8
      $(document.body).css('margin-right', '');
    })
    .on('show.bs.modal', '.modal', function() {
      // When modal is shown, scrollbar on body disappears.  In order not
      // to experience a "shifting" effect, replace the scrollbar width
      // with a right-margin on the body.
      if ($(window).height() < $(document).height()) {
        $(document.body).css('margin-right', scrollBarWidth + 'px');
      }
    });
});

关于css - 加载 twitter bootstrap 模式对话框时,如何防止正文滚动条和移动?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/19288546/

相关文章:

javascript - 自动宽度属性不适用于数据表

javascript - 使用 Javascript 在导航栏中显示当前选项卡

node.js - 如何在多页 Electron 应用程序中停止 'white' 页面刷新

css - Bootstrap 元素溢出其容器

html - 带有更大箭头的选项下拉按钮

javascript - 链接以显示特定的 div,从侧面动画进入(在父级内)

javascript - 如何在不使用长度和宽度的像素的情况下制作完美的圆

javascript - angularjs ui bootstrap datepicker意外添加一天

css - 如何根据 768px 以下的屏幕尺寸制作 col-xs-12 和 col-xs-6

twitter-bootstrap - 强制列中的内容在多个断点上具有相同的高度