微信小程序中,解决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事件不触发

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

相关推荐
fakaifa9 小时前
点大餐饮独立版系统源码v1.0.3+uniapp前端+搭建教程
小程序·uni-app·php·源码下载·点大餐饮·扫码点单
Dignity_呱14 小时前
如何在不发版时,实现小程序的 AB 测试?
前端·面试·微信小程序
说私域15 小时前
基于开源 AI 大模型 AI 智能名片 S2B2C 商城小程序视角下的企业组织能力建设与破圈升级
人工智能·小程序
fakaifa1 天前
【最新版】CRMEB Pro版v3.4系统源码全开源+PC端+uniapp前端+搭建教程
人工智能·小程序·uni-app·php·crmeb·源码下载·crmebpro
2501_915918411 天前
iOS 应用上架全流程实践,从开发内测到正式发布的多工具组合方案
android·ios·小程序·https·uni-app·iphone·webview
上海云盾第一敬业销售1 天前
小程序被爬虫攻击,使用waf能防护吗?
爬虫·小程序
suncentwl1 天前
做一个答题pk小程序多少钱?
小程序·答题小程序·知识竞赛·答题pk软件
说私域2 天前
基于开源链动2+1模式AI智能名片S2B2C商城小程序的流量转化策略研究
人工智能·小程序
咸虾米_2 天前
微信小程序通过uni.chooseLocation打开地图选择位置,相关设置及可能出现的问题
微信小程序·小程序·uniapp开发·小程序地图api
未来之窗软件服务2 天前
蔬菜批发小程序:生产商的数字化转型利器——仙盟创梦IDE
小程序·自动化·仙盟创梦ide·东方仙盟·蔬菜批发·批发系统