动态修改主题色(uni-app、H5)

H5端(后台管理等):

1.首先使用状态管理,我这里是pinia,vuex也是一样的,新建theme.js

这里关键是使用el.style.setProperty()方法可以修改css变量的值,示例没有做持久化储存,刷新会丢失,可自行添加

import { defineStore } from 'pinia'

export const useThemeStore = defineStore('theme', {
    state: () => ({
        theme: {
            colorPrimary: '#46D52E',
            colorPrimaryActive: '#319520',
            colorPrimaryLight: '#C7F2C0'
        }
    }),
    actions: {
        setAvtiveTheme () {
            const el = document.getElementById('app')
            if (el) {
                el.style.setProperty('--color-primary', this.theme.colorPrimary)
                el.style.setProperty('--color-primary-active', this.theme.colorPrimaryActive)
                el.style.setProperty('--color-primary-light', this.theme.colorPrimaryLight)
            }
        },
        async getTheme () {
            // 只是示例,可以从后端接口获取主题颜色
            let res = await api.getTheme()
            if (res.success) {
                this.theme = res.data
                this.setAvtiveTheme()
            }
        }
    }
})

具体使用要看具体需求,如果只是浅色主题和深色主题等更换,只需要在state把主题的配置写好,用开关控制即可。示例:

state: () => ({
    theme: {
        // 深色主题
        dark: {
            // 文字颜色
            textColor: '#ffffff',
            // 背景颜色
            backgroundColor: '#000000',
            // 边框颜色
            borderColor: '#0000004d',
        },
        // 浅色主题
        light: {
            textColor: '#000000',
            backgroundColor: '#ffffff',
            borderColor: '#0000004d',
        },
        // 当前主题
        isDark: false
    }
}),
actions: {
    setAvtiveTheme (data) {
        const el = document.getElementById('app')
        if (el) {
            el.style.setProperty('--text-color', data.textColor)
            el.style.setProperty('--background-color', data.backgroundColor)
            el.style.setProperty('--border-color', data.borderColor)
        }
    },
    //修改主题色
    changeTheme() {
        this.theme.isDark = !this.theme.isDark
        if (this.theme.isDark) {
            this.setAvtiveTheme(this.theme.dark)
        } else {
            this.setAvtiveTheme(this.theme.light)
        }
    },
}

 2.在需要使用主题色的页面css使用

        .submitBtn{
            width: 344px;
            background-color: var(--color-primary);
            font-weight: 700;
            font-size: 16px;
            &:active{
                background-color: var(--color-primary-active);
            }
        }

如果是js控制显示主题色的就需要引入仓库使用

<el-switch :style="`--el-switch-on-color: ${themeStore.theme.colorPrimary}`" />

import { useThemeStore } from '@/stores/theme'
const themeStore = useThemeStore()

3.在App.vue入口文件导入并获取颜色

<script setup>
import { RouterView } from 'vue-router'
import { onMounted } from 'vue'
import { useThemeStore } from '@/stores/theme'

const themeStore = useThemeStore()

onMounted(() => {
    themeStore.setAvtiveTheme()
})
</script>

<template>
  <RouterView />
</template>

注意:此方法仅适用有ducument对象的H5端,后台管理等项目。

uniapp--小程序或app端:

由于uni-app的 js 代码,web 端运行于浏览器中。非 web 端(包含小程序和 App),Android 平台运行在 v8 引擎中,iOS 平台运行在 iOS 自带的 jscore 引擎中,都没有运行在浏览器或 webview 里,因此不支持 window、document、navigator 等浏览器专用对象。

1.这里示例用vuex演示,首先也是新建一个仓库theme.js

const state = {
	activeTheme: {
		'--color-primary': '#ffbb12',
		'--color-primary-active': '#cf9236',
		'--color-primary-light': '#C7F2C0',
	}
}

const mutations = {
	setActiveTheme (state, data) {
		state.activeTheme = data
	},
}

const actions = {
	async getTheme(context,params) {
		//模拟从接口获取数据
		let res = await api.getTheme()
		if (res.success) {
			context.commit('setActiveTheme', res.data)
		}
	},
}

export default {
  state,
  mutations,
  actions
}

2.App.vue 中在小程序启动时引入仓库并获取颜色,此步适用颜色是从后端获取,如果本地存储可不用加:

onLaunch: function() {
	// 仓库需要增加方法 getTheme 从接口获取颜色数据
	store.dispatch('theme/getTheme')
}

3.在需要用到主题颜色的页面,最外层view绑定仓库的值:

<template>
	<view class="test_comtainer" :style="[activeTheme]">
		<view class="btn" @click="confirm">确定</view>
	</view>
</template>

<script>
	import { mapState, mapMutations } from 'vuex'
	export default {
		data () {
			return {
				
			}
		},
		computed: {
			...mapState({
				activeTheme: store => store.theme.activeTheme
			})
		},
		onLoad () {
			
		},
		methods: {
			...mapMutations({
				setActiveTheme: 'theme/setActiveTheme'
			}),
			confirm () {
				this.setActiveTheme({
					'--color-primary': '#46D52E',
					'--color-primary-active': '#319520',
					'--color-primary-light': '#C7F2C0'
				})
			}
		}
	}
</script>

<style lang="scss" scoped>
	.test_comtainer {
		
		.btn{
			width: 328rpx;
			height: 80rpx;
			border-radius: 16rpx;
			display: flex;
			align-items: center;
			justify-content: center;
			font-size: 32rpx;
			line-height: 1;
			background-color: var(--color-primary);
			&:active{
				background-color: #cf9236;
			}
		}
	}
</style>

这个方法有个弊端,就是在使用到的页面,都需要绑定一下store里面的数据。比较麻烦。

uni-app 是一种基于 Vue.js 的跨平台应用框架,它能够帮助开发者通过编写一次代码即可发布到包括微信小程序、支付宝小程序、百度智能云等在内的多种移动平台上。uni-appH5播放器组件允许开发者轻松地在应用程序中嵌入音频或视频播放功能。 ### uni-app H5 播放器的基本组成部分: 1. **音频组件**: 支持基本的音量调节、暂停、继续播放、上一首、下一首等功能,并提供API供自定义播放行为。 2. **视频组件**: 提供了播放、暂停、全屏显示、上下左右滑动控制进度等功能。支持多种视频格式,如 MP4 等,并且可以添加字幕文件。 3. **音频和视频控制界面**: 自带简洁美观的播放控件设计,也可以通过配置选项定制播放器外观。 4. **事件处理**: 提供了一系列事件回调机制,便于监听用户操作,如播放完成、暂停、错误发生等。 ### 使用步骤: 1. **安装 uni-app**:首先需要下载并安装 uni-app 开发环境及配套工具。 2. **引入播放器组件**: 在项目中引入所需的音频或视频播放器组件。通常 uni-app 已内置这些组件,只需按需导入即可。 3. **创建播放器实例**: 初始化播放器,指定播放资源、是否自动播放、初始音量等参数。 4. **集成到页面中**: 将播放器组件插入到需要展示音乐或视频的页面模板中。 5. **处理用户交互**: 配置事件监听函数,以便对用户的点击、拖拽、触控等操作做出响应。 6. **调试与优化**: 测试播放器的各项功能,检查性能和兼容性,针对不同设备进行适配调整。 ### 相关问题: 1. 如何在uni-app中自定义H5播放器的样式? 2. uni-app H5播放器如何处理流式音频/视频数据? 3. 当在不同环境下运行应用时,uni-app H5播放器会遇到哪些常见的兼容性问题?
评论 4
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

整把来福

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值