前端主题切换的多种方式

动态link标签加载不同主题css

**原理:**提前准备好几套CSS主题样式文件,在点击切换主题时,创建link标签动态加载到head标签中,或者是动态改变link标签的href属性。
缺点:

动态加载样式文件,如果文件过大网络情况不佳的情况下可能会有加载延迟,导致样式切换不流畅

维护不便!

**优点:**实现了按需加载,提高了首屏加载时的性能且兼容性好

  1. 先创建主题css文件dark.csslight.css
  2. html中link引入
html 复制代码
<head>
	<link rel="stylesheet" href="./css/dark.css" >
	<link rel="stylesheet" href="./css/light.css" disable>
</head>
  1. 切换主题
typescript 复制代码
const themesCssSheet = [
	document.querySelector("link[data-theme='light']"),
	document.querySelector("link[data-theme='dark']"),
]
btn.addEventlistner("click",()=>{
	const dataTheme = btn.getAttribute("data-theme")
	themesCssSheet.forEach(theme=>{
		t.disable = theme.dataset.theme === dataTheme 
	})
})

提前引入不同主题的css样式,通过切换类名切换主题

  1. 定义好css样式
css 复制代码
/* light样式主题 */
body{
  color: #f90;
  background: #fff;
  --text-color:black
  --header-bg:orange
}
/* dark样式主题 */
.dark {
  color: #eee;
  background: #333;
  --text-color:#fff
  --header-bg:blue
}
  1. 点击切换主题时 js切换
typescript 复制代码
document.body.classList.toggle("dark")

css预编译器

scss 复制代码
$themes:(
	light:(
		textColor:black
		headerBg:orange
	),
	dark:(
		textColor:#fff
		headerBg:blue
	)
)
$currentTheme:light;
@mixin changeTheme(){
	@each $key,$value in $themes{
		$currentTheme:$key !global;
		html[data-theme='#{$key}'] & {
			@content;
		}
	}
}

@function getTextColor(){
	$currentThemeObj: map-get($themes,$curTheme);
	@return map-get($currentThemeObj,"textColor");
}
@function getHeaderBg(){
	$currentThemeObj: map-get($themes,$curTheme);
	@return map-get($currentThemeObj,"headerBg");
}

@function getThemeValue($key){
	$currentThemeObj: map-get($themes,$curTheme);
	@return map-get($currentThemeObj,$key);
}
.content{
	width:100%;
	height:100%;
	@changeTheme{
		textColor:getTextColor();
		headerBg:getHeaderBg();
	}
}

基于CSS变量(最优方式)

原理 :根据html元素标签的属性切换所使用的主题变量。
缺点 :IE兼容性不行(基本可以忽略)。
优点:便于维护,基本无需css参与

  1. 定义两套(多主体多套)变量
css 复制代码
:root{
	--text-color:black
	--header-bg:orange
	...
}
// 暗色主题
html[data-theme='dark']{
	--text-color:#fff
	--header-bg:blue
	...
}
  1. 在main.js项目入口文件导入该变量,便于组件使用
  2. 组件中主题相关的直接使用变量
  3. 主题切换时
typescript 复制代码
type Theme = "dark" | "light"
export const changeTheme = (theme:Theme )=>{
	document.documentElement.dataset.theme = theme
}

CSS变量+动态setProperty

  1. 定义变量
css 复制代码
:root{
	--text-color:black
	--header-bg:orange
	...
}
  1. 定义js更改属性值方法
typescript 复制代码
export const setCssVar = (prop: string, val: any, dom = document.documentElement) => {
  dom.style.setProperty(prop, val)
}
// 当样式改变
setCssVar('--text-color', color)

总结

还有其他方式(例如vue3 v-bind css,变量和类名结合),但是总体都大同小异。

相关推荐
新加坡内哥谈技术2 分钟前
口哨声、歌声、boing声和biotwang声:用AI识别鲸鱼叫声
人工智能·自然语言处理
wx74085132613 分钟前
小琳AI课堂:机器学习
人工智能·机器学习
FL162386312921 分钟前
[数据集][目标检测]车油口挡板开关闭合检测数据集VOC+YOLO格式138张2类别
人工智能·yolo·目标检测
YesPMP平台官方23 分钟前
AI+教育|拥抱AI智能科技,让课堂更生动高效
人工智能·科技·ai·数据分析·软件开发·教育
FL16238631291 小时前
AI健身体能测试之基于paddlehub实现引体向上计数个数统计
人工智能
黑客-雨1 小时前
构建你的AI职业生涯:从基础知识到专业实践的路线图
人工智能·产品经理·ai大模型·ai产品经理·大模型学习·大模型入门·大模型教程
子午1 小时前
动物识别系统Python+卷积神经网络算法+TensorFlow+人工智能+图像识别+计算机毕业设计项目
人工智能·python·cnn
大耳朵爱学习1 小时前
掌握Transformer之注意力为什么有效
人工智能·深度学习·自然语言处理·大模型·llm·transformer·大语言模型
TAICHIFEI1 小时前
目标检测-数据集
人工智能·目标检测·目标跟踪
qq_15321452641 小时前
【2023工业异常检测文献】SimpleNet
图像处理·人工智能·深度学习·神经网络·机器学习·计算机视觉·视觉检测