如何使用css或js在关闭时将Bootstrap 4模式对话框向右滑动

我有一个引导模式,当打开时,我应用css动画从右侧滑入,它工作得很好,但我希望在单击自定义关闭按钮时将其滑回右侧,而不是在没有动画的情况下只是隐藏。

请任何人知道我如何做到这一点,我已经在网上搜索过,但我看到的只是如何在打开时应用动画。

数据-lang="js"数据-隐藏="假"数据-控制台="真"数据-巴贝尔="假">
function customOpen(element){
  $(element).modal({
    backdrop: false,
    keyboard: false
  });
}
function customClose(element){
  $(element).modal('hide');
}
$(document).on("click", "#open", function() {
  var elem = $(this).attr("data-element");
  customOpen(elem);
});
$(document).on("click", "#close", function() {
  var elem = $(this).attr("data-element");
  customClose(elem);
});
.modal.fade {
    transition: opacity .0s linear !important;
}
.modal.fade:not(.show).left .modal-dialog {
	-webkit-transform: translate3d(-125%, 0, 0);
	-ms-transform: translate3d(-125%, 0, 0);
	-o-transform: translate3d(-125%, 0, 0);
	transform: translate3d(-125%, 0, 0);
}
.modal.fade:not(.show).right .modal-dialog {
	-webkit-transform: translate3d(125%, 0, 0);
	-ms-transform: translate3d(125%, 0, 0);
	-o-transform: translate3d(125%, 0, 0);
	transform: translate3d(125%, 0, 0);
}
<link rel="stylesheet" href="https://stackpath.bootstrapcdn.com/bootstrap/4.3.1/css/bootstrap.min.css" integrity="sha384-ggOyR0iXCbMQv3Xipma34MD+dH/1fQ784/j6cY/iJTQUOhcWr7x9JvoRxT2MZw1T" crossorigin="anonymous">

<script src="https://code.jquery.com/jquery-3.3.1.slim.min.js" integrity="sha384-q8i/X+965DzO0rT7abK41JStQIAqVgRVzpbzo5smXKp4YfRvH+8abtTE1Pi6jizo" crossorigin="anonymous"></script>
<script src="https://stackpath.bootstrapcdn.com/bootstrap/4.3.1/js/bootstrap.min.js" integrity="sha384-JjSmVgyd0p3pXB1rRibZUAYoIIy6OrQ6VrjIEaFf/nJGzIxFDsf4x0xIM+B07jRM" crossorigin="anonymous"></script>

<div class="modal modal-full fade right" id="newPage">
  <div class="modal-dialog">
    <div class="modal-content">
      <div class="modal-header">
        <h4 class="modal-title">Modal title</h4>
      </div>
      <div class="modal-body">
        <p>One fine body&hellip;</p>
      </div>
      <div class="modal-footer">
        <button type="button" class="btn btn-default" id="close" data-element='#newPage'>Close</button>
      </div>
    </div>
  </div>
</div>

<a class='btn btn-primary btn' id="open" data-element='#newPage'>Open Right</a>


解决方案

只需使用一个自定义类modal-right,并相应地更改modal-dialogtransform属性。默认情况下,引导从top应用。

数据-lang="js"数据-隐藏="假"数据-控制台="真"数据-巴贝尔="假">
.modal.fade.modal-right .modal-dialog {
  transform: translate(125%, 0px);
}

.modal.show.modal-right .modal-dialog {
  transform: none;
}
<link rel="stylesheet" href="https://stackpath.bootstrapcdn.com/bootstrap/4.3.1/css/bootstrap.min.css" integrity="sha384-ggOyR0iXCbMQv3Xipma34MD+dH/1fQ784/j6cY/iJTQUOhcWr7x9JvoRxT2MZw1T" crossorigin="anonymous">

<script src="https://code.jquery.com/jquery-3.3.1.slim.min.js" integrity="sha384-q8i/X+965DzO0rT7abK41JStQIAqVgRVzpbzo5smXKp4YfRvH+8abtTE1Pi6jizo" crossorigin="anonymous"></script>
<script src="https://stackpath.bootstrapcdn.com/bootstrap/4.3.1/js/bootstrap.min.js" integrity="sha384-JjSmVgyd0p3pXB1rRibZUAYoIIy6OrQ6VrjIEaFf/nJGzIxFDsf4x0xIM+B07jRM" crossorigin="anonymous"></script>

<div class="modal fade modal-right" id="newPage" tabindex="-1" role="dialog" aria-labelledby="newPage" aria-hidden="true">
  <div class="modal-dialog">
    <div class="modal-content">
      <div class="modal-header">
        <h4 class="modal-title">Modal title</h4>
      </div>
      <div class="modal-body">
        <p>One fine body&hellip;</p>
      </div>
      <div class="modal-footer">
        <button type="button" class="close" data-dismiss="modal" aria-label="Close">Close</button>
      </div>
    </div>
  </div>
</div>

<a class="btn btn-primary" data-toggle="modal" data-target="#newPage">Open Right</a>

相关文章