炫酷水波浪Loading过渡动画

海轰Pro

共 9116字,需浏览 19分钟

 · 2021-08-13

效果展示

Demo代码

HTML

<!DOCTYPE html>
<html lang="en">

    <head>
        <meta charset="UTF-8">
        <meta http-equiv="X-UA-Compatible" content="IE=edge">
        <meta name="viewport" content="width=device-width, initial-scale=1.0">
        <link rel="stylesheet" href="style.css">
        <title>Document</title>
    </head>

    <body>
        <section>
            <div class="circle">
                <div class="wave"></div>
            </div>
        </section>
    </body>

</html>

CSS

/* 
模版css样式
仅供演示使用 
*/


htmlbody {
 margin0;
 height100%;


body {
 display: flex;
 justify-content: center;
 align-items: center;
 background-color#12383e;
}

section {
 width650px;
 height300px;
 padding10px;
 position: relative;
 display: flex;
 align-items: center;
 justify-content: center;
 border-radius20px;
 border18px solid white;
 overflow: hidden;
}

section::before {
 content'CSS';
 width150px;
 height150px;
 text-align: center;
 line-height250px;
 background-color#00cec9;
 position: absolute;
 top: -76px;
 right: -76px;
 transformtranslate(50%, -50%);
 font-size32px;
 font-weight500;
 font-family: sans-serif;
 color: white;
 transformrotate(45deg);
}

section::after {
 content'';
 position: absolute;
 width100%;
 height100%;
 border10px solid white;
 border-radius20px;
}

/* 实现代码 */

.circle {
 position: relative;
 width200px;
 height200px;
 background#b0f4ff;
 border-radius50%;
 overflow: hidden;
 animation: loadingBreath 5s infinite linear;
}

.circle::before {
 content'Loading...';
 position: absolute;
 top50%;
 left50%;
 transformtranslate(-50%, -50%);
 font-size18px;
 letter-spacing2px;
 color#10a789;
 font-family: sans-serif;
 z-index2;
}

.circle::after {
 content'';
 position: absolute;
 width100%;
 height25%;
 bottom0;
 background-imagelinear-gradient(to top, #12c8e0, #36e9ff, #5ffbf1);
 animation: loadingRun 5s linear infinite;
}

.wave::before {
 content'';
 position: absolute;
 left: -50%;
 width200%;
 height200%;
 z-index1;
 background-color#85f7fb;
 border-radius52% 25% 62% 69%/25% 38%;
 animation: loadingWave 5s linear infinite;
}

.wave::after {
 content'';
 position: absolute;
 left: -50%;
 width200%;
 height200%;
 z-index1;
 background-color#d0f4ff;
 border-radius42% 38% 40% 62%/28% 35%;
 animation: loadingWave 5s ease-in-out infinite;
}

/* 呼吸灯动画 */

@keyframes loadingBreath {
 0% {
  box-shadow0 0 5px 0 #85f7fb;
 }
 25% {
  box-shadow0 0 20px 0 #85f7fb;
 }
 50% {
  box-shadow0 0 5px 0 #85f7fb;
 }
 75% {
  box-shadow0 0 20px 0 #85f7fb;
 }
 100% {
  box-shadow0 0 5px 0 #85f7fb;
 }
}

/* 底部液体上升动画 */

@keyframes loadingRun {
 0% {
  height25%;
 }
 100% {
  height100%;
 }
}

/* wave动画 */

@keyframes loadingWave {
 0% {
  top: -100%;
  transformrotate(0);
 }
 100% {
  top: -200%;
  transformrotate(360deg);
 }
}

原理详解

步骤1

从效果图上分析

可以将其分为两个部分

  • 容器
  • 波浪

这里使用两个div,一个为circle类,一个为wave类,分别代表容器和wave

            <div class="circle">
                <div class="wave"></div>
            </div>

步骤2

设置circle类

  • 相对定位
  • 宽度、高度均为200px
  • 背景色:#b0f4ff
  • 圆角:50%
.circle {
 position: relative;
 width200px;
 height200px;
 background#b0f4ff;
 border-radius50%;
}

效果图如下:

步骤3

利用.circle::befor伪元素

用于显示“Loading...”字样

设置为

  • 绝对定位
  • 使其位于「正中间」( top: 50%; left: 50%; transform: translate(-50%, -50%);)
  • 字体大小:18px
  • 颜色:#10a789;
  • z-index:2(比1大就行 使其文字处于最上层)
.circle::before {
 content'Loading...';
 position: absolute;
 top50%;
 left50%;
 transformtranslate(-50%, -50%);
 font-size18px;
 letter-spacing2px;
 color#10a789;
 font-family: sans-serif;
 z-index2;
}

效果图如下:

步骤4

利用.circle::after伪元素

设置为

  • 绝对定位(bottom: 0; )
  • 宽度:100%
  • 高度:25%
  • 背景颜色为渐变色 linear-gradient(to top, #12c8e0, #36e9ff, #5ffbf1);
.circle::after {
 content'';
 position: absolute;
 width100%;
 height25%;
 bottom0;
 background-imagelinear-gradient(to top, #12c8e0, #36e9ff, #5ffbf1);
}

效果图如下:

步骤5

为.circle::after伪元素添加动画

使其随时间其高度逐渐增大

只需要明确两个关键帧

  • 初始位置:height: 25%
  • 结束位置:height: 100%
.circle::after {
 animation: loadingRun 5s linear infinite;
}


@keyframes loadingRun {
 0% {
  height25%;
 }
 100% {
  height100%;
 }
}

效果图如下:

步骤6

对circle设置隐藏溢出

.circle {
 overflow: hidden;
}

效果图如下:

步骤7

这里先注释circle隐藏溢出 以及 circle::after动画 便于后面单独分析

.circle {
 /* overflow: hidden; */
}
.circle::after {
 /* animation: loadingRun 5s linear infinite; */
}

然后我们使用wave的两个伪元素.wave::before、.wave::afte与cirle::after产生波浪的效果

首先设置wave::before

  • 绝对定位(left: -50%;)
  • 宽度、高度均为200%
  • z-index:1
  • 背景色:#85f7fb
  • border-radius: 52% 25% 62% 69%/25% 38%; 「重点」
.wave::before {
 content'';
 position: absolute;
 left: -50%;
 width200%;
 height200%;
 z-index1;
 background-color#85f7fb;
 border-radius52% 25% 62% 69%/25% 38%;/*重点*/
}

效果图如下:

注:.wave::before z-index为1 大于circile(0) 小于.circle::before(2)

为.wave::before 添加动画

效果描述

自身不断旋转的同时 也不断上升

.wave::before {
 animation: loadingWave 5s linear infinite;
}

@keyframes loadingWave {
 0% {
  top: -100%;
  transformrotate(0);
 }
 100% {
  top: -200%;
  transformrotate(360deg);
 }
}

效果图如下:

同理,对wave::after进行同样的设置

不同在于ç四边圆角率与before不同、颜色浅一点

border-radius: 42% 38% 40% 62%/28% 35%;
background-color#d0f4ff;

其他都一样

当wave::after、before运用同样的动画时

效果图如下:

步骤8

取消circle隐藏溢出 以及 circle::after动画

.circle {
 overflow: hidden; 
}
.circle::after {
  animation: loadingRun 5s linear infinite; 
}

效果图如下:

步骤9

最后为cirlce添加一个呼吸灯动画效果

.circle {
 animation: loadingBreath 5s infinite linear;
}

```css

@keyframes loadingBreath {
 0% {
  box-shadow0 0 5px 0 #85f7fb;
 }
 25% {
  box-shadow0 0 20px 0 #85f7fb;
 }
 50% {
  box-shadow0 0 5px 0 #85f7fb;
 }
 75% {
  box-shadow0 0 20px 0 #85f7fb;
 }
 100% {
  box-shadow0 0 5px 0 #85f7fb;
 }
}

得到最终效果图

结语

学习参考:

https://www.bilibili.com/video/BV1Ai4y1t7od https://developer.mozilla.org/zh-CN/docs/Web/CSS/::before

文章仅作为学习笔记,记录从0到1的一个过程

希望对您有所帮助,如有错误欢迎小伙伴指正~

我是 海轰ଘ(੭ˊᵕˋ)੭

如果您觉得写得可以的话,请点个赞吧

谢谢支持❤️

浏览 25
点赞
评论
收藏
分享

手机扫一扫分享

举报
评论
图片
表情
推荐
点赞
评论
收藏
分享

手机扫一扫分享

举报