当前位置:首页 > CSS

css 制作下雨特效

2026-04-01 19:35:56CSS

使用 CSS 制作下雨特效

通过 CSS 动画和伪元素可以模拟下雨效果。以下是实现方法:

创建雨滴元素

定义雨滴的基本样式,使用伪元素 ::before::after 生成雨滴形状:

.raindrop {
  position: absolute;
  width: 2px;
  height: 15px;
  background: linear-gradient(to bottom, transparent, #a0d8f3);
  border-radius: 0 0 5px 5px;
}

添加下落动画

使用 @keyframes 定义雨滴从顶部到底部的动画:

@keyframes fall {
  from { transform: translateY(-100px) rotate(15deg); }
  to { transform: translateY(100vh) rotate(15deg); }
}

随机化雨滴位置和延迟

通过 JavaScript 动态生成雨滴并设置随机属性:

const container = document.querySelector('.rain-container');
for (let i = 0; i < 50; i++) {
  const drop = document.createElement('div');
  drop.className = 'raindrop';
  drop.style.left = `${Math.random() * 100}vw`;
  drop.style.animation = `fall ${0.5 + Math.random() * 1}s linear infinite`;
  drop.style.animationDelay = `${Math.random() * 2}s`;
  container.appendChild(drop);
}

添加涟漪效果(可选)

当雨滴"落地"时创建涟漪动画:

css 制作下雨特效

@keyframes ripple {
  0% { transform: scale(0); opacity: 1; }
  100% { transform: scale(3); opacity: 0; }
}

.ripple {
  position: absolute;
  width: 10px;
  height: 10px;
  border-radius: 50%;
  background: rgba(160, 216, 243, 0.5);
  animation: ripple 0.5s linear forwards;
}

完整示例代码

<!DOCTYPE html>
<html>
<head>
<style>
  body {
    margin: 0;
    height: 100vh;
    overflow: hidden;
    background: #222;
  }

  .rain-container {
    position: relative;
    width: 100%;
    height: 100%;
  }

  .raindrop {
    position: absolute;
    width: 2px;
    height: 15px;
    background: linear-gradient(to bottom, transparent, #a0d8f3);
    border-radius: 0 0 5px 5px;
  }

  @keyframes fall {
    from { transform: translateY(-100px) rotate(15deg); }
    to { transform: translateY(100vh) rotate(15deg); }
  }

  @keyframes ripple {
    0% { transform: scale(0); opacity: 1; }
    100% { transform: scale(3); opacity: 0; }
  }

  .ripple {
    position: absolute;
    width: 10px;
    height: 10px;
    border-radius: 50%;
    background: rgba(160, 216, 243, 0.5);
    animation: ripple 0.5s linear forwards;
  }
</style>
</head>
<body>
  <div class="rain-container"></div>

  <script>
    const container = document.querySelector('.rain-container');

    // 创建雨滴
    for (let i = 0; i < 80; i++) {
      const drop = document.createElement('div');
      drop.className = 'raindrop';
      drop.style.left = `${Math.random() * 100}vw`;
      drop.style.animation = `fall ${0.5 + Math.random() * 1}s linear infinite`;
      drop.style.animationDelay = `${Math.random() * 2}s`;
      container.appendChild(drop);

      // 创建涟漪效果
      setTimeout(() => {
        setInterval(() => {
          const ripple = document.createElement('div');
          ripple.className = 'ripple';
          ripple.style.left = drop.style.left;
          ripple.style.bottom = '0';
          container.appendChild(ripple);

          // 移除涟漪元素
          setTimeout(() => {
            ripple.remove();
          }, 500);
        }, 1000 + Math.random() * 1000);
      }, Math.random() * 2000);
    }
  </script>
</body>
</html>

增强效果的技巧

调整雨滴数量可以改变降雨强度,增加更多雨滴会使效果更密集。修改动画时间和延迟可以创建不同速度的雨滴,模拟真实降雨的不规则性。添加背景模糊或渐变可以增强场景深度感。

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

相关文章

css网页制作

css网页制作

CSS网页制作基础 CSS(层叠样式表)用于控制网页的视觉表现,包括布局、颜色、字体等。以下是核心知识点: 选择器与样式规则 /* 标签选择器 */ p { color: #333; fo…

css怎么制作段落

css怎么制作段落

段落样式基础设置 使用 p 标签定义段落,通过CSS控制字体、间距等属性: p { font-family: Arial, sans-serif; font-size: 16px;…

css制作三行三列表格

css制作三行三列表格

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

css制作雪花

css制作雪花

CSS 制作雪花效果 使用纯CSS可以创建简单的雪花动画效果,主要通过伪元素、动画和变换实现。 基础雪花结构 <div class="snowflake"></div> 六…

网页制作 css

网页制作 css

CSS 基础语法 CSS(层叠样式表)用于控制网页的样式和布局。基本语法由选择器和声明块组成。选择器指向需要样式化的HTML元素,声明块包含一个或多个用分号分隔的声明,每个声明由属性和值组成。 选择…

css制作菜单

css制作菜单

CSS 制作菜单的方法 使用 CSS 可以制作多种风格的菜单,包括水平菜单、垂直菜单、下拉菜单等。以下是几种常见的实现方式。 水平菜单 水平菜单通常用于导航栏,可以通过 display: inlin…