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

api部分

javascript 复制代码
export function getRainInfo() {
  return onlineRequest({
    url: '/ball/recruit/getRainInfo',
    method: 'get'
  });
}

data存储json数据

javascript 复制代码
data:{
	rainJson:{}
}

onLoad方法获取json数据

javascript 复制代码
onLoad(options) {
  let that = this
  getRainInfo().then((res)=>{
    that.setData({
      rainJson:res
    })
  })
}

initLottie动画方法

javascript 复制代码
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接口返回

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

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

html 复制代码
<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事件不触发

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

相关推荐
TiAmo zhang30 分钟前
微信小程序开发案例 | 个人相册小程序(上)
微信小程序·小程序
00后程序员张13 小时前
iOS 上架费用全解析 开发者账号、App 审核、工具使用与开心上架(Appuploader)免 Mac 成本优化指南
android·macos·ios·小程序·uni-app·cocoa·iphone
WenGyyyL16 小时前
微信小程序开发——第三章:WXML 与 WXSS —— 小程序页面结构与样式设计
微信小程序·小程序
万岳科技系统开发16 小时前
外卖小程序中的高并发处理:如何应对大流量订单的挑战
算法·小程序·开源
WenGyyyL17 小时前
微信小程序开发——第四章:小程序的组件与模块化开发
微信小程序·小程序·notepad++
说私域19 小时前
社群时代下的商业变革:“开源AI智能名片链动2+1模式S2B2C商城小程序”的应用与影响
人工智能·小程序·开源
毕设源码-朱学姐21 小时前
【开题答辩全过程】以 基于Java的医务室病历管理小程序为例,包含答辩的问题和答案
java·开发语言·小程序
文人sec1 天前
微信小程序minium自动化测试SOP
微信小程序·小程序
克里斯蒂亚诺更新1 天前
微信小程序 点击地图后弹出一个模态框
微信小程序·小程序·notepad++
云起SAAS1 天前
患者随访管理抖音快手微信小程序看广告流量主开源
微信小程序·小程序·ai编程·看广告变现轻·患者随访管理