弹窗动画代码集合

404 阅读2分钟

使用 transitionanimation 来创建弹窗动画。以下是几种常见的 弹窗动画 效果。


🌟 1. 缩放淡入(Scale + Fade In)

效果:弹窗从小变大,同时透明度从 0 变 1,营造渐入的感觉。

@keyframes fadeInScale {
  from {
    opacity: 0;
    transform: scale(0.8);
  }
  to {
    opacity: 1;
    transform: scale(1);
  }
}

.modal {
  width: 300px;
  padding: 20px;
  background: white;
  border-radius: 10px;
  box-shadow: 0 10px 30px rgba(0, 0, 0, 0.3);
  animation: fadeInScale 0.3s ease-out;
}

2. 从顶部滑入(Slide Down)

效果:弹窗从上方滑入,同时淡入。

@keyframes slideDown {
  from {
    opacity: 0;
    transform: translateY(-50px);
  }
  to {
    opacity: 1;
    transform: translateY(0);
  }
}

.modal {
  width: 300px;
  padding: 20px;
  background: white;
  border-radius: 10px;
  box-shadow: 0 10px 30px rgba(0, 0, 0, 0.3);
  animation: slideDown 0.4s ease-out;
}

🎭 3. 从底部弹出(Bounce Up)

效果:弹窗从底部弹出,带有轻微的弹跳感。

@keyframes bounceUp {
  0% {
    transform: translateY(100px);
    opacity: 0;
  }
  60% {
    transform: translateY(-10px);
    opacity: 1;
  }
  80% {
    transform: translateY(5px);
  }
  100% {
    transform: translateY(0);
  }
}

.modal {
  width: 300px;
  padding: 20px;
  background: white;
  border-radius: 10px;
  box-shadow: 0 10px 30px rgba(0, 0, 0, 0.3);
  animation: bounceUp 0.5s ease-out;
}

🔄 4. 旋转进入(Rotate In)

效果:弹窗从小到大,同时带有旋转效果。

@keyframes rotateIn {
  from {
    opacity: 0;
    transform: rotate(-20deg) scale(0.5);
  }
  to {
    opacity: 1;
    transform: rotate(0deg) scale(1);
  }
}

.modal {
  width: 300px;
  padding: 20px;
  background: white;
  border-radius: 10px;
  box-shadow: 0 10px 30px rgba(0, 0, 0, 0.3);
  animation: rotateIn 0.4s ease-out;
}

🎩 5. 模糊淡入(Blur In)

效果:弹窗从模糊状态逐渐清晰,同时淡入。

@keyframes blurIn {
  from {
    opacity: 0;
    filter: blur(10px);
  }
  to {
    opacity: 1;
    filter: blur(0);
  }
}

.modal {
  width: 300px;
  padding: 20px;
  background: white;
  border-radius: 10px;
  box-shadow: 0 10px 30px rgba(0, 0, 0, 0.3);
  animation: blurIn 0.4s ease-out;
}

🎬 如何使用这些动画?

你可以在 弹窗的 modal 上应用这些动画,并在 JavaScript 中控制弹窗的显示和隐藏。例如:

<div class="modal">Hello, this is a modal!</div>

如果你想让弹窗 关闭时有动画,可以使用 CSS transition 让其淡出,而不是直接 display: none

.modal {
  opacity: 1;
  transition: opacity 0.3s ease-out;
}

.modal.hidden {
  opacity: 0;
  pointer-events: none;
}

然后在 JavaScript 里控制 classList.toggle('hidden') 来切换弹窗状态。


🎨 总结

  • 渐入 + 缩放(fadeInScale):适合现代 UI 设计,常见于 Material Design。
  • 从顶部滑入(slideDown):适合通知类弹窗。
  • 从底部弹出(bounceUp):适合移动端底部弹窗。
  • 旋转进入(rotateIn):适合有趣的弹窗动画。
  • 模糊淡入(blurIn):适合高端风格的 UI 设计。