css制作海浪
使用CSS制作海浪效果
通过CSS的clip-path属性和动画可以创建动态的海浪效果。以下是一种实现方法:
<div class="wave"></div>
.wave {
position: absolute;
bottom: 0;
left: 0;
width: 100%;
height: 100px;
background: linear-gradient(to right, #4facfe 0%, #00f2fe 100%);
clip-path: path("M0,0 C150,200 350,0 500,100 L500,0 L0,0 Z");
animation: wave 8s linear infinite;
}
@keyframes wave {
0% {
clip-path: path("M0,0 C150,200 350,0 500,100 L500,0 L0,0 Z");
}
50% {
clip-path: path("M0,0 C150,0 350,200 500,100 L500,0 L0,0 Z");
}
100% {
clip-path: path("M0,0 C150,200 350,0 500,100 L500,0 L0,0 Z");
}
}
使用SVG结合CSS
另一种方法是使用SVG路径配合CSS动画:
<div class="wave-container">
<svg viewBox="0 0 1200 120" preserveAspectRatio="none">
<path class="wave" d="M0,0 C150,200 350,0 500,100 C650,200 850,0 1000,100 C1150,200 1350,0 1500,100 L1500,0 L0,0 Z"></path>
</svg>
</div>
.wave-container {
position: relative;
height: 150px;
overflow: hidden;
}
.wave {
fill: #4facfe;
animation: wave 8s linear infinite;
}
@keyframes wave {
0% {
transform: translateX(0);
}
100% {
transform: translateX(-50%);
}
}
多层波浪效果
创建更真实的海浪效果可以叠加多个波浪层:
<div class="ocean">
<div class="wave wave1"></div>
<div class="wave wave2"></div>
<div class="wave wave3"></div>
</div>
.ocean {
position: relative;
height: 200px;
overflow: hidden;
}
.wave {
position: absolute;
bottom: 0;
left: 0;
width: 200%;
height: 100%;
background-repeat: repeat no-repeat;
background-position: 0 bottom;
transform-origin: center bottom;
}
.wave1 {
background-size: 50% 80px;
background-image: url('data:image/svg+xml;utf8,<svg viewBox="0 0 1200 120" xmlns="http://www.w3.org/2000/svg"><path d="M0,0 C150,40 350,-40 500,0 C650,40 850,-40 1000,0 C1150,40 1350,-40 1500,0 L1500,120 L0,120 Z" fill="%234facfe"/></svg>');
animation: wave 10s linear infinite;
}
.wave2 {
background-size: 50% 100px;
background-image: url('data:image/svg+xml;utf8,<svg viewBox="0 0 1200 120" xmlns="http://www.w3.org/2000/svg"><path d="M0,0 C150,60 350,-60 500,0 C650,60 850,-60 1000,0 C1150,60 1350,-60 1500,0 L1500,120 L0,120 Z" fill="%2300f2fe"/></svg>');
animation: wave 8s linear infinite reverse;
opacity: 0.7;
}
.wave3 {
background-size: 50% 60px;
background-image: url('data:image/svg+xml;utf8,<svg viewBox="0 0 1200 120" xmlns="http://www.w3.org/2000/svg"><path d="M0,0 C150,20 350,-20 500,0 C650,20 850,-20 1000,0 C1150,20 1350,-20 1500,0 L1500,120 L0,120 Z" fill="%2300d1fe"/></svg>');
animation: wave 12s linear infinite;
opacity: 0.5;
}
@keyframes wave {
0% {
transform: translateX(0) translateZ(0);
}
100% {
transform: translateX(-50%) translateZ(0);
}
}
这些方法提供了不同复杂度的海浪效果实现,可根据项目需求选择合适的方式。SVG方法通常能提供更平滑的波浪曲线,而纯CSS方法则更轻量。多层波浪叠加可以创造出更真实的海洋波动效果。







