CSS 实现波浪效果

参考文章:巧妙使用 CSS 渐变来实现波浪动画_css实现波浪动效-CSDN博客

思路

绘制两个曲面拼接起来,就形成了波浪形。

使用 radial-gradient 绘制曲面。

css 复制代码
radial-gradient(
  [size] at [position],    /* 形状和位置 */
  [color-stop1],          /* 颜色断点1 */
  [color-stop2]           /* 颜色断点2 */
)

/* 举例: */
background: radial-gradient(90% 50% at 20% 100%, #fff 98%, var(--color) 99%)

Size(大小):90% 50%

第一个值:渐变椭圆的宽度,第二个值:渐变椭圆的高度;也可以使用关键字:circle(圆形)或 ellipse(椭圆形)。

**改变波浪形状大小:**增大90%缩小50%可以让波浪更宽,反之更窄。

Position(位置):at 20% 100%

at 后面的值定义渐变的中心点。第一个值:水平位置(left, center, right 或百分比),第二个值:垂直位置(top, center, bottom 或百分比)。

**移动波浪的位置:**缩小/增大20%会左/右移动波浪的位置。

Color Stops(颜色断点):#fff 98%, var(--color) 99%

每个颜色断点包含:颜色值 + 位置值(百分比),可以有多个颜色断点。

绘制曲面代码

css 复制代码
.wave {
  position: absolute;
  top: 0;
  left: 0;
  width: 100%;
  height: 100%;
  background: radial-gradient(100% 57% at bottom,#0000 100%, #2196F3 100.5%) no-repeat;
  background-size: 50% 100%;
  background-position: 0 100%;
  background-repeat: no-repeat;
}

绘制波浪代码

css 复制代码
.wave {
  position: absolute;
  top: 0;
  left: 0;
  width: 100%;
  height: 100%;
  --color: #2196F3;
  --w1: radial-gradient(100% 57% at bottom,#0000 100%, var(--color) 100.5%) no-repeat;
  --w2: radial-gradient(100% 58% at top, var(--color) 100%,#0000 100.5%) no-repeat;
  background: var(--w1), var(--w2);
  background-size: 50% 100%, 50% 100%;
  background-position: 0 100%, 100% 100%;
  background-repeat: no-repeat;
}

具体使用需要自行调节 radial-gradient,background-size,background-position 的值,使形状衔接流畅。调整 background-size 的百分比会改变波浪的覆盖范围,调整 background-position 的值会改变两个渐变图案的重叠程度。

具体实现

登录页背景

代码

html 复制代码
<template>
  <view class="wave-container">  
    <view class="wave1"></view>
    <view class="wave2"></view>
    <view class="wave3"></view>
  </view>
</template>
css 复制代码
<style lang="scss" scoped>
.wave-container {  
  position: relative;  
  width: 100%;
  height: 300px;
  overflow: hidden;
}
.wave1 {
  position: absolute;
  top: 0;
  left: 0;
  width: 100%;
  height: 100%;
  --color: #487CF5;
  --w1: radial-gradient(95% 56.8% at 50% 100%,#0000 100%, var(--color) 100.5%) no-repeat;
  --w2: radial-gradient(150% 46.8% at 70% 10%, var(--color) 100%,#0000 100.5%) no-repeat;
  background: var(--w1), var(--w2);
  background-size: 70% 100%, 30% 100%;
  background-position: 0 100%, 100% 100%;
  background-repeat: no-repeat;
  z-index: 0;
}
.wave2 {
  position: absolute;
  top: 0;
  left: 0;
  width: 100%;
  height: 100%;
  --color: #819adf;
  --w1: radial-gradient(95% 53% at 38% 100%,#0000 100%, var(--color) 100.5%) no-repeat;
  --w2: radial-gradient(160% 56.8% at 80% 10%, var(--color) 100%,#0000 100.5%) no-repeat;
  background: var(--w1), var(--w2);
  background-size: 70% 100%, 30% 100%;
  background-position: 0 100%, 100% 100%;
  background-repeat: no-repeat;
  z-index: -1;
  opacity: 0.1; // 使用透明度实现叠加效果
}
.wave3 {
  position: absolute;
  top: 0;
  left: 0;
  width: 100%;
  height: 100%;
  --color: #E8F2FB;
  --w1: radial-gradient(140% 24% at 50% 80%,#0000 100%, var(--color) 100.5%) no-repeat;
  --w2: radial-gradient(80% 51% at 30% 10%, var(--color) 100%,#0000 100.5%) no-repeat;
  background: var(--w1), var(--w2);
  background-size: 20% 100%, 80% 100%;
  background-position: 0 100%, 100% 100%;
  background-repeat: no-repeat;
  z-index: -2;
}
</style>
相关推荐
却尘15 小时前
Next.js 请求最佳实践 - vercel 2026一月发布指南
前端·react.js·next.js
ccnocare15 小时前
浅浅看一下设计模式
前端
Lee川15 小时前
🎬 从标签到屏幕:揭秘现代网页构建与适配之道
前端·面试
Ticnix15 小时前
ECharts初始化、销毁、resize 适配组件封装(含完整封装代码)
前端·echarts
纯爱掌门人15 小时前
终焉轮回里,藏着 AI 与人类的答案
前端·人工智能·aigc
twl15 小时前
OpenClaw 深度技术解析
前端
崔庆才丨静觅15 小时前
比官方便宜一半以上!Grok API 申请及使用
前端
星光不问赶路人15 小时前
vue3使用jsx语法详解
前端·vue.js
天蓝色的鱼鱼15 小时前
shadcn/ui,给你一个真正可控的UI组件库
前端
布列瑟农的星空15 小时前
前端都能看懂的Rust入门教程(三)——控制流语句
前端·后端·rust