小日
小日
  • 发布:2022-03-29 18:01
  • 更新:2022-03-29 18:04
  • 阅读:436

动态设置背景图,H5显示正, App端显示空白(包括安卓和ios都是)

分类:uni-app

动态设置背景图的时候,以下案例在h5环境下均可以正常展示,而app环境只有最后一种通过class的方式设置成功。

现在其实有了解决方案就是将之前动态的style改为动态的class就ok了。

但是我想不通的是为什么app环境在dom中设置背景图的方法全都失败

<template>  
  <view class="demo-content">  
    <div :style="{backgroundImage:`url(${indexBackgroundImage})`}">  
      <h1>动态设置背景</h1>  
    </div>  

    <div class="mt-40" :style="{backgroundImage:`url(${indexBackgroundImage2})`}">  
      <h1>动态设置背景2</h1>  
    </div>  

    <view  
      class="mt-40"  
      :style="{backgroundImage:'url(../../static/images/home/running/record-run.svg)'}"  
    >  
      <h1>相对路径引用背景图</h1>  
    </view>  

    <view  
      class="mt-40"  
      :style="{ backgroundImage:'url(/static/images/home/running/record-run.svg)'}"  
    >  
      <h1>绝对路径引用背景图</h1>  
    </view>  

    <view  
      class="mt-40"  
      :style="{backgroundImage:'url(../../static/images/activity/defaultBg/week_0.png'}"  
    >  
      <h1>相对路径引入png作为背景</h1>  
    </view>  

    <view  
      class="box mt-40"  
    >  
      <h1>使用class 添加背景</h1>  
    </view>  
  </view>  
</template>  

<script>  
import indexBackgroundImage from '@/static/images/home/running/record-run.svg';  
export default {  

  data() {  
    return {  
      indexBackgroundImage,  
      indexBackgroundImage2:require('../../static/images/home/running/record-run.svg')  

    };  
  },  
  computed: {  

  },  
  methods: {  

  }  
};  
</script>  

<style lang="scss" scoped>  
.demo-content {  
  padding-top: 80rpx;  
}  
.box {  
  background-image: url('../../static/images/home/running/record-run.svg');  
}  
.mt-40 {  
  margin-top: 40rpx;  
}  
</style>  
2022-03-29 18:01 负责人:无 分享
已邀请:
FullStack

FullStack - 【插件开发】【专治疑难杂症】【ios上架、马甲包、白包、过审、已成功上架过几百个】【多款插件已上架:https://ext.dcloud.net.cn/publisher?id=22130】【非诚勿扰】QQ:543610866

使用base64看看

  • 小日 (作者)

    重新尝试了一下,如果引用的图片类型为png或者base64都可以正常显示,而如果是svg类型就会导致在app中无法正常展示。

    2022-03-30 10:42

  • FullStack

    回复 小日: 那就是不支持svg

    2022-03-30 13:19

该问题目前已经被锁定, 无法添加新回复