使用css或js关闭时如何向右滑动bootstrap 4模态对话框

Posted

技术标签:

【中文标题】使用css或js关闭时如何向右滑动bootstrap 4模态对话框【英文标题】:How do slide bootstrap 4 modal dialog to right when closing using css or js 【发布时间】:2019-09-08 09:04:26 【问题描述】:

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

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

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>

【问题讨论】:

【参考方案1】:

只需使用自定义类modal-right 并相应地更改modal-dialogtransform 属性。 Bootstrap 默认从top 应用。

.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>

【讨论】:

以上是关于使用css或js关闭时如何向右滑动bootstrap 4模态对话框的主要内容,如果未能解决你的问题,请参考以下文章

如何在从左到右滑动时禁用 ViewController 关闭?

如何关闭 iPad 上的橡胶/弹性滚动效果?

LibGDX - 向上滑动或向右滑动等?

向右滑动以关闭边缘大小

jQuery控制的不同方向的滑动(向左向右滑动等)

向右滑动时如何旋转到3d立方体的另一面