问题
How do I hide Bootstrap modal in mobile. I tried to use modifier classes such as hidden-xs hidden-sm but fail. The modal would go to the bottom page, leaving the top page inaccessible.
Here's my code:
<div class="modal fade hidden-xs hidden-sm" id="myModal" tabindex="-1" role="dialog" aria-labelledby="myModalLabel" aria-hidden="true" style="margin-top:1em;">
<div class="modal-dialog">
<div class="modal-content">
<div class="modal-body">
<img src="banner.jpg" height="388" width="558" alt="" class="img-rounded">
</div>
<div class="modal-footer" style="border:0">
<button type="button" class="btn btn-default" data-dismiss="modal">Close</button>
</div>
</div>
</div>
</div>
Since hidden-xs and hidden-sm fail, I tried to change my css:
@media only screen and (max-device-width: 480px){
#myModal{display: none;}
}
or
@media only screen and (max-device-width: 480px){
.modal{display: none;}
}
but, they're also fail.
回答1:
Your media query is overwritten by jQuery. Try it with jQuery.
something like this:
$('#myModal').on('shown.bs.modal', function () {
var width = $(window).width();
if(width < 480){
$(this).hide();
}
});
回答2:
as you said that your css is not working i would recommend using jquery method just add this before the modal html code <div class='check-width'>
, and use the below script.
if($('.check-width').width() == 500){
$('#myModal').modal('hide');
}
else{
}
回答3:
Danko's answer above still left the darkened overlay on my page (even though no modal appeared) but this is what worked for me:
$('#myModal').on('shown.bs.modal', function () {
var width = $(window).width();
if(width < 768){
$('#myModal').modal('hide')
}
});
来源:https://stackoverflow.com/questions/29584172/hide-bootstrap-modal-in-mobile