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

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

相关推荐
木风未来1 小时前
破解家政维修信任困局:一个小程序如何让家庭服务更透明、更高效
小程序
Java.慈祥3 小时前
速通-微信小程序 5Day
java·微信小程序·小程序·npm
说私域3 小时前
数字围城下的防御与突围:基于私域流量与智能名片商城小程序的用户关系重构研究
小程序·重构·流量运营·私域运营
晋阳十二夜3 小时前
txt文件中去掉空行的简单操作
notepad++
未来之窗软件服务4 小时前
服务器运维(三十四)小程序web访问慢ssl优化—东方仙盟
运维·服务器·小程序·仙盟创梦ide·东方仙盟
全栈小54 小时前
【小程序】微信小程序slice方法分割无效,单独输出一直为空,这是为什么呢
微信小程序·小程序·数组分割
Light604 小时前
心智有效性测试小程序 V1.0 产品白皮书——心智结构量化与系统稳定性评估平台
小程序·apache
予你@。5 小时前
uni-app(Vue3)实现自定义 Tab 切换滑块效果(微信小程序)
vue.js·微信小程序·uni-app
说私域5 小时前
数字商超的崛起:基于“链动2+1模式智能名片S2B2C商城小程序”的社交电商生态重构
人工智能·小程序·重构·流量运营
低代码布道师5 小时前
【教培管家】后台订单管理与预约确认
低代码·小程序·云开发