简单的css动画-加载时的圆形滚动条

发布于:2022-12-06 ⋅ 阅读:(827) ⋅ 点赞:(0)

目录

一,加载时候的圆形滚动条效果

 二,html代码结构与css样式设置

1.盒子设置

2.圆点们的定位

 三,动画的实现

1.定义动画规则

2.设置动画延时播放

3.最后优化

四,另一种麻烦的办法


一,加载时候的圆形滚动条效果

我们在播放视频,在视频的加载中,经常出现一个圆点组成的圆形滚动条的加载效果,如下图。今天我们就尝试使用css的动画效果来实现这个效果。

分析下图,是八个圆点组成这个图像,八个圆点依次缩放大小,就可以实现这个动画效果。

 二,html代码结构与css样式设置

1.盒子设置

由上分析,那么我们使用两个div分别嵌套四个div。

<div class="box1">
        <div>1</div>
        <div>2</div>
        <div>3</div>
        <div>4</div>
    </div>
    <div class="box2">
        <div>3</div>
        <div>4</div>
        <div>5</div>
        <div>6</div>
    </div>

然后我们为box1,box2设置各种样式,设置边框使其轮廓清晰可见,用绝对定位使其重叠在一起,然后为box2设置旋转45度。

效果与代码如下。

* {
            padding: 0;
            margin: 0;
        }

        .box1{
            width: 300px;
            height: 300px;
            border: 1px solid black;
            position: absolute;
            top: 200px;
            left:600px;
        }
        .box2{
            width: 300px;
            height: 300px;
            border: 1px solid black;
            position: absolute;
            top: 200px;
            left: 600px;
            transform: rotate(45deg);
        }

2.圆点们的定位

box中的四个div(圆点),分别为其设置宽高样式,圆角边框,背景颜色使其清晰可见,再使用绝对定位,一个一个的将其定位在正方形大盒子(box1与box2)的四个角里面。

.box1 div{
            width: 50px;
            height: 50px;
            border-radius: 50%;
            background-color: aquamarine;
            position: absolute;
            animation: box infinite both 2s;
        }
        .box1 div:nth-child(1){
            top:0px;
            left: 0;

        }
        .box1 div:nth-child(2){
            top:0;
            right: 0;
            animation-delay: 0.5s;
        }
        .box1 div:nth-child(3){
            bottom: 0;
            right: 0;
            animation-delay: 1s;
        }
        .box1 div:nth-child(4){
            bottom: 0;
            left: 0;
            animation-delay: 1.5s;
        }
        .box2 div{
            width: 50px;
            height: 50px;
            border-radius: 50%;
            background-color: aquamarine;
            position: absolute;
            animation: box infinite both 2s;
        }
        .box2 div:nth-child(1){
            top:0px;
            left: 0;
            animation-delay: 0.25s;
        }
        .box2 div:nth-child(2){
            top:0;
            right: 0;
            animation-delay: 0.75s;
        }
        .box2 div:nth-child(3){
            bottom: 0;
            right: 0;
            animation-delay: 1.25s;
        }
        .box2 div:nth-child(4){
            bottom: 0;
            left: 0;
            animation-delay: 1.75s;
        }

 三,动画的实现

1.定义动画规则

使用 transform: scale(1);使每个圆点进行缩放,初始状态和结束状态,使其不变,动画的50%状态缩放为原来的一半。,transform: scale(0.5)。可以使用opacity: 0.5;使其在缩放一半时半透明,效果更加清晰可见。

 @keyframes box {
            0% {
                -webkit-transform: scale(1);
                transform: scale(1);
            }

            50% {
                -webkit-transform: scale(0.5);
                transform: scale(0.5);
                opacity: 0.5;
            }

            100% {
                -webkit-transform: scale(1);
                transform: scale(1);
                opacity: 1;
            }
        }

2.设置动画延时播放

为每个圆点的div设置animation-delay属性,使其动画延时都不一样,从而出现依次缩小放大的效果。本人设置动画时间为两秒,为box1里第1,2,3,4个圆点div设置延时为0.25s,0.75s,1.25s,1.75s,box2里的5,6,7,8个圆点div设置延时为0.5s,1s,1.5s和2s,正好每个相邻的圆点相差0.25s,并将动画的播放次数设置为无限,从而达到效果。

3.最后优化

记得除去box1,2的边框线,和div内的文字内容,就可以实现想要的效果。

四,另一种麻烦的办法

还有另一种办法看实现一样的效果,只是使用了定位将圆点排列称为圆环,别的都一样,在这里给出代码,不做赘叙。不建议使用这个办法。

html:

 <div class="box">
        <div></div>
        <div></div>
        <div></div>
        <div></div>
        <div></div>
        <div></div>
        <div></div>
        <div></div>
        <div></div>
        <div></div>
        <div></div>
        <div></div>
    </div>

css样式:

  * {
            padding: 0;
            margin: 0;
        }

        .box {
            position: relative;
            width: 200px;
            height: 200px;
            margin: 50px auto;
            background-color: antiquewhite;
        }

        .box>div {
            position: absolute;
            background-color: aqua;
            border-radius: 50%;
            width: 40px;
            height: 40px;
            margin: 0;
            padding: 0;
            animation: box infinite both 2s;
        }

        .box>div:nth-child(1) {
            top: 20px;
            left: 40px;
        }

        .box>div:nth-child(2) {
            top: 0px;
            left: 80px;
            animation-delay: 0.25s;
        }

        .box>div:nth-child(3) {
            top: 20px;
            left: 120px;
            animation-delay: 0.5s;
        }

        .box>div:nth-child(4) {
            top: 60px;
            left: 140px;
            animation-delay: 0.75s;
        }

        .box>div:nth-child(5) {
            top: 100px;
            left: 120px;
            animation-delay: 1s;
        }

        .box>div:nth-child(6) {
            top: 120px;
            left: 80px;
            animation-delay: 1.25s;
        }

        .box>div:nth-child(7) {
            top: 100px;
            left: 40px;
            animation-delay: 1.5s;
        }

        .box>div:nth-child(8) {
            top: 60px;
            left: 20px;
            animation-delay: 1.75s;
        }

        .box>div:nth-child(9) {
            top: 160px;
            left: 20px;
            animation-delay: 0s;
        }

        .box>div:nth-child(10) {
            top: 160px;
            left: 60px;
            animation-delay: 0.5s;
        }

        .box>div:nth-child(11) {
            top: 160px;
            left: 100px;
            animation-delay: 1s;
        }

        .box>div:nth-child(12) {
            top: 160px;
            left: 140px;
            animation-delay: 1.5s;
        }

        @keyframes box {
            0% {
                -webkit-transform: scale(1);
                transform: scale(1);
            }

            50% {
                -webkit-transform: scale(0.5);
                transform: scale(0.5);
                opacity: 0.5;
            }

            100% {
                -webkit-transform: scale(1);
                transform: scale(1);
                opacity: 1;
            }
        }

 效果展示:

本人才疏学浅,若是可以帮助各位就心满意足了,不足之处还请多多指教。

本文含有隐藏内容,请 开通VIP 后查看

网站公告

今日签到

点亮在社区的每一天
去签到