微信小程序实现一些炫酷的loading动画

1.实现效果

https://images.gitee.com/uploads/images/2021/0826/160929_902bf951_8576727.png

2.实现原理

  • .伪元素
  • .css3动画
  • .transform
  • .box-shadow

3.实现代码

从上到下,从左到右依次的代码如下

<!--pages/subPack/loading/index.wxml-->
<view class="title">样式一:</view>
<view class="ring">
  <view>sss</view>
  <view class="line"></view>
</view>

<view class="title">样式二:</view>
<view class="box">
  <view class="loading">
    <view></view>
    <view></view>
    <view></view>
    <view></view>
    <view></view>
    <view></view>
    <view></view>
    <view></view>
  </view>
</view>
<view class="title">样式三:</view>
<view class="box">
  <view class="circle-line">
    <text></text>
    <text></text>
    <text></text>
    <text></text>
    <text></text>
    <text></text>
    <text></text>
    <text></text>
  </view>
</view>
<view class="title">样式四:</view>
<view class="box">
  <view class="circle-line1">
    <text></text>
    <text></text>
    <text></text>
    <text></text>
    <text></text>
    <text></text>
    <text></text>
    <text></text>
  </view>
</view>
<view class="title">样式五:</view>
<view class="box">
  <view class="circle-line-spin">
    <text></text>
    <text></text>
    <text></text>
    <text></text>
    <text></text>
    <text></text>
    <text></text>
    <text></text>
  </view>
</view>
<view class="title">样式六:</view>
<view class="box">
  <view class="circle-line2">
    <text></text>
    <text></text>
    <text></text>
    <text></text>
    <text></text>
    <text></text>
    <text></text>
    <text></text>
  </view>
</view>
<view class="title">样式七:</view>
<view class="box">
  <view class="circle_bx">
    <view></view>
  </view>
</view>
<view class="title">样式8:</view>
<view class="box">
  <view class="loader-dots">
    <view></view>
    <view></view>
    <view></view>
    <view></view>
    <view></view>
  </view>
</view>
<view class="title">样式9:</view>
<view class="box">
  <view class="loader-rainbow">
    <view class="loader-inner">
      <view class="loader-line-wrap">
        <view class="loader-line"></view>
      </view>
      <view class="loader-line-wrap">
        <view class="loader-line"></view>
      </view>
      <view class="loader-line-wrap">
        <view class="loader-line"></view>
      </view>
      <view class="loader-line-wrap">
        <view class="loader-line"></view>
      </view>
      <view class="loader-line-wrap">
        <view class="loader-line"></view>
      </view>
    </view>
  </view>
</view>

<view class="title">样式10:</view>
<view class="box">
  <view class="loader-line">
    <view class="bar"></view>
    <view class="bar"></view>
    <view class="bar"></view>
    <view class="bar"></view>
    <view class="bar"></view>
    <view class="ball"></view>
  </view>
</view>
<view class="title">样式11:(不是css写的,是一张gif)</view>
<view class="box">
  <view class="wrapper">
    <view class="rubik-loader"></view>
  </view>
</view>
<view class="title">样式12:</view>
<view class="box">
  <view>
    <view class="stick"></view>
    <view class="stick"></view>
    <view class="stick"></view>
    <view class="stick"></view>
    <view class="stick"></view>
    <view class="stick"></view>
    <view class="loading_text">Loading...</view>
  </view>
</view>
<view class="title">样式13:</view>
<view class="box1">
  <view class="tan-loader">
    <view class="binary"></view>
    <view class="binary"></view>
    <view class="getting-there">LOADING...</view>
  </view>
</view>

<view class="title">样式14:</view>
<view class="box">
  <view class="css-heart"></view>
</view>
<view class="title">样式15:</view>
<view class="box">
  <view class="circle-spin">
    <text>加载中</text>
  </view>
</view>
<view class="title">样式16:</view>
<view class="box">
  <view class="circle-add">
    <text class="a1"></text>
    <text class="a1"></text>
    <text class="a1"></text>
    <text class="a1"></text>
    <text class="a1"></text>
    <text class="a1"></text>
    <text class="a1"></text>
    <text class="a1"></text>
  </view>
</view>
<view class="title">样式17:</view>
<view class="load1">
  <view class="rect1"></view>
  <view class="rect2"></view>
  <view class="rect3"></view>
  <view class="rect4"></view>
  <view class="rect5"></view>
</view>

<view class="title">样式18:</view>
<view class="load1-circle">
  <view class="rect1"></view>
  <view class="rect2"></view>
  <view class="rect3"></view>
  <view class="rect4"></view>
  <view class="rect5"></view>
</view>
page {
  background: #fff;
  padding-bottom: 100px;
  /* background: #000;
  background: radial-gradient(#222, #000); */
}
.title{
  margin: 20rpx ;
  font-size: 25rpx;
  text-align: center;
  font-weight: bold;
}
.box{
  /* width: 100px; */
  height: 100px;
  display: flex;
  align-items: center;
  justify-content: center;
  margin: 0 auto;
  overflow: unset;
}
/* 第一个 */
.ring {
  position: relative;
  margin: 40rpx auto;
  width: 80px; 
  height: 80px; 
  text-align: center; /* 字体水平居中 */
  line-height: 80px; 
  font-size: 20px; 
  letter-spacing: 4px; 
  background: transparent; 
  border: 2px solid #3c3c3c; 
  border-radius: 50%; 
  box-shadow: 0 0 20 rgba(0, 0, 0, .5); 
  user-select: none; /* 无法选中 */
}
.ring::before {
  content: ''; /* 内容 */
  position: absolute;
  z-index: 99;
  top: -3px;
  left: -3px;
  width: 100%;
  height: 100%;
  border: 3px solid transparent;
  border-top: 3px solid #b3205d;
  border-right: 3px solid #771940;
  border-radius: 50%;
  animation: animateCircle 2s linear infinite; /* 动画:名称 时间 速率 重复 */
}
.line{
  display: block;
  position: absolute;
  top: calc(50% - 2px);
  left: 50%;
  width: 50%;
  height: 4px;
  background: transparent;
  transform-origin: left; /* 动画开始位置 */
  animation: animate 2s linear infinite;
  transform: rotate(45deg);
}
.line::before {
  content: '';
  position: absolute;
  width: 8px;
  height: 8px;
  border-radius: 50%;
  background: #b3205d;
  top: -4px;
  right: -4px;
  box-shadow: 0 0 20px #b3205d;
}
@keyframes animate {
  100% {
    /* 360+45 */
    transform: rotate(405deg); 
  }
}
@keyframes animateCircle {
  0% {
    transform: rotate(0deg);
  }
  100% {
    transform: rotate(360deg);
  }
}

/* 第二个 */

.loading{
  position: relative;
}
.loading>view{
  position: absolute;
  width: 4px;
  height: 20px;
  border-radius: 10px;
  background-color: #b3205d;
}
.loading>view:nth-child(1){
  top: 20px;
  left: 0px;
  animation: loading infinite 1s;
}
.loading>view:nth-child(2){
  top: 14.1442px;
  left: 14.1442px;
  transform: rotate(-45deg);
  animation: loading infinite 1s 0.125s;
}
.loading>view:nth-child(3){
  top: 0px;
  left: 20px;
  transform: rotate(90deg);
  animation: loading infinite 1s 0.25s;
}
.loading>view:nth-child(4){
  top: -14.1442px;
  left: 14.1442px;
  transform: rotate(45deg);
  animation: loading infinite 1s 0.375s;
}
.loading>view:nth-child(5){
  top: -20px;
  left: 0px;
  transform: rotate(0deg);
  animation: loading infinite 1s 0.5s;
}
.loading>view:nth-child(6){
  top: -14.1442px;
  left: -14.1442px;
  transform: rotate(-45deg);
  animation: loading infinite 1s 0.625s;
}
.loading>view:nth-child(7){
  top: 0px;
  left: -20px;
  transform: rotate(90deg);
  animation: loading infinite 1s 0.75s;
}
.loading>view:nth-child(8){
  top: 14.1442px;
  left: -14.1442px;
  transform: rotate(45deg);
  animation: loading infinite 1s 0.875s;
}

@keyframes loading {
  50% {
    opacity: 0.1;
  }
  100% {
    opacity: 1;
  }
}
/* 第三个 */
.circle-line,.circle-line1,.circle-add{
  width: 100px;
  height: 100px;
  display: inline-block;
  position: relative;
}
.circle-line text{
  display: block;
  width: 40px;
  height: 40px;
  border-radius: 50%;
  opacity: .7;
  position: absolute;
  top: calc(50% - 20px);
  left: calc(50% - 40px);
  background-color: #a8c992;
  transform-origin: center right; 
  animation: circle 1.5s linear infinite; 
}

.circle-line text:nth-child(1){
  transform: rotate(0deg);
  animation-delay: 0.2s;
}
.circle-line text:nth-child(2){
  transform: rotate(45deg);
  animation-delay: 0.4s;
}
.circle-line text:nth-child(3){
  transform: rotate(90deg);
  animation-delay: 0.6s;
}
.circle-line text:nth-child(4){
  transform: rotate(135deg);
  animation-delay: 0.8s;
}
.circle-line text:nth-child(5){
  transform: rotate(180deg);
  animation-delay: 1s;
}
.circle-line text:nth-child(6){
  transform: rotate(225deg);
  animation-delay: 1.2s;
}
.circle-line text:nth-child(7){
  transform: rotate(270deg);
  animation-delay: 1.4s;
}
.circle-line text:nth-child(8){
  transform: rotate(315deg);
  animation-delay: 1.6s;
}
@keyframes circle {
  0%{
      opacity: 0.45;
  }
  100%{
      opacity: 0.9;
  }
}
/* 第四个 */
.circle-line1 text{
  display: block;
  width: 40px;
  height: 50px;
  border-radius: 50%;
  position: absolute;
  top: calc(50% - 25px);
  left: calc(50% - 40px);
  transform-origin: center right; 
  animation: circle1 1.5s linear infinite; 

}
.circle-line1 text::before{
  content: '';
  display: block;
  width: 8px;
  height: 8px;
  border-radius: 50%;
  position: absolute;
  top: 0;
  right: 10px;
  background-color: #a8c992;
}
.circle-line1 text:nth-child(1){
  transform: rotate(0deg);
  animation-delay: 0.2s;
}
.circle-line1 text:nth-child(2){
  transform: rotate(45deg);
  animation-delay: 0.4s;
}
.circle-line1 text:nth-child(3){
  transform: rotate(90deg);
  animation-delay: 0.6s;
}
.circle-line1 text:nth-child(4){
  transform: rotate(135deg);
  animation-delay: 0.8s;
}
.circle-line1 text:nth-child(5){
  transform: rotate(180deg);
  animation-delay: 1s;
}
.circle-line1 text:nth-child(6){
  transform: rotate(225deg);
  animation-delay: 1.2s;
}
.circle-line1 text:nth-child(7){
  transform: rotate(270deg);
  animation-delay: 1.4s;
}
.circle-line1 text:nth-child(8){
  transform: rotate(315deg);
  animation-delay: 1.6s;
}
@keyframes circle1 {
  0%{
      opacity: 0.15;
  }
  100%{
      opacity: 0.9;
  }
}

/* 第五个 */
.circle-line-spin{
  position: relative;
  animation: circle-line 1.5s linear infinite;  
}
.circle-line-spin text{
  display: block;
  width: 50%;
  height: 5px;
  opacity: .7;
  position: absolute;
  top: calc(50% - 2.5px);
  left: 0px;
  transform-origin: center right;
}
.circle-line-spin text::before{
  content: '';
  display: block;
  width: 15px;
  height: 5px;
  position: absolute;
  top: 0;
  right: 10px;
  background-color: #a8c992;
}
.circle-line-spin text:nth-child(1){
  transform: rotate(0deg);
  animation-delay: 0.2s;
}
.circle-line-spin text:nth-child(2){
  transform: rotate(45deg);
  animation-delay: 0.4s;
}
.circle-line-spin text:nth-child(3){
  transform: rotate(90deg);
  animation-delay: 0.6s;
}
.circle-line-spin text:nth-child(4){
  transform: rotate(135deg);
  animation-delay: 0.8s;
}
.circle-line-spin text:nth-child(5){
  transform: rotate(180deg);
  animation-delay: 1s;
}
.circle-line-spin text:nth-child(6){
  transform: rotate(225deg);
  animation-delay: 1.2s;
}
.circle-line-spin text:nth-child(7){
  transform: rotate(270deg);
  animation-delay: 1.4s;
}
.circle-line-spin text:nth-child(8){
  transform: rotate(315deg);
  animation-delay: 1.6s;
}
@keyframes circle-line {
  0%{
    transform:rotate(-360deg);
  }
  100%{
    transform:rotate(10deg);
  }
}
/* 第六个 */
.circle-line2{
  position: relative;
}
.circle-line2 text{
  display: block;
  width: 40px;
  height: 50px;
  border-radius: 50%;
  position: absolute;
  top: calc(50% - 25px);
  left: calc(50% - 40px);
  transform-origin: center right; 
  animation: circle2 1.5s linear infinite; 
}
.circle-line2 text::before{
  content: '';
  display: block;
  width: 8px;
  height: 8px;
  border-radius: 50%;
  position: absolute;
  top: 0;
  right: 10px;
  background-color: #22a2c2;
}
.circle-line2 text:nth-child(1){
  transform: rotate(0deg);
  animation-delay: 0.2s;
}
.circle-line2 text:nth-child(2){
  transform: rotate(45deg);
  animation-delay: 0.4s;
}
.circle-line2 text:nth-child(3){
  transform: rotate(90deg);
  animation-delay: 0.6s;
}
.circle-line2 text:nth-child(4){
  transform: rotate(135deg);
  animation-delay: 0.8s;
}
.circle-line2 text:nth-child(5){
  transform: rotate(180deg);
  animation-delay: 1s;
}
.circle-line2 text:nth-child(6){
  transform: rotate(225deg);
  animation-delay: 1.2s;
}
.circle-line2 text:nth-child(7){
  transform: rotate(270deg);
  animation-delay: 1.4s;
}
.circle-line2 text:nth-child(8){
  transform: rotate(315deg);
  animation-delay: 1.6s;
}
@keyframes circle2 {
  0%{
    opacity: 0.15;
    transform:rotate(-360deg);
  }
  100%{
    opacity: 0.9;
    transform:rotate(360deg);
  }
}
/* 样式七 */
.circle_bx{
  position: relative;
}
.circle_bx view{
  display: inline-block;
  position: relative;
  border-radius: 50%;
  border-top: 48px #22a2c2 solid;
  border-bottom: 48px #22a2c2 solid;
  border-left: 48px #22a2c2 solid;
  border-right: 48px transparent solid;
  color: #22a2c2;
  animation: circle_bx  1s linear infinite ;
}
/* box-shadow: h-shadow v-shadow blur spread color inset; */
@keyframes circle_bx {
  0% {
    box-shadow: 120px 0px 0 -40px rgba(55, 145, 197, 0.5), 
    100px 0px 0 -40px rgba(62, 96, 119, 0.75), 
    80px 0px 0 -40px rgb(24, 149, 221);
  }
  100% {
    box-shadow: 100px 0px 0 -45px rgba(9, 150, 206, 0.5), 
    80px 0px 0 -45px rgba(84, 100, 117, 0.75), 
    60px 0px 0 -45px rgb(8, 78, 90);
  }
}
.circle_bx view::before {
  position: absolute;
  border-radius: 50%;
  content: '';
  top: -48px;
  left: -48px;
  border-top: 48px #22a2c2 solid;
  border-bottom: 48px transparent solid;
  border-left: 48px transparent solid;
  border-right: 48px transparent solid;
  animation: ccc .5s linear infinite alternate;
}
@keyframes ccc {
  0% {
      transform: rotate(45deg)
  }
  100% {
      transform: rotate(0deg)
  }
}
.circle_bx view::after {
  position: absolute;
  content: '';
  top: -48px;
  left: -48px;
  border-top: 48px transparent solid;
  border-bottom: 48px #22a2c2 solid;
  border-left: 48px transparent solid;
  border-right: 48px transparent solid;
  border-radius: 50%;
  animation: bbb .5s linear infinite alternate;
}
@keyframes bbb {
  0% {
      transform: rotate(-45deg)
  }
  100% {
      transform: rotate(0deg)
  }
} 

/* di8个 */
.loader-dots view{
  width: 10px;
  height: 10px;
  background: #3ac;
  border-radius: 50%;
  display:inline-block;
  animation: slide 1s infinite;
}
.loader-dots view:nth-child(1){
  animation-delay:.1s;
}
.loader-dots view:nth-child(2){
  animation-delay:.2s;
}
.loader-dots view:nth-child(3){
  animation-delay:.3s;
}
.loader-dots view:nth-child(4){
  animation-delay:.4s;
  background: linear-gradient(#9403b8,#3ac)
}
.loader-dots view:nth-child(5){
  animation-delay:.5s;
  background:radial-gradient(#6d9b93,#3ac)
}
@keyframes slide{
  0%{
    transform: scale(1)
  }
  50%
  {
    opacity:0.3;
    transform:scale(2);
  }
  100%{
    transform: scale(1)
  }
}
/* 9个 */
.loader-rainbow{
  position: relative;
  width:100px;
  height: 100px;
}
.loader-inner {
  bottom: 0;
  height: 100px;
  left: 0;
  margin: auto;
  position: absolute;
  right: 0;
  top: 0;
  width: 100px;
}

.loader-line-wrap {
  animation: 
  spin 2000ms cubic-bezier(.175, .885, .32, 1.275) infinite
;
  box-sizing: border-box;
  height: 50px;
  left: 0;
  overflow: hidden;
  position: absolute;
  top: 0;
  transform-origin: 50% 100%;
  width: 100px;
}
.loader-line {
  border: 4px solid transparent;
  border-radius: 100%;
  box-sizing: border-box;
  height: 100px;
  left: 0;
  margin: 0 auto;
  position: absolute;
  right: 0;
  top: 0;
  width: 100px;
}
.loader-line-wrap:nth-child(1) { animation-delay: -50ms; }
.loader-line-wrap:nth-child(2) { animation-delay: -100ms; }
.loader-line-wrap:nth-child(3) { animation-delay: -150ms; }
.loader-line-wrap:nth-child(4) { animation-delay: -200ms; }
.loader-line-wrap:nth-child(5) { animation-delay: -250ms; }

.loader-line-wrap:nth-child(1) .loader-line {
  border-color: hsl(0, 80%, 60%);
  height: 90px;
  width: 90px;
  top: 7px;
}
.loader-line-wrap:nth-child(2) .loader-line {
  border-color: hsl(60, 80%, 60%);
  height: 76px;
  width: 76px;
  top: 14px;
}
.loader-line-wrap:nth-child(3) .loader-line {
  border-color: hsl(120, 80%, 60%);
  height: 62px;
  width: 62px;
  top: 21px;
}
.loader-line-wrap:nth-child(4) .loader-line {
  border-color: hsl(180, 80%, 60%);
  height: 48px;
  width: 48px;
  top: 28px;
}
.loader-line-wrap:nth-child(5) .loader-line {
  border-color: hsl(240, 80%, 60%);
  height: 34px;
  width: 34px;
  top: 35px;
}

@keyframes spin {
  0%, 15% {
  transform: rotate(0);
}
100% {
  transform: rotate(360deg);
}
}

4.更多小程序demo,关注苏苏的码云!如果对你有帮助,烦请star+订阅~,更多资讯,尽在公众号,苏苏的bug!

最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
  • 序言:七十年代末,一起剥皮案震惊了整个滨河市,随后出现的几起案子,更是在滨河造成了极大的恐慌,老刑警刘岩,带你破解...
    沈念sama阅读 199,519评论 5 468
  • 序言:滨河连续发生了三起死亡事件,死亡现场离奇诡异,居然都是意外死亡,警方通过查阅死者的电脑和手机,发现死者居然都...
    沈念sama阅读 83,842评论 2 376
  • 文/潘晓璐 我一进店门,熙熙楼的掌柜王于贵愁眉苦脸地迎上来,“玉大人,你说我怎么就摊上这事。” “怎么了?”我有些...
    开封第一讲书人阅读 146,544评论 0 330
  • 文/不坏的土叔 我叫张陵,是天一观的道长。 经常有香客问我,道长,这世上最难降的妖魔是什么? 我笑而不...
    开封第一讲书人阅读 53,742评论 1 271
  • 正文 为了忘掉前任,我火速办了婚礼,结果婚礼上,老公的妹妹穿的比我还像新娘。我一直安慰自己,他们只是感情好,可当我...
    茶点故事阅读 62,646评论 5 359
  • 文/花漫 我一把揭开白布。 她就那样静静地躺着,像睡着了一般。 火红的嫁衣衬着肌肤如雪。 梳的纹丝不乱的头发上,一...
    开封第一讲书人阅读 48,027评论 1 275
  • 那天,我揣着相机与录音,去河边找鬼。 笑死,一个胖子当着我的面吹牛,可吹牛的内容都是我干的。 我是一名探鬼主播,决...
    沈念sama阅读 37,513评论 3 390
  • 文/苍兰香墨 我猛地睁开眼,长吁一口气:“原来是场噩梦啊……” “哼!你这毒妇竟也来了?” 一声冷哼从身侧响起,我...
    开封第一讲书人阅读 36,169评论 0 254
  • 序言:老挝万荣一对情侣失踪,失踪者是张志新(化名)和其女友刘颖,没想到半个月后,有当地人在树林里发现了一具尸体,经...
    沈念sama阅读 40,324评论 1 294
  • 正文 独居荒郊野岭守林人离奇死亡,尸身上长有42处带血的脓包…… 初始之章·张勋 以下内容为张勋视角 年9月15日...
    茶点故事阅读 35,268评论 2 317
  • 正文 我和宋清朗相恋三年,在试婚纱的时候发现自己被绿了。 大学时的朋友给我发了我未婚夫和他白月光在一起吃饭的照片。...
    茶点故事阅读 37,299评论 1 329
  • 序言:一个原本活蹦乱跳的男人离奇死亡,死状恐怖,灵堂内的尸体忽然破棺而出,到底是诈尸还是另有隐情,我是刑警宁泽,带...
    沈念sama阅读 32,996评论 3 315
  • 正文 年R本政府宣布,位于F岛的核电站,受9级特大地震影响,放射性物质发生泄漏。R本人自食恶果不足惜,却给世界环境...
    茶点故事阅读 38,591评论 3 303
  • 文/蒙蒙 一、第九天 我趴在偏房一处隐蔽的房顶上张望。 院中可真热闹,春花似锦、人声如沸。这庄子的主人今日做“春日...
    开封第一讲书人阅读 29,667评论 0 19
  • 文/苍兰香墨 我抬头看了看天上的太阳。三九已至,却和暖如春,着一层夹袄步出监牢的瞬间,已是汗流浃背。 一阵脚步声响...
    开封第一讲书人阅读 30,911评论 1 255
  • 我被黑心中介骗来泰国打工, 没想到刚下飞机就差点儿被人妖公主榨干…… 1. 我叫王不留,地道东北人。 一个月前我还...
    沈念sama阅读 42,288评论 2 345
  • 正文 我出身青楼,却偏偏与公主长得像,于是被迫代替她去往敌国和亲。 传闻我的和亲对象是个残疾皇子,可洞房花烛夜当晚...
    茶点故事阅读 41,871评论 2 341

推荐阅读更多精彩内容