Reputation: 1248
I just want to add style to the body when my modal popup is open. After that it needs to remove when modal popup is closed. I just tried with this code. But not working at all.
if( $('.modal').hasClass('in') === true ) {
$('body').style({ 'overflow': "hidden" });
}
Here is the fiddle
https://jsfiddle.net/DTcHh/56667/
Upvotes: 0
Views: 4799
Reputation: 1242
Instead of changing CSS with JavaScript I would recommend you to use HTML classes instead:
var $body = $('body');
$('#myModal').on({
'show.bs.modal': function() { $body.addClass('visible') },
'hide.bs.modal': function() { $body.removeClass('visible') }
});
And CSS:
body {
transition: .5s background;
}
body.visible {
background: red;
}
And here is jFiddle.
Upvotes: 0
Reputation: 189
You can use bootstrap modal events. https://getbootstrap.com/docs/3.3/javascript/#modals-events
$('.modal').on('shown.bs.modal', function (e) {
$('body').style({ 'overflow': "hidden" });
})
Upvotes: 0
Reputation: 29
modify code use for init dialog with:
$(elm).dialog({
...
, open: function() { $("body").css({ "overflow":"hidden" }); }
, close: function() { $("body").css({ "overflow":"" }); }
});
Upvotes: 1
Reputation: 15846
You can try the events shown.bs.modal
and hidden.bs.modal
. Also you are using the wrong function style
you need to change that to 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">×</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">×</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>
Upvotes: 0