uniapp微信小程序css中background-image失效问题

项目场景:

提示:这里简述项目相关背景:

在用uniapp做微信小程序的时候,需要一张背景图,用的是当时做app的时候的框架,但是,在class的样式中background-image失效了,查了后才知道,微信小程序不支持在class中使用background-image,但是可以在标签中使用,而且需要图片路径是base64编码格式的。


解决方案:

提示:这里填写该问题的具体解决方案:

1:图片转换base64编码的工具

javascript 复制代码
npm i image-tools --save

//引入使用
import { pathToBase64, base64ToPath } from 'image-tools'

2:使用函数进行转换

javascript 复制代码
//this.imgUrl 图片路径 /static/img/bg1.png
pathToBase64(this.imgUrl).then(res=>{
    this.bgImgBase64=res
})

3:在标签中使用

html 复制代码
//width height 要设置 
/*
background-attachment 属性值说明
background-attachment 属性有三个主要的可选值,每个值对应不同的效果:
scroll:默认值,背景图像会随着页面内容滚动。
fixed:背景图像固定在视窗中,不会随页面滚动。
local:背景图像与元素内容一起滚动,但仅限于该元素的滚动区域内。
*
/

<view 
	:style="{
		width:'100%',
		height:'100%',
		backgroundImage:'url('+bgImgBase64+')',
		backgroundRepeat: 'no-repeat',
		backgroundPosition: 'center',
		backgroundSize: '100% 100%',
		backgroundAttachment: 'fixed',
	}"
	class="index-content">
......
</view>

4:效果图

相关推荐
球球pick小樱花16 小时前
游戏官网前端工具库:海内外案例解析
前端·javascript·css
Mr_li16 小时前
给 Vue 开发者的 uni-app 快速指南
vue.js·uni-app
anyup19 小时前
🔥2026最推荐的跨平台方案:H5/小程序/App/鸿蒙,一套代码搞定
前端·uni-app·harmonyos
icebreaker20 小时前
Weapp-vite:原生模式之外,多一种 Vue SFC 选择
前端·vue.js·微信小程序
icebreaker20 小时前
重走 Vue 长征路 Weapp-vite:编译链路与 Wevu 运行时原理拆解
前端·vue.js·微信小程序
AAA阿giao2 天前
从零构建一个现代登录页:深入解析 Tailwind CSS + Vite + Lucide React 的完整技术栈
前端·css·react.js
Mintopia2 天前
Vue3 项目如何迁移到 uni-app x:从纯 Web 到多端应用的系统指南
uni-app
Mintopia2 天前
uni-app x 发展前景技术分析:跨端统一的新阶段?
uni-app
掘金安东尼2 天前
用 CSS 打造完美的饼图
前端·css
掘金安东尼2 天前
纯 CSS 实现弹性文字效果
前端·css