巧妙使用 CSS3 实现 HTML 加载动画

仅使用 CSS 和 HTML 实现网页动画,HTML 动画,加载动画,CSS3 动画

发布用户:小梦

2023/04/04 13:17


虽然 CSS3 和 HTML5 已基本在互联网上普及,但出于对不同浏览器环境的兼容性考虑,还是先看看下面的效果是不是理想状态再使用吧。

代码好理解,实现起来也不算麻烦,思路是对每根线条(点、块)分别做动画,然后按照不一的时间组合起来,所以下面的 HTML 代码中,使用了空的 div 元素充当这些线条(点、块):

<div class="DIV_loadingAnimation1">
   <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>

<div class="DIV_loadingAnimation2">
   <div></div>
   <div></div>
   <div></div>
   <div></div>
   <div></div>
   <div></div>
   <div></div>
   <div></div>
</div>

<div class="DIV_loadingAnimation3">
   <div></div>
   <div></div>
   <div></div>
   <div></div>
   <div></div>
</div>

下面是 3 个动画的样式:

<style>
.DIV_loadingAnimation1 {
   position: relative;
   margin: 20px auto;
   width: 100px;
   height: 100px;
   box-shadow: 0 0 2px 0 rgba(0,0,0,.5);
   user-select: none;
}

   .DIV_loadingAnimation1 div {
       width: 18px;
       height: 4px;
       background: #27b6df;
       transform-origin: 100%;
       position: absolute;
       opacity: 0;
       border-radius: 25%;
   }

       .DIV_loadingAnimation1 div:nth-child(1) {
           animation: loadingAnimation1 1s linear infinite;
           transform: rotate(0deg);
           left: 21px;
           top: 50px;
       }

       .DIV_loadingAnimation1 div:nth-child(2) {
           animation: loadingAnimation1 1s linear infinite .08s;
           transform: rotate(30deg);
           left: 23px;
           top: 45px;
       }

       .DIV_loadingAnimation1 div:nth-child(3) {
           animation: loadingAnimation1 1s linear infinite .16s;
           transform: rotate(60deg);
           left: 27px;
           top: 41px;
       }

       .DIV_loadingAnimation1 div:nth-child(4) {
           animation: loadingAnimation1 1s linear infinite .24s;
           transform: rotate(90deg);
           left: 33px;
           top: 40px;
       }

       .DIV_loadingAnimation1 div:nth-child(5) {
           animation: loadingAnimation1 1s linear infinite .32s;
           transform: rotate(120deg);
           left: 39px;
           top: 41px;
       }

       .DIV_loadingAnimation1 div:nth-child(6) {
           animation: loadingAnimation1 1s linear infinite .41s;
           transform: rotate(150deg);
           left: 42px;
           top: 45px;
       }

       .DIV_loadingAnimation1 div:nth-child(7) {
           animation: loadingAnimation1 1s linear infinite .48s;
           transform: rotate(180deg);
           left: 43px;
           top: 50px;
       }

       .DIV_loadingAnimation1 div:nth-child(8) {
           animation: loadingAnimation1 1s linear infinite .56s;
           transform: rotate(210deg);
           left: 42px;
           top: 56px;
       }

       .DIV_loadingAnimation1 div:nth-child(9) {
           animation: loadingAnimation1 1s linear infinite .64s;
           transform: rotate(240deg);
           left: 38px;
           top: 59px;
       }

       .DIV_loadingAnimation1 div:nth-child(10) {
           animation: loadingAnimation1 1s linear infinite .72s;
           transform: rotate(270deg);
           left: 33px;
           top: 60px;
       }

       .DIV_loadingAnimation1 div:nth-child(11) {
           animation: loadingAnimation1 1s linear infinite .81s;
           transform: rotate(300deg);
           left: 28px;
           top: 59px;
       }

       .DIV_loadingAnimation1 div:nth-child(12) {
           animation: loadingAnimation1 1s linear infinite .9s;
           transform: rotate(330deg);
           left: 23px;
           top: 55px;
       }

@keyframes loadingAnimation1 {
   from {
       opacity: 1;
   }

   to {
       opacity: 0;
   }
}

.DIV_loadingAnimation2 {
   position: relative;
   margin: 20px auto;
   padding: 20px 0;
   width: 200px;
   text-align: center;
   box-shadow: 0 0 2px 0 rgba(0,0,0,.5);
   user-select: none;
   font-size: 0;
}

   .DIV_loadingAnimation2 div {
       width: 10px;
       height: 50px;
       background: #27b6df;
       display: inline-block;
       margin: 0 1px 0 0;
       transform: scaleY(.5);
   }

       .DIV_loadingAnimation2 div:nth-child(1) {
           animation: loadingAnimation2 1.5s linear infinite;
       }

       .DIV_loadingAnimation2 div:nth-child(2) {
           animation: loadingAnimation2 1.5s linear infinite .125s;
       }

       .DIV_loadingAnimation2 div:nth-child(3) {
           animation: loadingAnimation2 1.5s linear infinite .25s;
       }

       .DIV_loadingAnimation2 div:nth-child(4) {
           animation: loadingAnimation2 1.5s linear infinite .375s;
       }

       .DIV_loadingAnimation2 div:nth-child(5) {
           animation: loadingAnimation2 1.5s linear infinite .5s;
       }

       .DIV_loadingAnimation2 div:nth-child(6) {
           animation: loadingAnimation2 1.5s linear infinite .625s;
       }

       .DIV_loadingAnimation2 div:nth-child(7) {
           animation: loadingAnimation2 1.5s linear infinite .75s;
       }

       .DIV_loadingAnimation2 div:nth-child(8) {
           animation: loadingAnimation2 1.5s linear infinite .875s;
       }

@keyframes loadingAnimation2 {
   0% {
       transform: scaleY(.5);
   }

   20% {
       transform: scaleY(1);
   }

   40% {
       transform: scaleY(.5);
   }
}

.DIV_loadingAnimation3 {
   width: 100%;
   margin: 20px auto;
   padding: 20px 0;
   overflow: hidden;
   box-shadow: 0 0 2px 0 rgba(0,0,0,.5);
   user-select: none;
   font-size: 0;
}

   .DIV_loadingAnimation3 div {
       position: relative;
       background: #27b6df;
       display: inline-block;
       left: -50%;
       width: 20px;
       height: 20px;
   }

       .DIV_loadingAnimation3 div:nth-child(1) {
           animation: loadingAnimation3 cubic-bezier(0,1,1,.1) 2.75s infinite 1s;
       }

       .DIV_loadingAnimation3 div:nth-child(2) {
           animation: loadingAnimation3 cubic-bezier(0,1,1,.1) 2.75s infinite .75s;
       }

       .DIV_loadingAnimation3 div:nth-child(3) {
           animation: loadingAnimation3 cubic-bezier(0,1,1,.1) 2.75s infinite .5s;
       }

       .DIV_loadingAnimation3 div:nth-child(4) {
           animation: loadingAnimation3 cubic-bezier(0,1,1,.1) 2.75s infinite .25s;
       }

       .DIV_loadingAnimation3 div:nth-child(5) {
           animation: loadingAnimation3 cubic-bezier(0,1,1,.1) 2.75s infinite;
       }

@keyframes loadingAnimation3 {
   0% {
       left: -50%;
   }

   50% {
       left: 125%;
   }

   100% {
       left: 200%;
   }
}
</style>



代码写多了,脑力不够用了,请我喝杯咖啡提提神 ☕

警告!禁止网络诈骗、非法集资、非法套现等法律外的缘由转账汇款,你的赞赏转账请考虑再三后支付,此收款不作任何形式的退款。





金额:

* 支付宝支付后如果没有自动返回请手动返回此页并刷新。



* 在线内容服务不支持任何形式的退款/退费操作;支付后的订单记录可以在“用户主页 - 用户订单中心”查看。

此网站可能不完全兼容您目前的浏览器!
此页随时可能被替换下线,请不要依赖此页功能!
此页正在维护更新,可能会出现错误或卡顿。
通知 & 公告

暂时还没有消息