RulerNature
RulerNature

Reputation: 753

Display bootstrap modal only for mobile and not for desktop

I have a nasty problem regarding bootstrap modals on mobile vs desktop.

so the question will be short:

How can I disable bootstrap modal on desktop and enable only for mobile/tablets?

Here is a small js for tests:

Upvotes: 2

Views: 12279

Answers (3)

acmsohail
acmsohail

Reputation: 1023

U can use .hidden-lg class in the html. Check the below code it will hide the div in large devices. reference - Bootstrap Responsive Utilities

jsfiddle

<div class="panel panel-warning hidden-lg" id="mymodal" data-toggle="modal" data-target="#myModal">
  <div class="panel-body">
  <h3>click on panel to open the modal</h3>
  </div>
</div>
<!-- Modal -->
<div class="modal fade hidden-lg" 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-default" data-dismiss="modal">Close</button>
        <button type="button" class="btn btn-primary">Save changes</button>
      </div>
    </div>
  </div>
</div>

Upvotes: 1

Dekel
Dekel

Reputation: 62566

There are several ways to check if you are on mobile. You can read more here.

In this example I just used the width of the screen and based on it decided if to show the modal or not:

$('#myModal').on('show.bs.modal', function (e) {
  if (window.innerWidth < 800) {
    return e.preventDefault();
  }
})

Here is the update to your jsfiddle:
http://jsfiddle.net/1aeur58f/28/

Upvotes: 4

Mukul Kant
Mukul Kant

Reputation: 7122

You can use responsive utilities classes. In your case you should use .hidden-md or .hidden-md class in modal box

<div class="modal fade hidden-lg hidden-md" 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>

Upvotes: 3

Related Questions