如何在保持淡入淡出动画的同时禁用淡出 Bootstrap 5 Modal?

How to disable fading out of Boostrap 5 Modal while keeping the fade in animation active?

我知道如何通过移除父项 div 中的“淡入淡出”class 来移除模态框的淡入淡出动画。但是我需要在去掉淡出动画的同时保留淡入动画。

有什么想法吗?

您可以通过将以下 CSS 放入您的自定义 CSS 文件中使其工作。

.modal.fade {
    transition: opacity 0s linear 0s;
}
.modal.fade.show {
    transition: opacity 0.15s linear 0s;
}

这是工作代码片段,请查看一次。

.modal.fade {
    transition: opacity 0s linear 0s;
}
.modal.fade.show {
    transition: opacity 0.15s linear 0s;
}
<!DOCTYPE html>
<html lang="en">

<head>
  <title>Bootstrap Example</title>
  <meta charset="utf-8">
  <meta name="viewport" content="width=device-width, initial-scale=1">
  <link href="https://cdn.jsdelivr.net/npm/bootstrap@5.1.3/dist/css/bootstrap.min.css" rel="stylesheet">
  <script src="https://cdn.jsdelivr.net/npm/bootstrap@5.1.3/dist/js/bootstrap.bundle.min.js"></script>
</head>

<body>

  <div class="container py-3">
    <button type="button" class="btn btn-primary" data-bs-toggle="modal" data-bs-target="#myModal">
    Open modal
  </button>
  </div>

  <!-- The Modal -->
  <div class="modal fade" id="myModal">
    <div class="modal-dialog">
      <div class="modal-content">

        <!-- Modal Header -->
        <div class="modal-header">
          <h4 class="modal-title">Modal Heading</h4>
          <button type="button" class="btn-close" data-bs-dismiss="modal"></button>
        </div>

        <!-- Modal body -->
        <div class="modal-body">
          Modal body..
        </div>

        <!-- Modal footer -->
        <div class="modal-footer">
          <button type="button" class="btn btn-danger" data-bs-dismiss="modal">Close</button>
        </div>

      </div>
    </div>
  </div>


</body>

</html>