使用序列帧实现动画

序列帧图片:https://lk-webfont.oss-cn-beijing.aliyuncs.com/web/play-qx/LKW/识别动效61张600\*600.png

这个链接有元素的宽高+张数

vue项目

动画组件 test.vue

复制代码
<template>
    <div class="frame_wrap" :style="{'--width': width+'px','--height': height+'px','--count': count, '--duration': duration+'s', '--bgSize': bgSize+'px', '--bgSize1': -bgSize+'px' }"> </div>
</template>
<script lang="ts" setup>
import { computed } from 'vue'
const props:any = defineProps({
    count: {
        type: Number,
        vlaue: 0
    },
    width: {
        type: Number,
        value: 0
    },
    height: {
        type: Number,
        value: 0
    },
    duration: {
        type: Number,
        value: 0
    }
})
const bgSize = computed(() => props.width * props.count)
</script>
<style lang="scss">
.frame_wrap {
    width: var(--width);
    height: var(--height);
    position: absolute;
    background: pink;
    -webkit-animation: frames var(--duration) steps(var(--count)) infinite;
    animation: frames var(--duration) steps(var(--count)) infinite;
    background-position: 0 0;
    background-size:  var(--bgSize) var(--height);
    background-repeat: no-repeat;
    background-image: url(https://lk-webfont.oss-cn-beijing.aliyuncs.com/web/play-qx/LKW/%E8%AF%86%E5%88%AB%E5%8A%A8%E6%95%8861%E5%BC%A0600*600.png);
}

@keyframes frames {
    from {
        background-position-x: 0;
    }

    to {
        background-position-x: var(--bgSize1);
    }
}

@-webkit-keyframes frames {
    from {
        background-position-x: 0;
    }

    to {
        background-position-x: var(--bgSize1);
    }
}
</style>

父组件调用:

<test :width="300" :height="300" :count="61" :duration="6"></test>

微信小程序开发

子组件:

复制代码
子组件:
wxml:
<view class="frame_wrap" style="background-image: url({{url}});--width:{{width}}rpx;--height:{{height}}rpx;--count:{{count}};--duration:{{duration}};"></view>

js:
Component({
  properties: {
    url: {
      type: String,
      value: ""
    },
    count: {
      type: String,
      value: 3
    },
    width:{
      type:String,
      value:360
    },
    height:{
      type:String,
      value:300
    },
    duration:{
      type: String,
      value: 0.3
    },
  },
  data: {

  },
  methods: {

  }
})
wxss:
.frame_wrap {
    width: var(--width);
    height: var(--height);
    animation: frames calc(var(--duration) * 1s) steps(var(--count)) infinite;
    background-position: 0 0;
    background-size: calc(var(--width) * var(--count)) var(--height);
  }
  
  @keyframes frames {
    from {
      background-position-x: 0;
    }
  
    to {
      background-position-x: calc(var(--width) *-1 *var(--count));
    }
  }

父组件调用:

复制代码
<css-animation wx:if="{{hasUnreadMsg == false}}" class="animation-qi" width="300" height="300" count="61" duration="6"
      url="https://lk-webfont.oss-cn-beijing.aliyuncs.com/web/play-qx/LKW/%E8%AF%86%E5%88%AB%E5%8A%A8%E6%95%8861%E5%BC%A0600*600.png"></css-animation>
相关推荐
liangshanbo12151 小时前
写好 React useEffect 的终极指南
前端·javascript·react.js
哆啦A梦15883 小时前
搜索页面布局
前端·vue.js·node.js
_院长大人_4 小时前
el-table-column show-overflow-tooltip 只能显示纯文本,无法渲染 <p> 标签
前端·javascript·vue.js
哆啦A梦15885 小时前
axios 的二次封装
前端·vue.js·node.js
阿珊和她的猫5 小时前
深入理解与手写发布订阅模式
开发语言·前端·javascript·vue.js·ecmascript·状态模式
yinuo5 小时前
一行 CSS 就能搞定!用 writing-mode 轻松实现文字竖排
前端
snow@li6 小时前
html5:拖放 / demo / 拖放事件(Drag Events)/ DataTransfer 对象方法
前端·html·拖放
浪裡遊7 小时前
Nivo图表库全面指南:配置与用法详解
前端·javascript·react.js·node.js·php
漂流瓶jz8 小时前
快速定位源码问题:SourceMap的生成/使用/文件格式与历史
前端·javascript·前端工程化
samroom8 小时前
iframe实战:跨域通信与安全隔离
前端·安全