当前位置: 首页 > article >正文

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

api部分

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

data存储json数据

data:{rainJson:{}
}

onLoad方法获取json数据

onLoad(options) {let that = thisgetRainInfo().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事件不触发

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

http://www.lryc.cn/news/2383989.html

相关文章:

  • Wireshark 抓包工具使用
  • 大语言模型(LLM)本身是无状态的,怎么固化记忆
  • JUC入门(六)
  • std::chrono类的简单使用实例及分析
  • Git命令汇总(自用,持续更新update 5/23)
  • window xampp apache使用腾讯云ssl证书配置https
  • MATLAB求解二元一次方程组基础教程
  • Android13 wifi设置国家码详解
  • 逆向音乐APP:Python爬虫获取音乐榜单 (1)
  • JVM 垃圾回收器
  • Java合并两个列表到目标列表,并且进行排序
  • Spring AI Alibaba集成阿里云百炼大模型应用
  • 22. 用例依赖装饰器的实现思路和方法
  • 支持向量存储:PostgresSQL及pgvector扩展详细安装步骤!老工程接入RAG功能必备!
  • 【部署】如何离线环境创建docker容器执行python命令行程序
  • idea常用配置 properties中文输出乱码
  • 【Bluedroid】蓝牙 HID Host connect全流程源码解析
  • day1 大模型学习 Qwen系列学习
  • Unity3D仿星露谷物语开发47之砍树时落叶特效
  • 第十节第六部分:常见API:DateTimeFormatter、Period、Duration
  • 如何在VSCode中更换默认浏览器:完整指南
  • B2160 病人排队
  • 【机器人】复现 3D-Mem 具身探索和推理 | 3D场景记忆 CVPR 2025
  • 鸿蒙进阶——CMakelist、GN语法简介及三方库通用移植指南
  • CSS-5.1 Transition 过渡
  • TTS:VITS-fast-fine-tuning 快速微调 VITS
  • 从虚拟仿真到行业实训再到具身智能--华清远见嵌入式物联网人工智能全链路教学方案
  • 告别手动绘图!2分钟用 AI 生成波士顿矩阵
  • GraphPad Prism工作表的管理
  • UE 材质几个输出向量节点