Bootstrap3,具有透明覆盖的模态,但也可以通过单击模态外部来关闭它

时间:2018-05-26 13:49:52

标签: javascript css twitter-bootstrap twitter-bootstrap-3

<!DOCTYPE html>
<html>
<head>
  <meta name="viewport" content="width=device-width, initial-scale=1">
  <link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/css/bootstrap.min.css">
  <script src="https://ajax.googleapis.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
  <script src="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/js/bootstrap.min.js"></script>
  <script>
    $(document).ready(function() {
      $("#myBtn2").click(function() {
        $("#myModal2").modal({
          backdrop: false
        });
      });
    });
  </script>
</head>

<body>

  <div class="container">
    <h2>Modal</h2>
    <button type="button" class="btn btn-info" id="myBtn2">Modal</button>
    <!-- Modal -->
    <div class="modal fade" id="myModal2" role="dialog">
      <div class="modal-dialog">

        <div class="modal-content">
          <div class="modal-header">
            <button type="button" class="close" data-dismiss="modal">×</button>
            <h4 class="modal-title">Modal</h4>
          </div>
          <div class="modal-body">
          </div>
        </div>

      </div>
    </div>

  </div>
</body>
</html>

我发现{backdrop: false}可以使模态的叠加层透明。

但是它可以防止在模态外点击关闭模态。

问题: 如何通过close modal启用click outside of the modal,同时,模式的叠加层保持透明?

2 个答案:

答案 0 :(得分:2)

只需将opacity:0添加到背景元素,然后移除backdrop:false

即可

.modal-backdrop {
  opacity:0!important;
}
<!DOCTYPE html>
<html>
<head>
  <meta name="viewport" content="width=device-width, initial-scale=1">
  <link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/css/bootstrap.min.css">
  <script src="https://ajax.googleapis.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
  <script src="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/js/bootstrap.min.js"></script>
  <script>
    $(document).ready(function() {
      $("#myBtn2").click(function() {
        $("#myModal2").modal();
      });
    });
  </script>
</head>

<body>

  <div class="container">
    <h2>Modal</h2>
    <button type="button" class="btn btn-info" id="myBtn2">Modal</button>
    <!-- Modal -->
    <div class="modal fade" id="myModal2" role="dialog">
      <div class="modal-dialog">

        <div class="modal-content">
          <div class="modal-header">
            <button type="button" class="close" data-dismiss="modal">×</button>
            <h4 class="modal-title">Modal</h4>
          </div>
          <div class="modal-body">
          </div>
        </div>

      </div>
    </div>

  </div>
</body>
</html>

答案 1 :(得分:1)

添加以下样式

.modal-backdrop.in {
    filter: alpha(opacity=0); // For IE
    opacity: 0; // For all other browsers
}

此外不需要背景:现在是假的。

但有一点我想提一下,虽然你可以在背景中看到什么,但是在模态关闭之前它不会被点击或访问。