uni微信小程序使用lottie

  1. 在uni插件市场找到
    lottie-uni
    https://ext.dcloud.net.cn/plugin?id=1044
  2. 按照文档要求安装
  3. HBuilderX 引入
    下载或导入示例获取插件
cpp 复制代码
 import lottie from '@/common/lottie-miniprogram.js'

index.vue

cpp 复制代码
<template>
  <uni-popup
    ref="popup"
    type="center"
    background-color="#fff"
    :mask-click="false"
  >
    <view class="content">
      <view style="text-align: center">
        <canvas
          id="c1"
          type="2d"
          style="display: inline-block; width: 200px; height: 200px"
        ></canvas>
        <button @click="init" style="width: 300px; margin-top: 10px">
          init
        </button>
        <button @click="play" style="width: 300px; margin-top: 10px">
          play
        </button>
        <button @click="pause" style="width: 300px; margin-top: 10px">
          pause
        </button>
      </view>
    </view>
  </uni-popup>
</template>

<script>
// https://app.lottiefiles.com/animation/8606532e-798d-419e-9dc5-8abe1cc7f7d6?panel=download
// https://ext.dcloud.net.cn/plugin?id=1044
// https://github.com/airbnb/lottie-web

import lottie from "./common/lottie-miniprogram.js";
export default {
  data() {
    return {
      title: "Hello",
      _inited: true,
    };
  },
  onLoad() {},
  mounted() {
    this.$refs.popup.open();
    setTimeout(() => {
      this.init();
    }, 2000);
  },
  methods: {
    init() {
      if (this._inited) {
        return;
      }
      // https://developers.weixin.qq.com/miniprogram/dev/api/wxml/wx.createSelectorQuery.html
      // https://developers.weixin.qq.com/miniprogram/dev/api/wxml/NodesRef.fields.html
      // node是否返回节点对应的 Node 实例  size是否返回节点尺寸(width height)
      this.createSelectorQuery()
        .select("#c1")
        .fields({ node: true, size: true })
        .exec((res) => {
          const canvas = res[0].node;
          const ctx = canvas.getContext("2d");
          // 设备像素比
          const dpr = wx.getSystemInfoSync().pixelRatio;
          canvas.width = res[0].width * dpr;
          canvas.height = res[0].height * dpr;
          ctx.scale(dpr, dpr);
          lottie.setup(canvas);
          lottie.loadAnimation({
            loop: true,
            autoplay: true,
            animationData: require("./data.json"),
            // path: 'https://www.lottiejs.com/wp-content/uploads/2022/01/83351-taking-the-duggy-out.json',
            rendererSettings: {
              context: ctx,
            },
          });
        });
    },
    play() {
      this.ani.play();
    },
    pause() {
      this.ani.pause();
    },
  },
};
</script>

<style>
</style>

按照官方文档中的示例引入没有问题 但是设计给的json文件对于我这边来讲没有加载出来 所以自己找网站转化成lottie能接受的json文件

  1. 将设计给的json转成lottieFiles
    LottieFiles

  2. 放在对应的文件夹下面

在对应的地方引入该组件即可显示

相关推荐
HappyAcmen5 小时前
关于微信小程序的面试题及其解析
微信小程序·小程序·notepad++
乔冠宇5 小时前
微信小程序修改个人信息头像(uniapp开发)
微信小程序·小程序·uni-app
lvbb666 小时前
微信小程序-路线规划功能
微信小程序·小程序·notepad++
爱上大树的小猪8 小时前
微信小程序模仿快播标签云滚动特效
微信小程序·小程序
從南走到北13 小时前
挪车小程序挪车二维码php+uniapp
微信小程序·小程序·开源·微信公众平台
黑云压城After13 小时前
uniapp小程序自定义日历(签到、补签功能)
小程序·uni-app
黑云压城After14 小时前
小程序(物流、快递),接入GPS北斗获取路线以及当前车辆位置
小程序
万岳科技程序员小金15 小时前
互联网医院系统源码解析:如何开发智能化的电子处方小程序?
小程序·app开发·互联网医院系统源码·智慧医疗小程序·医院app
Java Fans16 小时前
微信小程序——访问服务器媒体文件的实现步骤
服务器·微信小程序·小程序
Evaporator Core19 小时前
微信小程序数据绑定与事件处理:打造动态交互体验
微信小程序·小程序·交互