.rain { po…">
当前位置:首页 > CSS

css 制作下雨特效

2026-03-11 23:27:29CSS

使用 CSS 制作下雨特效

方法一:纯 CSS 动画

通过 CSS 动画和伪元素创建雨滴下落效果。

<div class="rain"></div>
.rain {
  position: relative;
  width: 100%;
  height: 100vh;
  overflow: hidden;
  background: #222;
}

.rain::before {
  content: "";
  position: absolute;
  top: -50px;
  left: 0;
  width: 1px;
  height: 50px;
  background: linear-gradient(transparent, #fff);
  animation: rain 1s linear infinite;
}

@keyframes rain {
  0% {
    transform: translateY(-50px) rotate(15deg);
  }
  100% {
    transform: translateY(100vh) rotate(15deg);
  }
}

方法二:多雨滴效果

使用 JavaScript 动态生成多个雨滴元素,增强真实感。

css 制作下雨特效

<div class="rain-container"></div>
.rain-container {
  position: relative;
  width: 100%;
  height: 100vh;
  overflow: hidden;
  background: #111;
}

.raindrop {
  position: absolute;
  width: 1px;
  height: 30px;
  background: linear-gradient(transparent, rgba(255, 255, 255, 0.8));
  animation: fall linear infinite;
}

@keyframes fall {
  to {
    transform: translateY(100vh);
  }
}
const container = document.querySelector('.rain-container');
const dropsCount = 100;

for (let i = 0; i < dropsCount; i++) {
  const drop = document.createElement('div');
  drop.classList.add('raindrop');
  drop.style.left = `${Math.random() * 100}%`;
  drop.style.top = `${Math.random() * -100}px`;
  drop.style.animationDuration = `${0.5 + Math.random() * 1}s`;
  drop.style.opacity = Math.random();
  container.appendChild(drop);
}

方法三:雨滴涟漪效果

添加雨滴落地时的涟漪效果,增强视觉效果。

css 制作下雨特效

.ripple {
  position: absolute;
  width: 10px;
  height: 10px;
  border-radius: 50%;
  background: rgba(255, 255, 255, 0.3);
  animation: ripple 0.5s ease-out;
  transform: scale(0);
}

@keyframes ripple {
  to {
    transform: scale(3);
    opacity: 0;
  }
}
container.addEventListener('animationiteration', (e) => {
  if (e.target.classList.contains('raindrop')) {
    const ripple = document.createElement('div');
    ripple.classList.add('ripple');
    ripple.style.left = e.target.offsetLeft + 'px';
    ripple.style.top = '90vh';
    container.appendChild(ripple);
    setTimeout(() => ripple.remove(), 500);
  }
});

方法四:斜向雨滴

通过旋转雨滴元素创建斜向下雨效果。

.raindrop {
  transform: rotate(15deg);
}

方法五:雨滴大小和速度变化

通过随机设置雨滴高度和动画时长,模拟真实雨滴效果。

drop.style.height = `${10 + Math.random() * 20}px`;
drop.style.animationDuration = `${0.3 + Math.random() * 0.7}s`;

这些方法可以单独使用或组合使用,根据需求调整参数如雨滴数量、速度、方向和视觉效果。

标签: 特效css
分享给朋友:

相关文章

css制作图片

css制作图片

CSS 制作图片的方法 使用 background-image 属性 通过 background-image 属性可以为元素设置背景图片。这种方法适用于将图片作为背景而非内容的一部分。 .eleme…

css怎么制作三角形

css怎么制作三角形

使用边框制作三角形 通过设置元素的边框宽度和颜色,可以创建不同方向的三角形。将元素的宽度和高度设为0,利用边框的透明属性实现。 .triangle-up { width: 0; height…

css怎么制作首字下沉

css怎么制作首字下沉

首字下沉的实现方法 首字下沉是一种常见的排版效果,通过放大段落的首字母并使其下沉,可以增强文本的视觉吸引力。以下是几种实现方法: 使用::first-letter伪元素 通过CSS的::first-…

css 制作输入框

css 制作输入框

基础输入框样式 使用 input 或 textarea 元素创建输入框,通过 CSS 设置边框、圆角、内边距和背景色: .input-basic { border: 1px solid #ccc…

css个人简历制作两页

css个人简历制作两页

使用CSS制作两页个人简历 创建两页的个人简历需要合理布局和分页控制,以下是实现方法: HTML基础结构 <!DOCTYPE html> <html lang="zh-CN">…

css制作三行三列表格

css制作三行三列表格

使用HTML和CSS创建三行三列表格 通过HTML的<table>标签结合CSS样式可以轻松实现三行三列的表格结构。 <table class="three-by-three"&g…