css 关键帧实现开盲盒动图

1,149 阅读3分钟

语法:

animation: name duration timing-function delay iteration-count direction fill-mode play-state;

name:指定要绑定到选择器的关键帧的名称

duration:动画指定需要多少秒或毫秒完成

animation-timing-function:设置动画将如何完成一个周期

  • linear:动画从头到尾的速度是相同的
  • ease:默认。动画以低速开始,然后加快,在结束前变慢
  • ease-in:动画以低速开始
  • ease-out:动画以低速结束
  • ease-in-out:动画以低速开始和结束
  • steps(int,start|end):指定了时间函数中的间隔数量(步长)。有两个参数,第一个参数指定函数的间隔数,该参数是一个正整数(大于 0)。 第二个参数是可选的,表示动画是从时间段的开头连续还是末尾连续。含义分别如下:
    • start:表示直接开始。
    • end:默认值,表示戛然而止。
  • cubic-bezier(*n*,*n*,*n*,*n*):在 cubic-bezier 函数中自己的值。可能的值是从 0 到 1 的数值。

animation-delay:设置动画在启动前的延迟间隔

animation-iteration-count:定义动画的播放次数

animation-direction:指定是否应该轮流反向播放动画

  • normal:默认值。动画按正常播放
  • reverse:动画反向播放
  • alternate:动画在奇数次(1、3、5...)正向播放,在偶数次(2、4、6...)反向播放
  • alternate-reverse:动画在奇数次(1、3、5...)反向播放,在偶数次(2、4、6...)正向播放

animation-fill-mode:规定当动画不播放时(当动画完成时,或当动画有一个延迟未开始播放时),要应用到元素的样式

  • none:默认值
  • forwards:最后一帧
  • backwards:动画将应用在 animation-delay 定义期间启动动画的第一次迭代的关键帧中定义的属性值。这些都是 from 关键帧中的值(当 animation-direction 为 "normal" 或 "alternate" 时)或 to 关键帧中的值(当 animation-direction 为 "reverse" 或 "alternate-reverse" 时)。

animation-play-state:指定动画是否正在运行或已暂停 paused:指定暂停动画 running:指定正在运行的动画

<view class="animate">
    <image
        class="light"
        src="光路径"
        mode="widthFix"
    ></image>
    <image
        class="box"
        src="盒子路径"
        mode="widthFix"
    ></image>
</view>
 
.animate {
    position: absolute;
    left: 0;
    top: 0;

    .box {
        position: absolute;
        top: 50%;
        left: 50%;
        width: 280rpx;
        margin-top: -140rpx;
        margin-left: -140rpx;
        transform-origin: center;
        animation: box 3s linear;
        animation-fill-mode: forwards;
    }

    .light {
        transform-origin: center;
        animation: light 3s infinite linear;
        animation-fill-mode: forwards;
    }
    
    // 实现盒子抖动效果
    @keyframes box {
        0% {
            -webkit-transform: rotate(0deg);
        }
        50% {
            -webkit-transform: rotate(0deg);
        }
        75% {
            -webkit-transform: rotate(-15deg);
        }
        78% {
            -webkit-transform: rotate(15deg);
        }
        82% {
            -webkit-transform: rotate(-15deg);
        }
        90% {
            -webkit-transform: rotate(0deg);
        }
        95% {
            opacity: 1;
        }
        100% {
            opacity: 0;
        }
    }
    
    // 实现背景旋转
    @keyframes light {
        0% {
            -webkit-transform: rotate(0deg);
        }
        25% {
            -webkit-transform: rotate(90deg);
        }
        50% {
            -webkit-transform: rotate(180deg);
        }
        75% {
            opacity: 1;
            -webkit-transform: rotate(270deg);
        }
        100% {
            opacity: 0;
            -webkit-transform: rotate(360deg);
        }
    }
}

image.png

<view class="animate flex-center">
    <image class="gif" :src="gif" mode="widthFix"></image>
</view>
<view class="animate-img">
    <view class="img">
        开完盲盒后的数据
    </view>
</view>

// 动图运行的时长
.animate {
    // 不占位置
    width: 100%;
    height: 100%;
    position: absolute;
    left: 0;
    top: 0;

    .gif {
        width: 90%;
        transform-origin: center;
        animation: gif 1.8s linear;
        animation-fill-mode: forwards;
    }
    
    // 动图运行完了隐藏
    @keyframes gif {
        95% {
            opacity: 1;
        }

        100% {
            opacity: 0;
        }
    }
}

// gif运行完后出现数据
.animate-img {
    .img {
        width: 100%;
        opacity: 0;
        transform-origin: center top;
        animation: img 2s alternate ease-in;
        animation-delay: 2s; // 等待gif运行后执行
        animation-fill-mode: forwards;
        padding-top: 40rpx;
        padding-bottom: 10rpx;
        border-radius: 40rpx;

        // 展示数据
        @keyframes img {
            0% {
                opacity: 0;
            }

            100% {
                opacity: 1;
            }
        }
    }
}

效果图:

image.png

参考链接:www.runoob.com/cssref/css3…

位移、缩放:blog.51cto.com/feng/528945…