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

1.实现效果

2.实现原理 

  • .伪元素
  • .css3动画
  • .transform

3.实现代码

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

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);
}
}
<!--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>
  • 0
    点赞
  • 4
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
要自定义微信小程序loading组件显示载入动画方法,可以按以下步骤进行: 1. 创建一个新的组件:首先,在小程序项目中创建一个新的组件,在组件目录下新建一个文件夹,例如命名为loading,在该文件夹下创建两个文件:loading.wxml和loading.wxss,用于编写组件的结构和样式。 2. 编写组件的结构:在loading.wxml文件中,可以使用合适的标签和样式来构建想要的载入动画效果,例如可以使用一个旋转的loading图标或者是一个逐帧动画来展示载入过程。 3. 定义组件的样式:在loading.wxss文件中,可以编写组件的样式代码,为载入动画提供合适的样式,包括大小、颜色、位置等,并通过class或id来选择组件的样式。 4. 在页面中引用组件:在需要显示载入动画的页面中,引入刚刚创建的loading组件,在页面的json文件中配置该组件的路径以及需要传递给组件的数据。 5. 设置组件的显示和隐藏:在页面的js文件中,通过设置数据来控制loading组件的显示和隐藏状态。可以通过设置组件的data中的某个属性为true或者false来控制组件的显示和隐藏。 6. 调用组件:在需要进行数据加载的时候,通过调用loading组件的显示方法来显示载入动画,例如在数据请求的开始和结束的地方分别调用。 通过上述步骤,就可以自定义微信小程序loading组件显示载入动画方法。可以根据实际需求和设计要求来创建和设计组件的结构和样式,并通过设置组件的显示和隐藏状态来控制显示载入动画的时机。

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值