uniapp换皮肤功能demo

本文介绍了如何在uniapp中实现换肤功能,通过创建项目目录结构,编写style.scss文件,定义样式值,并在App.vue中引入全局背景图片。利用uni-page-wrapper实现全屏背景,举例说明了将css转换为scss并提取动态变化值的方法。

不支持scss的需要提前安装:https://www.runoob.com/sass/sass-install.html

全代码下载地址

1.新建一个uni-app的默认模板
在这里插入图片描述
2.构建如下图的项目目录结构,uni.scss是默认模板中自带的
在这里插入图片描述

3.写style.scss文件

/*
@mixin 指令允许我们定义一个可以在整个样式表中重复使用的样式。
*/
@mixin bg_color($color) { //更换背景颜色
	background-color: $color ;
	[data-theme = "theme1"] & {
		background-color: $background-color-theme1 !important;
	}
	[data-theme = "theme2"] & {
		background-color: $background-color-theme2 !important;
	}		
}
 
 @mixin bg_img($image) { //更换背景图片
 	background-image: $image ;
 	[data-theme = "theme1"] & {
 		background-image: url($background-image-theme1)!important;
 	}
 	[data-theme = "theme2"] & {
 		background-image: url($background-image-theme2)!important;
 	}
 }
  
 
@mixin ft_color($color) { //更换文字颜色
	color: $color;
	[data-theme = "theme1"] & {
		color: $text-color-theme1 !important;
	}
	[data-theme = "theme2"] & {
		color: $text-color-theme2 !important;
	}	
}

@mixin login_content($content) { //更换image标签内容
	content: $content;
	[data-theme = "theme1"] & {
		content:url($text-content-theme1) !important;
	}
	[data-theme = "theme2"] & {
		content: url($text-content-theme2) !important;
	}	
}

4.在uni.scss文件中定义样式的具体值

@import './scss/style.scss'; //引入自定义的混入样式scss文件
// 为变量赋值
$background-color-theme1:#007aff;
$background-color-theme2:#D6C3A3;

$background-image-theme1:'static/bg1.png';
$background-image-theme2:'static/bg2.png';

$text-color-theme1:#ffffff;
$text-color-theme2:#535532;

$text-content-theme1:'/static/logo.png';
$text-content-theme2:'/static/logo2.png';

5.在App.vue中引入全局背景图片,设置theme1中的背景图片为默认背景

<script>
	export default {
		onLaunch: function() {
		//setAttribute() 方法添加指定的属性,并为其赋指定的值;设置data-theme
			window.document.documentElement.setAttribute('data-theme', 'theme1');
		},
		onShow: function() {
			console.log('App Show')
		},
		onHide: function() {
			console.log('App Hide')
		}
	}
</script>

<style>
	/*每个页面公共css */ 
</style>
<style lang="scss">
	// page{
	// 	@include bg_img(theme)
	// }
	
	// @include 指令可以将混入(mixin)引入到文档中。
	uni-page-wrapper{
		@include bg_img(theme)
	}
</style>

不用page标签而用uni-page-wrapper标签的原因是,page标签表示的是<uni-page-body>,背景图片只能显示在content中而不能全屏
在这里插入图片描述
6.修改index.vue

<template>
	<view class="content">
		<image class="logo"></image>
		<view class="text-area">
			<text class="title">{{title}}</text>
		</view>
		<view class="">
			
		</view>
		<view>
			<button @tap="doSkinme('theme1')">皮肤1</button>
			<button @tap="doSkinme('theme2')">皮肤2</button>
		</view>
	</view>
</template>

<script>
	export default {
		data() {
			return {
				title: 'Hello',
			}
		},
		onLoad() {

		},
		methods: {
			doSkinme(theme){
				window.document.documentElement.setAttribute('data-theme', theme);
			}
		}
	}
</script>

<style>
	.content {
		display: flex;
		flex-direction: column;
		align-items: center;
		justify-content: center;
	}
	.logo {
		height: 200rpx;
		width: 200rpx;
		margin-top: 200rpx;
		margin-left: auto;
		margin-right: auto;
		margin-bottom: 50rpx;
	}
	.text-area {
		display: flex;
		justify-content: center;
	}

	.title {
		font-size: 36rpx;
	}
</style>
<style lang="scss">
	.logo {
		@include login_content(theme)
	}
	.title {
		@include bg_color(theme)
		@include ft_color(theme)
	}
</style>

怎样将css样式修改为scss样式,以<image class="logo"></image>为例,将需要动态变化的值提取出来:

修改前
========文件index.vue=============
<style>
.logo {
		height: 200rpx;
		width: 200rpx;
		margin-top: 200rpx;
		margin-left: auto;
		margin-right: auto;
		margin-bottom: 50rpx;
		content: url(../../static/bg1.png);
	}
</style>

*********************************

修改后
========文件index.vue=============
<style>
.logo {
		height: 200rpx;
		width: 200rpx;
		margin-top: 200rpx;
		margin-left: auto;
		margin-right: auto;
		margin-bottom: 50rpx;
	}
</style>
<style lang="scss">
	.logo {
		@include login_content(theme)
	}
</style>

========文件style.scss=============
@mixin login_content($content) { //更换image标签内容
	content: $content;
	[data-theme = "theme1"] & {
		content:url($text-content-theme1) !important;
	}
	[data-theme = "theme2"] & {
		content: url($text-content-theme2) !important;
	}	
}

========文件uni.scss=============
@import './scss/style.scss'; //引入自定义的混入样式scss文件
// 为变量赋值
$text-content-theme1:'/static/logo.png';
$text-content-theme2:'/static/logo2.png';

效果预览:
在这里插入图片描述

评论 9
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值