Bootstrap模式滚动背景内容

时间:2015-10-09 02:52:05

标签: javascript html ruby-on-rails twitter-bootstrap

当我打开模态时,背景页面会自动滚动到顶部。在我的CSS中,如果我删除html{ overflow-y: scroll };我修复了这种行为,但现在右边的滚动条消失了,我的导航栏向右移动了滚动条占用的相同空间。

我希望能够保留我的溢出设置,并使其背景内容不会向上滚动。

example_controller.rb

def onsale
    @item= Products.first
    respond_to do |format|
      format.js
    end
  end

_header.html.erb

<div class="collapse navbar-collapse" id="navbar-collapse">
        <% if user_signed_in? %>
          <ul class="nav navbar-nav navbar-left">
            <li><%= link_to "Sale Items", onsale_path,  :remote => true %></li>
          </ul>
        <% end %>
</div>

application.html.erb 我把它放在体内  <div id="sale-modal"></div>

onsale.js.erb

$("#onsale-modal").html('<%= escape_javascript(render 'onsale') %>');

_onsale.html.erb

<div class="modal" id="myModal" tabindex="-1" role="dialog" aria-labelledby="myModalLabel">
  <div class="modal-dialog" role="document">
    <div class="modal-content">
      <div class="modal-header">
        <button type="button" class="close" data-dismiss="modal" aria-label="Close"><span aria-hidden="true">&times;</span></button>
        <h4 class="modal-title" id="myModalLabel">Modal title</h4>
      </div>
      <div class="modal-body">
        <%= image_tag @item.picture.large.url %>
      </div>
      <div class="modal-footer">
        <button type="button" class="btn btn-default" data-dismiss="modal">Close</button>
        <button type="button" class="btn btn-primary">Save changes</button>
      </div>
    </div>
  </div>
</div>
<script>$('#myModal').modal('show')</script>

的application.js

//= require jquery
//= require jquery.turbolinks
//= require jquery_ujs
//= require bootstrap-sprockets
//= require bootstrap-select.min
//= require turbolinks
//= require fancybox
//= require_tree .

$(document).ready(function() {
  $('.selectpicker').selectpicker();
  $('.fancybox').fancybox({
    openEffect: 'elastic',
    closeEffect: 'elastic',
    prevEffect: 'fade',
    nextEffect: 'fade',
    padding: 10
  });

  if ($('#infinite-scrolling').length) {
    $(window).scroll(function() {
      var url = $('.pagination .next_page').attr('href');
      if (url && $(window).scrollTop() > $(document).height() - $(window).height() - 60) {
        // TODO - Removing this line was causing duplicates to load so it's worth investigating the cause
        $('.pagination').html('Please Wait...');
        return $.getScript(url);
      }
    });
    return $(window).scroll();
  }
  $('#myModal').modal('show')
  //Hide Scroll when modal about to show
    $('#myModal').on('show.bs.modal', function () {
        $('html').css({
            'overflow': 'hidden'
        });
    });
    //add Scroll back when modal is hidden
    $('#myModal').on('hidden.bs.modal', function () {
        $('html').css({
            'overflow-y': 'scroll'
        });
    });

});

如果我从partial中删除脚本<script>$('#myModal').modal('show')</script>并将其放在application.js中,就像上面那样,模式根本不显示。如果我将行$('#myModal').modal('show')添加到onsale.js.erb,则模式将显示正常。

1 个答案:

答案 0 :(得分:0)

this问题的第一个答案对我有用。我刚刚添加了

body.modal-open {
  overflow: visible;
}

到我的css。