微信小程序background-image不能直接指定本地图片

微信小程序background-image不能直接指定本地图片

 

 

1

background-image: url("../../img/welcome.png");

 

使用本地图片指定背景的时候,系统会报错,如图所示

解决方法

如果出现这样的问题,我们有两种方法解决这个问题

方法一,使用base64编码

这种方法使用起来比较简单,但是图片的base64编码比较长,会很影响我们的代码结构,闹心

 

 

1

background-image: url("data:image/jpg;base64,");

 

可以直接采用这个网址进行转换:http://imgbase64.duoshitong.com/

方法二,使用image控件,设置src属性

我们使用第二种,这种需要用到CSS层叠样式

position:relative和position:absolute设置对象属性为可定位(可重叠)

 

 

1

2

3

4

5

6

7

8

<!--index.wxml-->

<view class="relative">

  <image class="pic_background" src="../../img/pic_background.png"></image>

 

  <view class="container">

    .....

  </view>

</view>

 

 

 

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19

20

21

22

.relative{

  height: 100%;

  width:100%;

  position:relative;

}

 

.pic_background{

  height: 100%;

  width:100%;

  position:absolute;

}

 

.container {

  height: 100%;

  width:100%;

  flex-direction: column;

  color:#222222;

  font-size: 30rpx;

  position: absolute;

  font-weight:900;

  font-family: "幼圆","方正姚体",Geneva,"Times New Roman";

}

转载于:https://my.oschina.net/u/2436852/blog/2251869

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值