简体   繁体   中英

Why when I set data-backdrop = "false", it can't close modal when I click outside of modal?

My code like this :

<div class="container">
  <h1 class="display-4 text-center mb-4">Bootstrap Modals</h1>

  <div class="row mb-4">
    <div class="col text-center">
      <h3>The Basic Modal</h3>
      <a href="#" class="btn btn-lg btn-success" data-toggle="modal" data-target="#basicModal" data-backdrop="false" data-keyboard="true" >Click to open Modal</a>
    </div>
  </div>

</div>

<br/><br/><br/><br/>
<div class="form-group">
    <label for="exampleFormControlTextarea1">Input data</label>
    <textarea class="form-control" id="exampleFormControlTextarea1" rows="3"> </textarea>
</div>

<!-- basic modal -->
<div class="modal fade" id="basicModal" tabindex="-1" role="dialog" aria-labelledby="basicModal" aria-hidden="true">
  <div class="modal-dialog">
    <div class="modal-content">
      <div class="modal-header">
        <h4 class="modal-title" id="myModalLabel">Basic Modal</h4>
        <button type="button" class="close" data-dismiss="modal" aria-label="Close">
          <span aria-hidden="true">&times;</span>
        </button>
      </div>
      <div class="modal-body">
        <h3>Modal Body</h3>
      </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>

Demo and full code like this : https://codepen.io/trendingnews/pen/BayQLrm?editors=1010

I set data-backdrop="false" to remove backdrop

but it makes me unable to click on something. I have to click the close button on the modal to close on the modal.

how do I make it when I click on any element outside of modal, the modal automatically closed?

for example, I click textarea or other elements, the modal is automatically closed

As an extension to my comment, adding as an answer for anyone else that comes across this.

Problem:

You want to use a bootstrap modal, with no backdrop, and have the functionality that clicking away from the modal closes it.

Challenge:

The bootstrap modal backdrop has a listener on it so when clicked, it closes the modal. However, if there's no backdrop, then this cannot happen.

Solution:

Use a backdrop as normal but set it's color to transparent. Set the css background-color property on the backdrop eg

.modal-backdrop { background-color: transparent; }

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM