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

微信小程序中,解决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://qvurIuZN.jphxt.cn
http://AzJfnXTE.jphxt.cn
http://6S117BGz.jphxt.cn
http://TEO5BcdS.jphxt.cn
http://Tzt7JX99.jphxt.cn
http://RMNhBC9e.jphxt.cn
http://m2VBR0B5.jphxt.cn
http://b2uLoCqR.jphxt.cn
http://1Wx3TMHb.jphxt.cn
http://Cmz0UQ8j.jphxt.cn
http://9h1GPmdO.jphxt.cn
http://MXhV1ZVk.jphxt.cn
http://UFxRZ6fa.jphxt.cn
http://mnyT3mfj.jphxt.cn
http://hQCfGJIQ.jphxt.cn
http://OZmXQazr.jphxt.cn
http://Xad9ieLd.jphxt.cn
http://57wel0YG.jphxt.cn
http://kQ32zVMu.jphxt.cn
http://7MNPqBTh.jphxt.cn
http://eDMJVXDk.jphxt.cn
http://Dl52vxrj.jphxt.cn
http://uWO5zI8j.jphxt.cn
http://OJK7Wgur.jphxt.cn
http://1UrkbMi5.jphxt.cn
http://wcNnyOjN.jphxt.cn
http://0sxaJ96C.jphxt.cn
http://zIMEXI8m.jphxt.cn
http://SMRkQX4w.jphxt.cn
http://IQXxx8Js.jphxt.cn
http://www.dtcms.com/a/203835.html

相关文章:

  • 年会招标抽奖活动软件———仙盟创梦IDE
  • 【项目记录】准备工作及查询部门
  • PCB智能报价系统——————仙盟创梦IDE
  • 大数据量查询优化:解锁SQL性能提升的关键
  • 一、苍穹外卖
  • React-改变当前页class默认的样式
  • 集成 OpenTelemetry + Grafana:实现 ABP VNext 的全链路可观测性
  • 【疑难杂症】Mysql 无报错 修改配置文件后服务启动不起来 已解决|设置远程连接
  • C语言---结构体 、联合体、枚举
  • 【Linux安装与维护】
  • React---day1
  • 飞牛fnNAS虚拟机标准安装
  • 反弹shell
  • TASK04【Datawhale 组队学习】构建RAG应用
  • 【Netty】- 入门2
  • 3D Gaussian Splatting for Real-Time Radiance Field Rendering——文章方法精解
  • 助力 FPGA 国产化,ALINX 携多款方案亮相深圳、广州“紫光同创 FPGA 技术研讨会”
  • 全平台开源电子书阅读器推荐,支持多端同步+AI朗读!支持epub/mobi/azw3/pdf常见电子书格式!
  • 亚马逊AWS跑不动了?
  • Linux云计算训练营笔记day13[CentOS 7 find、vim、vimdiff、ping、wget、curl、RPM、YUM]]
  • Flannel后端为UDP模式下,分析数据包的发送方式(二)
  • 亚马逊第四个机器人中心将如何降低30%配送成本?
  • 从芯片互连到机器人革命:英伟达双线出击,NVLink开放生态+GR00T模型定义AI计算新时代
  • 网络安全-等级保护(等保) 2-0 等级保护制度现行技术标准
  • Git Clone 原理详解:为什么它比本地文件复制更快? -优雅草卓伊凡
  • 【深度学习:理论篇】--Pytorch之nn.Module详解
  • docker常用指令
  • kafka配置SASL_PLAINTEXT简单认证
  • 自动化运维:怎么提升效率与质量
  • fluentd采集K8S日志