Vue使用CSS变量实现切换主题功能
导读: Github项目地址https://github.com/JofunLiang/vue-project-themable-demo 演示地址https://jofunliang.github.io/vue-project-them...
Github项目地址https://github.com/JofunLiang/vue-project-themable-demo
演示地址https://jofunliang.github.io/vue-project-themable-demo/
可行性测试
为了检验方法的可行性,在public文件夹下新建一个themes文件夹,并在themes文件夹新建一个default.css文件:
:root{
--color:red;
}
推荐学习:CSS视频教程
在public文件夹的index.html文件中引入外部样式theme.css,如下:
vue-skin-peeler-demo
We'resorrybutvue-skin-peeler-demodoesn'tworkproperlywithoutJavaScriptenabled.Pleaseenableittocontinue.
然后,在Home.vue中使用CSS变量:
变红色
exportdefault{
name:'home'
}
.demo{
color:var(--color);
}
然后,运行项目并在浏览器中打开页面,页面显示效果正常。
注意:@vue/cli使用link标签引入css样式可能报错"We'resorrybutvue-skin-peeler-demodoesn'tworkproperlywithoutJavaScriptenabled.Pleaseenableittocontinue."。这是因为@vue/cli将src目录下的文件都通过webpack打包所引起,所以,静态文件资源要放在public(如果是@vue/cli2.x版本放在static)文件夹下。
实现主题切换
这里主题切换的思路是替换link标签的href属性,因此,需要写一个替换函数,在src目录下新建themes.js文件,代码如下:
//themes.js
constcreateLink=(()=>
{
let$link=null
return()=>
{
if($link){
return$link
}
$link=document.createElement('link')
$link.rel='stylesheet'
$link.type='text/css'
document.querySelector('head').appendChild($link)
return$link
}
}
)()
/**
*主题切换函数
*@param{
string}
theme-主题名称,默认default
*@return{
string}
主题名称
*/
consttoggleTheme=(theme='default')=>
{
const$link=createLink()
$link.href=`./themes/${
theme}
.css`
returntheme
}
exportdefaulttoggleTheme
然后,在themes文件下创建default.css和dark.css两个主题文件。创建CSS变量,实现主题化。CSS变量实现主题切换请参考另一篇文章初次接触css变量
兼容性
IE浏览器以及一些旧版浏览器不支持CSS变量,因此,需要使用css-vars-ponyfill,是一个ponyfill,可在旧版和现代浏览器中为CSS自定义属性(也称为"CSS变量")提供客户端支持。由于要开启watch监听,所以还有安装MutationObserver.js。
安装:
npminstallcss-vars-ponyfillmutationobserver-shim--save
然后,在themes.js文件中引入并使用:
//themes.js
import'mutationobserver-shim'
importcssVarsfrom'css-vars-ponyfill'
cssVars({
watch:true
}
)
constcreateLink=(()=>
{
let$link=null
return()=>
{
if($link){
return$link
}
$link=document.createElement('link')
$link.rel='stylesheet'
$link.type='text/css'
document.querySelector('head').appendChild($link)
return$link
}
}
)()
/**
*主题切换函数
*@param{
string}
theme-主题名称,默认default
*@return{
string}
主题名称
*/
consttoggleTheme=(theme='default')=>
{
const$link=createLink()
$link.href=`./themes/${
theme}
.css`
returntheme
}
exportdefaulttoggleTheme
开启watch后,在IE11浏览器点击切换主题开关不起作用。因此,每次切换主题时都重新执行cssVars(),还是无法切换主题,原因是开启watch后重新执行cssVars()是无效的。最后,只能先关闭watch再重新开启。成功切换主题的themes.js代码如下:
//themes.js
import'mutationobserver-shim'
importcssVarsfrom'css-vars-ponyfill'
constcreateLink=(()=>
{
let$link=null
return()=>
{
if($link){
return$link
}
$link=document.createElement('link')
$link.rel='stylesheet'
$link.type='text/css'
document.querySelector('head').appendChild($link)
return$link
}
}
)()
/**
*主题切换函数
*@param{
string}
theme-主题名称,默认default
*@return{
string}
主题名称
*/
consttoggleTheme=(theme='default')=>
{
const$link=createLink()
$link.href=`./themes/${
theme}
.css`
cssVars({
watch:false
}
)
setTimeout(function(){
cssVars({
watch:true
}
)
}
,0)
returntheme
}
exportdefaulttoggleTheme
查看所有代码,请移步Github项目地址。
记住主题
实现记住主题这个功能,一是可以向服务器保存主题,一是使用本地存储主题。为了方便,这里主要使用本地存储主题的方式,即使用localStorage存储主题。具体实现请移步Github项目地址。本文转载自中文网
声明:本文内容由网友自发贡献,本站不承担相应法律责任。对本内容有异议或投诉,请联系2913721942#qq.com核实处理,我们将尽快回复您,谢谢合作!
若转载请注明出处: Vue使用CSS变量实现切换主题功能
本文地址: https://pptw.com/jishu/664898.html
