微信小程序中,解决lottie动画在真机不显示的问题

api部分

export function getRainInfo() {
  return onlineRequest({
    url: '/ball/recruit/getRainInfo',
    method: 'get'
  });
}

data存储json数据

data:{
	rainJson:{}
}

onLoad方法获取json数据

onLoad(options) {
  let that = this
  getRainInfo().then((res)=>{
    that.setData({
      rainJson:res
    })
  })
}

initLottie动画方法

initLottie(url, type) {
  // 1. 销毁旧动画
  if (this.anim) {
    this.anim = null;
  }

  // 2. 更新显示状态
  this.setData({
    rainShow: type === 'rain',
    snowShow: type === 'snow'
  }, () => {
    // 3. 在setData回调中确保DOM已更新
    const selector = type === 'rain' ? '#lottie-animation-rain' : '#lottie-animation-snow';
    // wx.showToast({ title: selector });

    wx.createSelectorQuery()
      .in(this)
      .select(selector)
      .node()
      .exec((res) => {
        if (!res[0] || !res[0].node) {
          console.error('未找到Canvas节点,选择器:', selector);
          return;
        }
        const canvas = res[0].node;
        const ctx = canvas.getContext('2d');
        // 清空画布
        ctx.clearRect(0, 0, canvas.width, canvas.height);
        canvas.width = canvas.width; // 强制重置画布
        // 加载新动画
        this.anim = lottie.loadAnimation({
          loop: true,
          autoplay: true,
          // path:url,  //注释这个,这个在真机不会显示!
          animationData: this.data.rainJson, //必须使用animationData,从后端返回json数据
          rendererSettings: {
            context: ctx
          }
        });
      });
  });
}

注意了!

path:url, 这个在真机不会显示!

animationData: this.data.rainJson, 必须使用animationData,从后端返回json数据

后端部分,把json文件放到resource里面

在这里插入图片描述

通过getRainInfo接口返回

 @GetMapping("/getRainInfo")
 public String getRainInfo() throws IOException {
     // 读取JSON文件
     return readJsonFile("rain.json");
 }

如果到这里还不显示,那么就是你们页面的层级有问题,把动画页面设置成z-index:999999最大

<view style="z-index: 9999999;">
  <canvas id="lottie-animation-rain" hidden="{{!rainShow}}" type="2d" style="position: fixed;top:0;left:0;width: 100%;height: {{margintop+140}}px;z-index: 9999999;pointer-events: none"></canvas>
  <canvas id="lottie-animation-snow" hidden="{{!snowShow}}" type="2d" style="position: fixed;top:0;left:0;width: 100%;height: {{margintop+140}}px;z-index: 9999999;pointer-events: none"></canvas>
</view>

pointer-events: none主要是防止其他view事件不触发

完成上面步骤真机就可以显示出来了!

### 微信小程序中使用Lottie动画 #### 插件安装与配置 为了在微信小程序中集成Lottie动画,需先下载并安装`lottie-miniprogram`插件。通过微信开发者工具完成此操作,在项目设置里找到第三方服务选项,添加指定名称的插件[^2]。 #### 创建Canvas组件 成功引入插件之后,下一步是在页面布局文件(`wxml`)内定义一个用于承载动画效果的<canvas>标签,并赋予其唯一标识符(id),以便后续脚本调用时能够准确定位到该元素。 ```html <!-- index.wxml --> <view class="container"> <!-- Canvas element for displaying Lottie animations --> <canvas type="2d" id="myCanvas"></canvas> </view> ``` #### 加载JSON格式的动画数据 接着处理动画资源本身——通常由设计师提供或自行创作保存为`.json`格式的数据包。确保这些文件放置于合适路径下方便程序读取;如果遇到兼容性问题,则可以考虑借助在线平台转换成适配版本[^3]。 #### 初始化及控制动画播放 最后一步涉及编写JavaScript逻辑来初始化Lottie实例并与UI控件关联起来。这包括设定初始参数、监听事件以及响应用户的交互行为等。以下是简化版代码片段: ```javascript //index.js const lottie = require('path/to/lottie.min'); Page({ onReady:function(){ const query = wx.createSelectorQuery(); query.select('#myCanvas').fields({node:true,size:true}).exec((res)=>{ let canvas=res[0].node; var animation = lottie.loadAnimation({ container: canvas, // the dom element that will contain the animation renderer: 'canvas', loop: true, autoplay: true, path: '/images/data.json' // url to your json file or object with JSON data. }); }) } }) ``` 上述方法实现了基本功能演示,实际开发过程中可能还需要针对同场景调整细节部分以满足具体需求[^1]。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

令人作呕的溏心蛋

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值