CSS3 实现 loading 动画效果

在观看视频时经常遇到卡顿,此时屏幕中间通常会有一个圆圈一直在转,作为一种正在加载的显示
其实,它可以通过CSS3的动画实现

实现方法

由围成一个圈的一些小圆点,然后顺时针方向,逐个半径变小,再变大,也就是消失再出现的过程,每个点变小不是同时的,设置相同的时间差,就能形成加载中的视觉效果.
首先,在一个块中绘制4个小圆点,为了明显,先设置一下边框,后面删除即可

<div class="circleBox">
    <p></p>
    <p></p>
    <p></p>
    <p></p>
</div>

然后让其固定在屏幕正中央,再分别将4个点放到边框4个角上,如图所示


/*居中显示*/
.circleBox{
    position: absolute;
    top: 50%;
    left: 50%;
    margin: -20px 0 0 -20px;
    width: 40px;
    height: 40px;
    /*border: 1px solid red;*/
}
/*4个点居于四个角*/
p:nth-of-type(1){
    position: absolute;
    left: 0;
    top: 0;
}

p:nth-of-type(2){
    position: absolute;
    right: 0;
    top: 0;
}
p:nth-of-type(3){
    position: absolute;
    right: 0;
    bottom: 0;
}
p:nth-of-type(4){
    position: absolute;
    left: 0;
    bottom: 0;
}

在这里插入图片描述
4个点看着太稀疏,我们上面整个框复制一遍,也放在屏幕正中央,不需要另外写太多CSS代码,然后将其旋转45度,实现下面的效果

/*复制上面的4点环,再旋转45度*/
.circleBox:nth-of-type(2){
    transform: rotate(45deg);
}

在这里插入图片描述
去除边框之后,效果如下
在这里插入图片描述

接下来,就是实现圆点相继变小的过程,我们给所有的圆点设置动画,使其依次慢慢消失


@keyframes move{
    0%{
        transform:scale(0);
    }
    50%{
        transform:scale(1);
    }
    100%{
        transform:scale(0);
    }

}

设置时间差


/*设置时间差*/
.circleBox:nth-of-type(1) p:nth-of-type(1){
    animation-delay:-0.1s;
}

.circleBox:nth-of-type(2) p:nth-of-type(4){
    animation-delay:-0.3s;
}

.circleBox:nth-of-type(1) p:nth-of-type(4){
    animation-delay:-0.5s;
}
.circleBox:nth-of-type(2) p:nth-of-type(3){
    animation-delay:-0.7s;
}

.circleBox:nth-of-type(1) p:nth-of-type(3){
    animation-delay:-0.9s;
}

.circleBox:nth-of-type(2) p:nth-of-type(2){
    animation-delay:-1.1s;
}

.circleBox:nth-of-type(1) p:nth-of-type(2){
    animation-delay:-1.3s;
}

.circleBox:nth-of-type(2) p:nth-of-type(1){
    animation-delay:-1.5s;
}

大概思路就是这样了,整个代码文件以及效果演示视频,请前往
https://github.com/zhangtsh5/loading-display.git

  • 2
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值