HTML5+CSS3小实例:黏性小球loading动画

300 阅读1分钟

HTML5+CSS3实现黏性小球的loading动画,主要通过 contrast 和 blur 两个滤镜搭配使用,进而实现小球来回穿梭的动画,动画过程伴随着融球效果,如此精致细腻的动画,用来做个loading,实在有点大材小用。

效果:

源码:

<!DOCTYPE html>
<html>

<head>
    <meta http-equiv="content-type" content="text/html; charset=utf-8">
    <meta name="viewport" content="width=device-width,initial-scale=1,maximum-scale=1,user-scalable=no">

    <title>粘性球球</title>
    <link rel="stylesheet" href="../css/12.css">
</head>

<body>
    <div class="effect">
        <div class="bigball"></div>
        <div class="smallball"></div>
    </div>
</body>

</html>
*{
    margin: 0;
    padding: 0;
}
body{
    display: flex;
    justify-content: center;
    align-items: center;
    height: 100vh;
    background: linear-gradient(to top,#537895,#09203f);
}
.effect{
    position: relative;
    width: 320px;
    height: 320px;
    border-radius: 50%;
    /* 对比度 */
    filter: contrast(10);
    background-color: white;
}
.bigball,.smallball{
    position: absolute;
    top: 50%;
    left: 50%;
    transform: translate(-50%,-50%);
    padding: 10px;
    border-radius: 50%;
    /* 设置模糊度,配合上面的contrast来显示圆球的粘性效果 */
    filter: blur(5px);
}
.bigball{
    width: 100px;
    height: 100px;
    background-color: black;
}
.smallball{
    width: 60px;
    height: 60px;
    background-color: red;
    /* 动画:名称 时长 infinite是无限次播放 */
    animation: ball 3s infinite;
}

/* 接下来定义小球的动画 */
@keyframes ball{
    0%,100%{
        left: 50px;
        width: 60px;
        height: 60px;
    }
    4%,54%{
        width: 60px;
        height: 60px;
    }
    10%,60%{
        width: 50px;
        height: 70px;
    }
    20%,70%{
        width: 60px;
        height: 60px;
    }
    34%,90%{
        width: 70px;
        height: 50px;
    }
    41%{
        width: 60px;
        height: 60px;
    }
    50%{
        left: 270px;
        width: 60px;
        height: 60px;
    }
}