<!-- 1. 顶部导航栏 -->
<nav class="navbar">
<div class="logo">MyBrand</div>
<!--
主题切换开关
aria-label 是为了无障碍访问
-->
<button id="theme-toggle" class="theme-btn" aria-label="切换主题">
<!-- 默认显示月亮图标 (代表点击进入深色模式) -->
<span class="icon">🌙</span>
<span class="text">深色模式</span>
</button>
</nav>
<!-- 2. 主要内容区域 -->
<main class="container">
<div class="hero-section">
<h1>欢迎来到我的博客</h1>
<p>这是一个演示 CSS 变量和深色模式切换的页面。</p>
</div>
<div class="card-grid">
<!-- 卡片 1 -->
<div class="card">
<h2>文章标题 1</h2>
<p>这是一些示例文本。在深色模式下,背景变黑,文字变白。</p>
<a href="#" class="link">阅读更多</a>
</div>
<!-- 卡片 2 -->
<div class="card">
<h2>文章标题 2</h2>
<p>CSS 变量让这一切变得非常简单且高效。</p>
<a href="#" class="link">阅读更多</a>
</div>
</div>
</main>
</body>
第二部分:CSS 样式 (CSS 变量的魔力)
这是本篇的核心。我们不再直接写死颜色(如 #ffffff 或 #000000),而是定义变量。
逻辑如下:
在 :root (默认状态) 下定义一套“浅色”变量。
在 [data-theme="dark"] 状态下重新定义这套变量为“深色”值。
在具体的 CSS 规则中,只使用 var(--variable-name)。
/* --- 1. 定义变量系统 --- */
/* 默认 (浅色) 主题 */
:root {
/* 背景色 */
--bg-body: #f4f7f6;
--bg-card: #ffffff;
--bg-nav: #ffffff;
/* 文本色 */
--text-primary: #333333;
--text-secondary: #666666;
/* 强调色 */
--accent-color: #007bff;
/* 边框与阴影 */
--border-color: #e1e4e8;
--shadow: 0 4px 12px rgba(0,0,0,0.08);
}
/* 深色主题 (通过 data-theme="dark" 属性激活) */
[data-theme="dark"] {
--bg-body: #1a1a1a;
--bg-card: #2d2d2d;
--bg-nav: #2d2d2d;
--text-primary: #ffffff;
--text-secondary: #b0b0b0;
--accent-color: #4dabf7; /* 深色模式下通常颜色要稍微亮一点 */
--border-color: #444444;
--shadow: 0 4px 12px rgba(0,0,0,0.3);
}
/* --- 2. 应用变量 --- */
body {
color: var(--text-primary);
font-family: -apple-system, sans-serif;
/* 核心:添加 transition 让颜色切换平滑过渡 */
transition: background-color 0.3s ease, color 0.3s ease;
margin: 0;
}
.navbar {
border-bottom: 1px solid var(--border-color);
padding: 15px 30px;
display: flex;
justify-content: space-between;
align-items: center;
transition: background-color 0.3s ease, border-color 0.3s ease;
}
.card {
border: 1px solid var(--border-color);
box-shadow: var(--shadow);
padding: 25px;
border-radius: 8px;
transition: background-color 0.3s ease, border-color 0.3s ease, box-shadow 0.3s ease;
}
.card h2 { margin-top: 0; }
.card p { color: var(--text-secondary); }
.link { color: var(--accent-color); text-decoration: none; }
/* --- 开关按钮样式 --- */
.theme-btn {
background: transparent;
border: 1px solid var(--border-color);
color: var(--text-primary);
padding: 8px 16px;
border-radius: 20px;
cursor: pointer;
display: flex;
align-items: center;
gap: 8px;
transition: all 0.3s ease;
}
.theme-btn:hover {
}
/* 布局辅助 */
.container { max-width: 800px; margin: 40px auto; padding: 0 20px; }
.card-grid { display: grid; grid-template-columns: 1fr 1fr; gap: 20px; }
第三部分:JS 交互逻辑 (LocalStorage)
JavaScript 的任务不仅仅是切换类名,更重要的是持久化用户的选择。
逻辑流程:
初始化:页面加载时,检查 localStorage。如果有保存的设置,立即应用。
切换:点击按钮时,切换 html 标签上的 data-theme 属性。
保存:将当前的状态('dark' 或 'light')写入 localStorage。
document.addEventListener('DOMContentLoaded', () => {
// 1. 获取 DOM 元素
const toggleBtn = document.getElementById('theme-toggle');
const toggleIcon = toggleBtn.querySelector('.icon');
const toggleText = toggleBtn.querySelector('.text');
// 我们将把 data-theme 属性放在 <html> 标签上
const htmlElement = document.documentElement;
// --- 辅助函数:更新按钮 UI ---
const updateButtonUI = (isDark) => {
if (isDark) {
toggleIcon.textContent = '☀';
toggleText.textContent = '浅色模式';
} else {
toggleIcon.textContent = '🌙';
toggleText.textContent = '深色模式';
}
};
// --- 2. 初始化:检查本地存储 ---
// 检查用户之前是否选过
const savedTheme = localStorage.getItem('myAppTheme');
// 如果之前保存了 'dark',则立即应用
if (savedTheme === 'dark') {
htmlElement.setAttribute('data-theme', 'dark');
updateButtonUI(true);
} else {
// 默认是浅色,无需操作 (或者也可以显式设置 'light')
updateButtonUI(false);
}
// --- 3. 绑定点击事件 ---
toggleBtn.addEventListener('click', () => {
// 检查当前是否是深色模式
const currentTheme = htmlElement.getAttribute('data-theme');
if (currentTheme === 'dark') {
// -> 切换为浅色
htmlElement.setAttribute('data-theme', 'light');
localStorage.setItem('myAppTheme', 'light'); // 保存到本地
updateButtonUI(false);
} else {
// -> 切换为深色
htmlElement.setAttribute('data-theme', 'dark');
localStorage.setItem('myAppTheme', 'dark'); // 保存到本地
updateButtonUI(true);
}
});
});
总结
恭喜!您构建了一个现代化的、具备记忆功能的深色模式系统。
https://weibo.com/ttarticle/p/show?comment=1&id=2309405298580973289482
https://weibo.com/ttarticle/p/show?id=2309405298581107245182
https://weibo.com/ttarticle/p/show?comment=1&id=2309405298581107245182
https://weibo.com/ttarticle/p/show?id=2309405298581170159988
https://weibo.com/ttarticle/p/show?comment=1&id=2309405298581170159988
https://weibo.com/ttarticle/p/show?id=2309405298581258502553
https://weibo.com/ttarticle/p/show?comment=1&id=2309405298581258502553
https://weibo.com/ttarticle/p/show?id=2309405298581329543277
https://weibo.com/ttarticle/p/show?comment=1&id=2309405298581329543277
https://weibo.com/ttarticle/p/show?id=2309405298581396914872
https://weibo.com/ttarticle/p/show?comment=1&id=2309405298581396914872
https://weibo.com/ttarticle/p/show?id=2309405298581472412181
https://weibo.com/ttarticle/p/show?comment=1&id=2309405298581472412181
https://weibo.com/ttarticle/p/show?id=2309405298581594046846
https://weibo.com/ttarticle/p/show?comment=1&id=2309405298581594046846
https://weibo.com/ttarticle/p/show?id=2309405298581690515477
https://weibo.com/ttarticle/p/show?comment=1&id=2309405298581690515477
https://weibo.com/ttarticle/p/show?id=2309405298581761556514
https://weibo.com/ttarticle/p/show?comment=1&id=2309405298581761556514
https://weibo.com/ttarticle/p/show?id=2309405298581832859730
https://weibo.com/ttarticle/p/show?comment=1&id=2309405298581832859730
https://weibo.com/ttarticle/p/show?id=2309405298581920940221
https://weibo.com/ttarticle/p/show?comment=1&id=2309405298581920940221
https://weibo.com/ttarticle/p/show?id=2309405298582005088549
https://weibo.com/ttarticle/p/show?comment=1&id=2309405298582005088549
https://weibo.com/ttarticle/p/show?id=2309405298582118072635
https://weibo.com/ttarticle/p/show?comment=1&id=2309405298582118072635
https://weibo.com/ttarticle/p/show?id=2309405298582193570035
https://weibo.com/ttarticle/p/show?comment=1&id=2309405298582193570035
https://weibo.com/ttarticle/p/show?id=2309405298582264872966
https://weibo.com/ttarticle/p/show?comment=1&id=2309405298582264872966
https://weibo.com/ttarticle/p/show?id=2309405298582327787908
https://weibo.com/ttarticle/p/show?comment=1&id=2309405298582327787908
https://weibo.com/ttarticle/p/show?id=2309405298582449684645
https://weibo.com/ttarticle/p/show?comment=1&id=2309405298582449684645
https://weibo.com/ttarticle/p/show?id=2309405298582562931122
https://weibo.com/ttarticle/p/show?comment=1&id=2309405298582562931122
https://weibo.com/ttarticle/p/show?id=2309405298582630039875
https://weibo.com/ttarticle/p/show?comment=1&id=2309405298582630039875
https://weibo.com/ttarticle/p/show?id=2309405298582692692169
https://weibo.com/ttarticle/p/show?comment=1&id=2309405298582692692169
https://weibo.com/ttarticle/p/show?id=2309405298582755868880
https://weibo.com/ttarticle/p/show?comment=1&id=2309405298582755868880
https://weibo.com/ttarticle/p/show?id=2309405298582814327238
https://weibo.com/ttarticle/p/show?comment=1&id=2309405298582814327238
https://weibo.com/ttarticle/p/show?id=2309405298582877241391
https://weibo.com/ttarticle/p/show?comment=1&id=2309405298582877241391
https://weibo.com/ttarticle/p/show?id=2309405298582935961979
https://weibo.com/ttarticle/p/show?comment=1&id=2309405298582935961979
https://weibo.com/ttarticle/p/show?id=2309405298583095345218
https://weibo.com/ttarticle/p/show?comment=1&id=2309405298583095345218
https://weibo.com/ttarticle/p/show?id=2309405298583175299280
https://weibo.com/ttarticle/p/show?comment=1&id=2309405298583175299280
https://weibo.com/ttarticle/p/show?id=2309405298583242145838
https://weibo.com/ttarticle/p/show?comment=1&id=2309405298583242145838
https://weibo.com/ttarticle/p/show?id=2309405298583334421119
https://weibo.com/ttarticle/p/show?comment=1&id=2309405298583334421119
https://weibo.com/ttarticle/p/show?id=2309405298583414374818
https://weibo.com/ttarticle/p/show?comment=1&id=2309405298583414374818
https://weibo.com/ttarticle/p/show?id=2309405298583506649691
https://weibo.com/ttarticle/p/show?comment=1&id=2309405298583506649691
https://weibo.com/ttarticle/p/show?id=2309405298583628022150
https://weibo.com/ttarticle/p/show?comment=1&id=2309405298583628022150
https://weibo.com/ttarticle/p/show?id=2309405298583699325186
https://weibo.com/ttarticle/p/show?comment=1&id=2309405298583699325186
https://weibo.com/ttarticle/p/show?id=2309405298584173543453
https://weibo.com/ttarticle/p/show?comment=1&id=2309405298584173543453
https://weibo.com/ttarticle/p/show?id=2309405298584299372625
https://weibo.com/ttarticle/p/show?comment=1&id=2309405298584299372625
https://weibo.com/ttarticle/p/show?id=2309405298584366481721
https://weibo.com/ttarticle/p/show?comment=1&id=2309405298584366481721
https://weibo.com/ttarticle/p/show?id=2309405298584437784582
https://weibo.com/ttarticle/p/show?comment=1&id=2309405298584437784582
https://weibo.com/ttarticle/p/show?id=2309405298584500699422
https://weibo.com/ttarticle/p/show?comment=1&id=2309405298584500699422
https://weibo.com/ttarticle/p/show?id=2309405298584567808138
https://weibo.com/ttarticle/p/show?comment=1&id=2309405298584567808138
https://weibo.com/ttarticle/p/show?id=2309405298584634916906
https://weibo.com/ttarticle/p/show?comment=1&id=2309405298584634916906
https://weibo.com/ttarticle/p/show?id=2309405298584710152522
https://weibo.com/ttarticle/p/show?comment=1&id=2309405298584710152522
https://weibo.com/ttarticle/p/show?id=2309405298584785912461
https://weibo.com/ttarticle/p/show?comment=1&id=2309405298584785912461
https://weibo.com/ttarticle/p/show?id=2309405298584857215328
https://weibo.com/ttarticle/p/show?comment=1&id=2309405298584857215328
https://weibo.com/ttarticle/p/show?id=2309405298584919867576
https://weibo.com/ttarticle/p/show?comment=1&id=2309405298584919867576
https://weibo.com/ttarticle/p/show?id=2309405298584982782684
https://weibo.com/ttarticle/p/show?comment=1&id=2309405298584982782684
https://weibo.com/ttarticle/p/show?id=2309405298585045959038
https://weibo.com/ttarticle/p/show?comment=1&id=2309405298585045959038
https://weibo.com/ttarticle/p/show?id=2309405298585112805449
https://weibo.com/ttarticle/p/show?comment=1&id=2309405298585112805449
https://weibo.com/ttarticle/p/show?id=2309405298585175982776
https://weibo.com/ttarticle/p/show?comment=1&id=2309405298585175982776
https://weibo.com/ttarticle/p/show?id=2309405298585243091194
https://weibo.com/ttarticle/p/show?comment=1&id=2309405298585243091194
https://weibo.com/ttarticle/p/show?id=2309405298585306005512
https://weibo.com/ttarticle/p/show?comment=1&id=2309405298585306005512
https://weibo.com/ttarticle/p/show?id=2309405298585611927641
https://weibo.com/ttarticle/p/show?comment=1&id=2309405298585611927641
https://weibo.com/ttarticle/p/show?id=2309405298585855197774
https://weibo.com/ttarticle/p/show?comment=1&id=2309405298585855197774
https://weibo.com/ttarticle/p/show?id=2309405298588183298620
https://weibo.com/ttarticle/p/show?comment=1&id=2309405298588183298620
https://weibo.com/ttarticle/p/show?id=2309405298588493676643
https://weibo.com/ttarticle/p/show?comment=1&id=2309405298588493676643
https://weibo.com/ttarticle/p/show?id=2309405298588632088996
https://weibo.com/ttarticle/p/show?comment=1&id=2309405298588632088996
https://weibo.com/ttarticle/p/show?id=2309405298588825026575
https://weibo.com/ttarticle/p/show?comment=1&id=2309405298588825026575
https://weibo.com/ttarticle/p/show?id=2309405298589244457418
https://weibo.com/ttarticle/p/show?comment=1&id=2309405298589244457418
https://weibo.com/ttarticle/p/show?id=2309405298589743317846
https://weibo.com/ttarticle/p/show?comment=1&id=2309405298589743317846
https://weibo.com/ttarticle/p/show?id=2309405298589877534784
https://weibo.com/ttarticle/p/show?comment=1&id=2309405298589877534784
https://weibo.com/ttarticle/p/show?id=2309405298590020403660
https://weibo.com/ttarticle/p/show?comment=1&id=2309405298590020403660
https://weibo.com/ttarticle/p/show?id=2309405298592188859010
https://weibo.com/ttarticle/p/show?comment=1&id=2309405298592188859010
https://weibo.com/ttarticle/p/show?id=2309405298592255705097
https://weibo.com/ttarticle/p/show?comment=1&id=2309405298592255705097
https://weibo.com/ttarticle/p/show?id=2309405298592461488239
https://weibo.com/ttarticle/p/show?comment=1&id=2309405298592461488239
https://weibo.com/ttarticle/p/show?id=2309405298595585982874
https://weibo.com/ttarticle/p/show?comment=1&id=2309405298595585982874
https://weibo.com/ttarticle/p/show?id=2309405298595644703475
https://weibo.com/ttarticle/p/show?comment=1&id=2309405298595644703475
https://weibo.com/ttarticle/p/show?id=2309405298595707617964
https://weibo.com/ttarticle/p/show?comment=1&id=2309405298595707617964
https://weibo.com/ttarticle/p/show?id=2309405298595770531888
https://weibo.com/ttarticle/p/show?comment=1&id=2309405298595770531888
https://weibo.com/ttarticle/p/show?id=2309405298595850485949
https://weibo.com/ttarticle/p/show?comment=1&id=2309405298595850485949
https://weibo.com/ttarticle/p/show?id=2309405298595908944338
https://weibo.com/ttarticle/p/show?comment=1&id=2309405298595908944338
https://weibo.com/ttarticle/p/show?id=2309405298595972120611
https://weibo.com/ttarticle/p/show?comment=1&id=2309405298595972120611
https://weibo.com/ttarticle/p/show?id=2309405298596039229519
https://weibo.com/ttarticle/p/show?comment=1&id=2309405298596039229519
https://weibo.com/ttarticle/p/show?id=2309405298596110271043
https://weibo.com/ttarticle/p/show?comment=1&id=2309405298596110271043
https://weibo.com/ttarticle/p/show?id=2309405298596215128784
https://weibo.com/ttarticle/p/show?comment=1&id=2309405298596215128784
https://weibo.com/ttarticle/p/show?id=2309405298596290888740
https://weibo.com/ttarticle/p/show?comment=1&id=2309405298596290888740
https://weibo.com/ttarticle/p/show?id=2309405298596488020524
https://weibo.com/ttarticle/p/show?comment=1&id=2309405298596488020524
https://weibo.com/ttarticle/p/show?id=2309405298596601004062
https://weibo.com/ttarticle/p/show?comment=1&id=2309405298596601004062
https://weibo.com/ttarticle/p/show?id=2309405298596751999505
https://weibo.com/ttarticle/p/show?comment=1&id=2309405298596751999505
https://weibo.com/ttarticle/p/show?id=2309405298596886478904
https://weibo.com/ttarticle/p/show?comment=1&id=2309405298596886478904
https://weibo.com/ttarticle/p/show?id=2309405298596999463086
https://weibo.com/ttarticle/p/show?comment=1&id=2309405298596999463086
https://weibo.com/ttarticle/p/show?id=2309405298597100388368
https://weibo.com/ttarticle/p/show?comment=1&id=2309405298597100388368
