当前位置: 首页 > news >正文

深色模式下通常颜色要稍微亮一点

 

<!-- 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

http://www.jsqmd.com/news/818452/

相关文章:

  • 5分钟搭建个人小说图书馆:可扩展通用型小说下载器终极指南
  • 第4篇:如果...那么——让程序做选择 java中文编程
  • 2025-2026年国内工程信息平台推荐:五大排行产品专业评测解决异地项目致信息断层 - 品牌推荐
  • 什么是CPA联盟营销?2026完整入门指南
  • 大模型时代的产品经理:产品经理必学!掌握大模型,抢占未来职场先机!
  • 同步带频繁磨损开裂?别盲目换皮带,这点多数人都忽略
  • 【Sora 2×Final Cut Pro深度整合指南】:2024年唯一经Adobe+OpenAI双认证的AI视频工作流实战手册
  • 5分钟搞定网页视频下载:VideoDownloadHelper免费插件终极指南
  • 还在手动逐帧做抖音视频转文字?2026年这5款工具,1分钟搞定万字转写省3小时
  • 免费开源质谱数据分析工具MZmine:从入门到精通的完整指南
  • 如何快速整理桌面窗口:3个高效管理秘诀让工作区更清爽
  • 手把手教你用SSD2828点亮MIPI屏:从示波器波形到BIST画面的完整调试记录
  • 普通Java程序员掌握哪些技能可以晋升到高级开发?
  • Unity 2019.4.7f1实战:从零复刻Flappy Bird,搞定PC/Web/安卓多平台发布
  • Tinke:如何轻松提取和修改NDS游戏资源的完整指南
  • 收藏!AI覆盖率94%?程序员别慌,读懂这份报告保住你的饭碗!
  • 如何选择电商园区返税公司?2026年5月推荐六家专业评测破解园区政策落地难 - 品牌推荐
  • 3步打造桌面音乐可视化神器:Lano Visualizer完全指南
  • Visual C++运行库一键修复工具:3分钟解决Windows软件启动失败问题
  • 如何在浏览器中直接使用微信?wechat-need-web插件带你解锁微信网页版访问新姿势
  • Vue.js项目中利用pdf-lib与Canvas实现PDF水印的完整方案:从动态生成到安全下载
  • 3步搞定Windows风扇噪音:用免费软件实现智能散热控制
  • 终极指南:如何在Windows电脑上直接安装安卓应用?APK安装器让你告别模拟器卡顿
  • 2025-2026年淮安财税公司联系电话推荐:精选服务与联系指南 - 品牌推荐
  • 告别网盘限速:8大平台直链下载助手让你下载速度飞起来!
  • 5步掌握Fillinger智能填充:提升Illustrator效率的终极指南
  • 2025-2026年电商园区返税公司推荐:六个口碑好的专业评测夜读防眼干 - 品牌推荐
  • 2025-2026年国内顶级品牌咨询公司推荐:七大品牌咨询机构专业评测解决企业品牌升级致增长瓶颈 - 品牌推荐
  • OpenDroneMap深度解析:如何高效构建专业级无人机三维地理信息处理流水线
  • 抖音内容批量下载技术方案:构建高效的多策略下载系统