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>
相关推荐
子兮曰1 小时前
async/await高级模式:async迭代器、错误边界与并发控制
前端·javascript·github
恋猫de小郭2 小时前
2026 Flutter VS React Native ,同时在 AI 时代 VS Native 开发,你没见过的版本
android·前端·flutter
GIS之路4 小时前
ArcGIS Pro 中的 Notebooks 入门
前端
IT_陈寒5 小时前
React状态管理终极对决:Redux vs Context API谁更胜一筹?
前端·人工智能·后端
Kagol6 小时前
TinyVue 支持 Skills 啦!现在你可以让 AI 使用 TinyVue 组件搭建项目
前端·agent·ai编程
柳杉6 小时前
从零打造 AI 全球趋势监测大屏
前端·javascript·aigc
simple_lau6 小时前
Cursor配置MasterGo MCP:一键读取设计稿生成高还原度前端代码
前端·javascript·vue.js
睡不着先生6 小时前
如何设计一个真正可扩展的表单生成器?
前端·javascript·vue.js
天蓝色的鱼鱼6 小时前
模块化与组件化:90%的前端开发者都没搞懂的本质区别
前端·架构·代码规范
明君879976 小时前
Flutter 如何给图片添加多行文字水印
前端·flutter