一、使用CSS的prefers-color-scheme
只需要简单的跟随系统深色/浅色模式,不涉及用户自定义设置,那么可以采用该方法:
// style.css
// 深色模式
@media (prefers-color-scheme: dark) {
html,
body {
background: #000;
color:#fff;
}
}
// 浅色模式
@media (prefers-color-scheme: light) {
html,
body {
background: #fff;
color:#000;
}
}
// 无色系偏好模式
@media (prefers-color-scheme: no-preference) {
html,
body {
background: #fff;
color:#000;
}
}
二、通过给html添加属性,自定义主题
1.初始化主题:首先检查浏览器缓存的主题模式,若不存在则跟随当前系统的模式,将获取到的模式赋值给html的’data-theme’属性,并且赋值给变量currentTheme以便模板中展示对应模式的图标。
2.点击图标或按钮切换主题:通过获取html的’data-theme’属性,利用三元表达式进行切换。
// test.vue
<script setup>
import * as Icon from '@/assets/images/icon/config.js';
import { ref, onMounted } from 'vue';
const currentTheme = ref('light');
function initTheme() {
const savedTheme = localStorage.getItem('theme');
const systemTheme = window.matchMedia('(prefers-color-scheme: dark)').matches ? 'dark' : 'light';
const theme = savedTheme || systemTheme;
document.documentElement.setAttribute('data-theme', theme);
currentTheme.value = theme;
}
function toggleTheme() {
const html = document.documentElement;
const isDark = html.getAttribute('data-theme') === 'dark';
const newTheme = isDark ? 'light' : 'dark';
html.setAttribute('data-theme', newTheme);
localStorage.setItem('theme', newTheme);
currentTheme.value = newTheme;
}
onMounted(() => {
initTheme();
});
</script>
<template>
<div class="page">
<img :src="currentTheme == 'dark' ? Icon.moon : Icon.sun" alt="" class="icon-theme" :title="currentTheme == 'dark' ? '切换至浅色模式' : '切换至深色模式'" @click="toggleTheme" />
<p>
Lorem ipsum dolor sit amet consectetur adipisicing elit. Doloribus dolorum cupiditate unde sapiente, molestiae ratione doloremque vel aut totam perferendis quasi voluptate iusto error
impedit ut non quia vitae? Necessitatibus?
</p>
</div>
</template>
<style lang="scss" scoped>
.page {
height: 100%;
width: 100%;
padding: 30px;
background: var(--my-background-color);
color: var(--my-text-color);
.icon-theme {
width: 20px;
height: 20px;
margin-bottom: 30px;
cursor: pointer;
}
}
</style>
3.自定义变量:新增variables.scss文件存储需要修改的UI框架变量或者自定义颜色变量
/* 浅色主题 ( 默认主题 ) */
:root {
--my-background-color: #ffffff;
--my-background-color-2: rgba(255, 255, 255, 0.8);
--my-background-color-3: rgba(0, 0, 0, 0.2);
--my-text-color: #222222;
}
/* 暗黑主题 */
[data-theme='dark'] {
--my-background-color: #000000 !important;
--my-background-color-2: #1a1a1a !important;
--my-background-color-3: #3a3a3c !important;
--my-text-color: #f5f5f5d6 !important;
}
// 滚动条
*::-webkit-scrollbar {
width: 5px;
height: 5px;
}
*::-webkit-scrollbar-thumb {
border-radius: 5px;
box-shadow: inset 0 0 5px rgba(0, 0, 0, 0.1);
background: var(--my-background-color-3);
}
*::-webkit-scrollbar-track {
box-shadow: none;
border-radius: 0;
background: var(--my-background-color-2);
}
在main.js中引入
import '@/assets/variables.scss';
启动项目后即可看到效果:

4.闪屏优化:处于深色模式时,刷新页面会出现白色闪烁,这是由于页面还没加载完JS逻辑前,浏览器默认先显示白色背景导致的。可以将test.vue中初始化逻辑提取到index.html的<head>标签中,让浏览器还没加载Vue应用之前就执行深色模式
// test.vue
function initTheme() {
const theme = document.documentElement.getAttribute('data-theme');
currentTheme.value = theme;
}
// index.html
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="utf-8" />
<meta http-equiv="X-UA-Compatible" content="IE=edge" />
<meta name="viewport" content="width=device-width,initial-scale=1.0" />
<link rel="shortcut icon" type="image/svg+xml" href="favicon.svg" />
<title>Demo</title>
<script>
const savedTheme = localStorage.getItem('theme');
const systemTheme = window.matchMedia('(prefers-color-scheme: dark)').matches ? 'dark' : 'light';
const theme = savedTheme || systemTheme;
document.documentElement.setAttribute('data-theme', theme);
</script>
<style>
[data-theme='dark'] {
background: #1a1a1a;
}
</style>
</head>
<body>
<div id="app"></div>
<script type="module" src="/src/main.js"></script>
</body>
</html>