自定义loading效果时使用less时用loop循环函数设置animation-delay动画延迟失效的问题

文章讲述了在用Less编写loading动画时遇到的问题,即设置的animation-delay在循环中无效,作者尝试通过调整animation-delay的计算方式来实现小球交替出现的loading效果。通过定义Less循环函数,计算每个小球的动画延迟时间,以达到预期的视觉效果。
摘要由CSDN通过智能技术生成

记录一个小问题 自定义一个loading效果 使用loop循环demo

使用less 自定义动画loading时设置的循环函数时animation-delay无效,想设置出循环时后一位循环数比前一位迟一点触发来展示loading效果交替的感觉

定义lessn内部变量

@ballSize:10px;//小球尺寸
@containerSize:150px;//容器尺寸
@n :36;//小球数量
@pdeg:360deg / @n;//每一次旋转的角度
@ani-duration :2s;//

定义循环函数 循环loading需要的小球 并设置小球循环后的位置

注意:此时循环函数内部的主体:如果.dot的样式写的时候包裹在.loading里 循环时要带上父级class 否则无效!!!

(此时我的.dot放在了.loading里面,所以我必须两边对应上)

 

 

//循环函数
.loop(@i) when(@i <= @n){
   .loading .dot:nth-child(@{i}){
       transform:rotate( @pdeg * @i ) translateY(-@containerSize / 2);
       //设置动画延迟
        &::before,
        &::after{
             animation-delay: -@ani-duration / @n * @i *6 ; //动画延迟时间 动画时间除以小球数量 乘每轮延迟数量 = 小球的间隔时间乘第几个小球
         }
    }
    .loop(@i+1)
}
.loop(1);

loading为外层盒子  .dot为内部小球盒子 设置好动画给到内部小球

.loading {
    width: @containerSize;
    height: @containerSize;
    margin: 50px auto;
    position: relative;
    border-radius: 50%;
    perspective: 70px;
    transform-style:preserve-3d ;
    // border: 1px solid #fff;

    .dot{
         position: absolute;
         left: 50%;
         right: 50%;
         width: @ballSize;
         height: @ballSize;
         margin-left: -@ballSize / 2;
         margin-top: (@containerSize - @ballSize) / 2 ;
        //  background: red;
         &::before,&::after{
             content: '';
             position: absolute;
             width: 100%;
             height: 100%;
             border-radius: 50%;
         }
          &::before{
            background-color: #000;
            top: -100%;
            animation: moveBlack @ani-duration infinite;
            // animation-delay : 6s;

          }
          &::after{
            background-color: #fff;
            top: 100%;
            animation: moveWhite @ani-duration infinite;

          }
    }
}
//黑球运动动画
@keyframes moveBlack{
    0%{
        animation-timing-function: ease-in;//规定动画的过渡曲线 由慢变快
    }
    25%{
     transform: translate3d(0,100%,@ballSize);
     animation-timing-function: ease-out;//规定动画的过渡曲线 由快变慢
    }
    50%{
     transform: translate3d(0,200%,0);
     animation-timing-function: ease-in;
    }
    75%{
     transform: translate3d(0,100%,-@ballSize);
     animation-timing-function: ease-out;
    }
}
//白球运动动画
@keyframes moveWhite{
    0%{
        animation-timing-function: ease-in;//规定动画的过渡曲线 由慢变快
    }
    25%{
     transform: translate3d(0,-100%,-@ballSize);
     animation-timing-function: ease-out;//规定动画的过渡曲线 由快变慢
    }
    50%{
     transform: translate3d(0,-200%,0);
     animation-timing-function: ease-in;
    }
    75%{
     transform: translate3d(0,-100%,@ballSize);
     animation-timing-function: ease-out;
    }
}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值