当模态打开时,将样式添加到正文

时间:2018-03-23 07:45:42

标签: javascript jquery twitter-bootstrap

我只想在我的模态弹出窗口打开时为主体添加样式。之后,当模态弹出窗口关闭时需要删除它。我刚试过这段代码。但根本不工作。

if( $('.modal').hasClass('in') === true ) {
  $('body').style({ 'overflow': "hidden" });
  } 

这是小提琴

example in the doc

4 个答案:

答案 0 :(得分:1)

使用以下命令修改init对话框的代码:

$(elm).dialog({
   ...
   , open: function() { $("body").css({ "overflow":"hidden" }); }
   , close: function() { $("body").css({ "overflow":"" }); }
});

答案 1 :(得分:0)

您可以尝试事件shown.bs.modalhidden.bs.modal。此外,您使用了错误的函数style,您需要将其更改为css

$('#myModal').on('shown.bs.modal', function(){
  $('body').css({ 'overflow': "hidden" });
}).on('hidden.bs.modal', function(){
  $('body').css({ 'overflow': "auto" });
});

$('#myModal').on('shown.bs.modal', function(){
  $('body').css({ 'overflow': "hidden" });
}).on('hidden.bs.modal', function(){
  $('body').css({ 'overflow': "auto" });
})
@import url('//netdna.bootstrapcdn.com/bootstrap/3.0.0/css/bootstrap-theme.min.css');

body {
    margin: 10px;
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<link href="https://netdna.bootstrapcdn.com/bootstrap/3.0.0/css/bootstrap.min.css" rel="stylesheet"/>
<script src="https://netdna.bootstrapcdn.com/bootstrap/3.0.0/js/bootstrap.min.js"></script>
<!-- Button trigger modal -->
<button type="button" class="btn btn-primary btn-lg" data-toggle="modal" data-target="#myModal">
  Open modal
</button>

<!-- Modal -->
<div class="modal fade" 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">
        ...
      </div>
      <div class="modal-footer">
        <button type="button" class="btn btn-primary btn-lg" data-toggle="modal" data-target="#myModal1" data-dismiss="modal">
  Open modal
</button>
      </div>
    </div>
  </div>
</div>

<!-- Modal1 -->
<div class="modal fade" id="myModal1" 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">
        ...
      </div>
      <div class="modal-footer">
        <button type="button" class="btn btn-primary" data-dismiss="modal">Close</button>
      </div>
    </div>
  </div>
</div>

DEMO

答案 2 :(得分:0)

您可以使用bootstrap模态事件。 https://getbootstrap.com/docs/3.3/javascript/#modals-events

$('.modal').on('shown.bs.modal', function (e) {
  $('body').style({ 'overflow': "hidden" });
})

答案 3 :(得分:0)

我建议您不要使用JavaScript更改CSS,而是建议您使用HTML类:

var $body = $('body');

$('#myModal').on({
    'show.bs.modal': function() { $body.addClass('visible') },
    'hide.bs.modal': function() { $body.removeClass('visible') }
});

和CSS:

body {
    transition: .5s background;
}

body.visible {
    background: red;
}

这是jFiddle