You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

求助实现底部Modal关闭时的反向滑出动画

实现底部Modal的关闭滑出动画

问题说明

我希望为底部Modal添加关闭动画,实现与打开时相反的效果:打开时它从屏幕底部滑入,关闭时我想让它以同样的方式向下滑出。参考过相关问题但不知道如何适配当前代码,当前使用的代码如下:


修改后的完整代码

HTML

<h2>Bottom Modal</h2>

<!-- Trigger/Open The Modal -->
<button id="myBtn">Open Modal</button>

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

  <!-- Modal content -->
  <div class="modal-content">
    <div class="modal-header">
      <span class="close">&times;</span>
      <h2>Modal Header</h2>
    </div>
    <div class="modal-body">
      <p>Some text in the Modal Body</p>
      <p>Some other text...</p>
    </div>
    <div class="modal-footer">
      <h3>Modal Footer</h3>
    </div>
  </div>

</div>

CSS

/* The Modal (background) */
.modal {
  display: none; /* Hidden by default */
  position: fixed; /* Stay in place */
  z-index: 1; /* Sit on top */
  left: 0;
  top: 0;
  width: 100%; /* Full width */
  height: 100%; /* Full height */
  overflow: auto; /* Enable scroll if needed */
  background-color: rgb(0,0,0); /* Fallback color */
  background-color: rgba(0,0,0,0.4); /* Black w/ opacity */
  animation-name: fadeIn;
  animation-duration: 0.4s;
}

/* Modal Content */
.modal-content {
  position: fixed;
  bottom: 0;
  background-color: #fefefe;
  width: 100%;
  animation-name: slideIn;
  animation-duration: 0.4s;
}

/* 关闭时的Modal内容动画 */
.modal-content.slide-out {
  animation-name: slideOut;
}

/* 关闭时的背景淡出动画 */
.modal.fade-out {
  animation-name: fadeOut;
}

/* The Close Button */
.close {
  color: white;
  float: right;
  font-size: 28px;
  font-weight: bold;
}

.close:hover,
.close:focus {
  color: #000;
  text-decoration: none;
  cursor: pointer;
}

.modal-header {
  padding: 2px 16px;
  background-color: #5cb85c;
  color: white;
}

.modal-body {padding: 2px 16px;}

.modal-footer {
  padding: 2px 16px;
  background-color: #5cb85c;
  color: white;
}

/* Add Animation */
@keyframes slideIn {
  from {bottom: -300px; opacity: 0} 
  to {bottom: 0; opacity: 1}
}

@keyframes slideOut {
  from {bottom: 0; opacity: 1}
  to {bottom: -300px; opacity: 0}
}

@keyframes fadeIn {
  from {opacity: 0} 
  to {opacity: 1}
}

@keyframes fadeOut {
  from {opacity: 1} 
  to {opacity: 0}
}

JavaScript

// Get the modal
var modal = document.getElementById("myModal");
var modalContent = modal.querySelector(".modal-content");

// Get the button that opens the modal
var btn = document.getElementById("myBtn");

// Get the <span> element that closes the modal
var span = document.getElementsByClassName("close")[0];

// 打开Modal
btn.onclick = function() {
  // 移除关闭类,确保下次打开动画正常
  modal.classList.remove("fade-out");
  modalContent.classList.remove("slide-out");
  modal.style.display = "block";
}

// 关闭Modal的通用函数
function closeModal() {
  modal.classList.add("fade-out");
  modalContent.classList.add("slide-out");
  
  // 监听动画结束事件,动画完成后隐藏Modal
  modalContent.addEventListener("animationend", function handleAnimationEnd() {
    modal.style.display = "none";
    // 移除事件监听,避免重复触发
    modalContent.removeEventListener("animationend", handleAnimationEnd);
  });
}

// 点击关闭按钮关闭
span.onclick = closeModal;

// 点击Modal背景关闭
window.onclick = function(event) {
  if (event.target == modal) {
    closeModal();
  }
}

关键修改说明

  • CSS部分:新增slideOut和fadeOut关键帧,分别对应内容滑出和背景淡出的动画;新增.slide-out和.fade-out类,用于触发关闭动画。
  • JS部分:不再直接设置display: none,而是先添加关闭动画类,监听animationend事件,等动画执行完毕后再隐藏Modal;打开时移除关闭类,保证下次打开动画正常触发。

内容的提问来源于stack exchange,提问作者user16770416

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.08.04 22:05:22