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. 放在对应的文件夹下面

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

相关推荐
中小企业实战军师刘孙亮4 小时前
实体店的小程序转型之路:拥抱新零售的密码-中小企实战运营和营销工作室博客
职场和发展·小程序·创业创新·学习方法·业界资讯·零售·内容运营
说私域5 小时前
基于开源技术体系的品牌赛道力重构:AI智能名片与S2B2C商城小程序源码驱动的品类创新机制研究
人工智能·小程序·重构·开源·零售
王哈哈的学习笔记7 小时前
uniapp小程序使用echarts
前端·小程序·uni-app
前端太佬10 小时前
小程序登录与授权功能全解析:从原理到设计的实战指南 (2025年最新规范实践版)
前端·微信·微信小程序
说私域11 小时前
颠覆传统微商!开源AI智能名片链动2+1模式S2B2C商城小程序:重构社交电商的“降维打击”革命
人工智能·小程序·开源·零售
ai大佬12 小时前
我是如何用AI编程制作一个AI表情包生成的小程序
人工智能·小程序·ai编程·api中转·apikey
2401_8906658613 小时前
免费送源码:Java+ssm+HTML 三分糖——甜品店网站设计与实现 计算机毕业设计原创定制
java·python·微信小程序·html·php·课程设计·android-studio
Maitians13 小时前
微信小程序 - 根据经纬度打开导航
微信小程序·小程序
前端太佬14 小时前
微信小程序支付全流程实战指南(Node.js后端篇)
前端·javascript·微信小程序
xuanjiong15 小时前
微信小程序,基于uni-app的轮播图制作,调用文件中图片
微信小程序·小程序·uni-app