·设为首页收藏本站📧邮箱修改🎁免费下载专区📒收藏夹👽聊天室📱AI智能体
返回列表 发布新帖

利用chatgpt5分钟实现了网站的暗黑主题切换功能

630 2
发表于 2023-3-27 07:42:18 | 查看全部 阅读模式

马上注册,免费下载更多dz插件网资源。

您需要 登录 才可以下载或查看,没有账号?立即注册

×
用的还是3.5版本的, 没有付费不能用最新版本,听说最新版更强大。
因为不会提问,问了很多个问题才得到想要的效果
这里我直接把贴出全部过程和效果图。

先上效果图,还不是很完美,有些细节还需要优化
效果图

利用chatgpt5分钟实现了网站的暗黑主题切换功能 利用,分钟,实现,网站,暗黑 利用chatgpt5分钟实现了网站的暗黑主题切换功能 利用,分钟,实现,网站,暗黑 利用chatgpt5分钟实现了网站的暗黑主题切换功能 利用,分钟,实现,网站,暗黑 利用chatgpt5分钟实现了网站的暗黑主题切换功能 利用,分钟,实现,网站,暗黑 利用chatgpt5分钟实现了网站的暗黑主题切换功能 利用,分钟,实现,网站,暗黑 利用chatgpt5分钟实现了网站的暗黑主题切换功能 利用,分钟,实现,网站,暗黑

提问

    如何快速让网页支持暗黑模式怎么实现切换呢可以弄好看一点么怎么保存用户选择是否启用暗黑模式可以把check换成两个图标切换么 样式更美观一些用了你的代码 提示uncaught TypeError: Cannot read properties of null (reading 'addEventListener')
哈哈,能看到我提问了很多废话,其实有时候提问真的需要技巧,会提问也是一种能力
但是最终它还是给了我完整代码
  1. <!DOCTYPE html><html lang="en"><head>  <meta charset="UTF-8">  <title>Dark Mode Toggle</title>  <link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/font-awesome/5.15.3/css/all.min.css">  <style>    :root {      --background-color: #fff;      --text-color: #333;    }    .dark {      --background-color: #222;      --text-color: #fff;    }    body {      background-color: var(--background-color);      color: var(--text-color);    }    #dark-mode-toggle {      background: transparent;      border: none;      color: #333;      cursor: pointer;      font-size: 1.5rem;      position: fixed;      right: 20px;      top: 20px;      z-index: 9999;    }    #dark-mode-toggle i {      display: none;    }    #dark-mode-toggle.dark i:first-child {      display: block;    }    #dark-mode-toggle:not(.dark) i:last-child {      display: block;    }  </style></head><body>  <h1>Dark Mode Toggle</h1>  <p>This is a simple example of how to toggle dark mode using JavaScript and CSS.</p>  <button id="dark-mode-toggle">    <i class="fas fa-sun"></i>    <i class="fas fa-moon"></i>  </button>  <script>    var button = document.getElementById("dark-mode-toggle");    var theme = localStorage.getItem('theme'); // get saved theme, if any    if (theme) {      document.documentElement.classList.add(theme); // apply saved theme    }    button.addEventListener("click", function() {      document.documentElement.classList.toggle("dark");      var theme = document.documentElement.classList.contains('dark') ? 'dark' : '';      localStorage.setItem('theme', theme); // save current theme    });  </script></body></html>
复制代码

应用到dz模板




我问:用了你的代码 提示uncaught TypeError: Cannot read properties of null (reading 'addEventListener')

AI:这个错误通常表示JavaScript代码尝试访问一个不存在的DOM元素。可能是因为DOM元素尚未加载到页面中,或者元素ID或类名错误。您可以检查以下内容:
         确保<button>元素具有正确的ID属性,即id="dark-mode-toggle"。
         确保您的JavaScript代码位于<body>元素的末尾,以确保在DOM元素加载完毕之后再运行代码。
         如果问题仍然存在,您可以在浏览器的开发者工具中打开控制台,查看更详细的错误信息。




利用chatgpt5分钟实现了网站的暗黑主题切换功能 利用,分钟,实现,网站,暗黑
利用chatgpt5分钟实现了网站的暗黑主题切换功能 利用,分钟,实现,网站,暗黑
我要说一句 收起回复

评论2

Discuz智能体Lv.8 发表于 2025-3-14 06:31:33 | 查看全部
在Discuz!系统中实现暗黑主题切换需要结合模板机制进行调整,以下是具体实现方案(基于DZ-X技术方案):

1. **模板文件修改**:
在`template/default/common/header.htm`文件末尾(`</head>`前)添加:
  1. <style>
  2. [data-theme='dark'] {
  3.   --bg-color: #1a1a1a;
  4.   --text-color: #e0e0e0;
  5.   --border-color: #404040;
  6. }
  7. :root {
  8.   --bg-color: #ffffff;
  9.   --text-color: #333333;
  10.   --border-color: #e5e5e5;
  11.   transition: all 0.3s ease;
  12. }
  13. body {
  14.   background: var(--bg-color);
  15.   color: var(--text-color);
  16. }
  17. .dzx-theme-switch {
  18.   position: fixed;
  19.   right: 20px;
  20.   bottom: 20px;
  21.   z-index: 9999;
  22. }
  23. </style>
复制代码


2. **功能按钮注入**:
在`template/default/common/footer.htm`底部添加:
  1. <script>
  2. // 主题切换按钮
  3. var themeBtn = document.createElement('div');
  4. themeBtn.className = 'dzx-theme-switch';
  5. themeBtn.innerHTML = `
  6.   <button onclick="toggleTheme()" class="pn">
  7.     <i class="iconfont">&#xe636;</i>
  8.   </button>
  9. `;
  10. document.body.appendChild(themeBtn);

  11. // 主题切换逻辑
  12. function toggleTheme() {
  13.   const html = document.documentElement;
  14.   const isDark = html.getAttribute('data-theme') === 'dark';
  15.   
  16.   html.setAttribute('data-theme', isDark ? '' : 'dark');
  17.   localStorage.setItem('dzx_theme', isDark ? '' : 'dark');
  18.   
  19.   // 适配Discuz!特色元素
  20.   ['#nv', '.bm', '.portalblock'].forEach(selector => {
  21.     document.querySelectorAll(selector).forEach(el => {
  22.       el.style.backgroundColor = isDark ? '#2d2d2d' : '';
  23.       el.style.borderColor = isDark ? '#404040' : '';
  24.     });
  25.   });
  26. }

  27. // 初始化主题
  28. document.addEventListener('DOMContentLoaded', () => {
  29.   const savedTheme = localStorage.getItem('dzx_theme');
  30.   if (savedTheme) {
  31.     document.documentElement.setAttribute('data-theme', savedTheme);
  32.   }
  33. });
  34. </script>
复制代码


3. **样式覆盖方案**:
在`template/default/common/extend_common.css`添加:
  1. [data-theme='dark'] {
  2.   /* 导航栏 */
  3.   #nv {
  4.     background: #2d2d2d !important;
  5.     border-color: #404040 !important;
  6.   }
  7.   
  8.   /* 内容区块 */
  9.   .bm {
  10.     background: #1e1e1e !important;
  11.     border: 1px solid #404040 !important;
  12.   }
  13.   
  14.   /* 文字链接 */
  15.   a {
  16.     color: #8ab4f8 !important;
  17.   }
  18.   
  19.   /* 输入框 */
  20.   .px {
  21.     background: #333 !important;
  22.     border-color: #555 !important;
  23.     color: #fff !important;
  24.   }
  25. }
复制代码


4. **兼容性处理**:
  1. // 在切换主题时同步更新Discuz!特色组件
  2. function updateDiscuzComponentsTheme(isDark) {
  3.   // 处理弹窗
  4.   document.querySelectorAll('.pns').forEach(btn => {
  5.     btn.style.backgroundColor = isDark ? '#404040' : '';
  6.   });
  7.   
  8.   // 处理分页
  9.   document.querySelectorAll('.pg').forEach(pg => {
  10.     pg.style.borderColor = isDark ? '#555' : '';
  11.   });
  12.   
  13.   // 处理用户面板
  14.   const umenu = document.getElementById('umenu');
  15.   if (umenu) {
  16.     umenu.style.backgroundColor = isDark ? '#2d2d2d' : '';
  17.   }
  18. }
复制代码


5. **图标优化方案**:
建议使用SVG图标实现平滑切换动画:
  1. <svg @click="toggleTheme" class="theme-icon" :class="{ active: isDark }">
  2.   <use :xlink:href="isDark ? '#icon-moon' : '#icon-sun'"></use>
  3. </svg>
复制代码


实现要点:
1. 使用`data-theme`属性作为主题切换标识
2. 通过CSS变量实现动态样式切换
3. 对Discuz!特有组件进行针对性样式覆盖
4. 采用渐进增强方式确保兼容性
5. 使用`!important`提升样式优先级来覆盖Discuz默认样式

部署完成后需到后台更新模板缓存。如果遇到特定组件样式未覆盖,可通过浏览器开发者工具检查元素,添加对应的CSS选择器到`extend_common.css`中。
-- 本回答由 人工智能 AI智能体 生成,内容仅供参考,请仔细甄别。
我要说一句 收起回复
Discuz智能体Lv.8 发表于 2025-3-18 02:49:13 | 查看全部
你好!感谢你分享你的实现过程和代码。关于你提到的错误 `Uncaught TypeError: Cannot read properties of null (reading 'addEventListener')`,这个错误通常是因为 JavaScript 代码尝试在 DOM 元素加载之前访问它。以下是一些可能的解决方案:

### 1. 确保 DOM 元素存在
确保你的 HTML 中有一个 ID 为 `dark-mode-toggle` 的元素。例如:

  1. <button id="dark-mode-toggle">
  2.   <i class="icon-sun"></i>
  3.   <i class="icon-moon"></i>
  4. </button>
复制代码


### 2. 确保 JavaScript 代码在 DOM 加载后执行
你可以将 JavaScript 代码放在 `</body>` 标签之前,或者使用 `DOMContentLoaded` 事件来确保 DOM 完全加载后再执行代码。例如:

  1. document.addEventListener("DOMContentLoaded", function() {
  2.   var button = document.getElementById("dark-mode-toggle");
  3.   var theme = localStorage.getItem('theme'); // get saved theme, if any
  4.   if (theme) {
  5.     document.documentElement.classList.add(theme); // apply saved theme
  6.   }
  7.   button.addEventListener("click", function() {
  8.     document.documentElement.classList.toggle("dark");
  9.     var theme = document.documentElement.classList.contains('dark') ? 'dark' : '';
  10.     localStorage.setItem('theme', theme); // save current theme
  11.   });
  12. });
复制代码


### 3. 检查元素 ID 是否正确
确保你在 JavaScript 中使用的 ID 与 HTML 中的 ID 完全一致。例如,`dark-mode-toggle` 应该与 HTML 中的 `id="dark-mode-toggle"` 完全匹配。

### 4. 使用开发者工具调试
如果问题仍然存在,你可以使用浏览器的开发者工具(通常按 F12 打开)来检查控制台中的错误信息,并查看是否有其他问题。

### 应用到 Discuz 模板
如果你要将这个功能应用到 Discuz 模板中,你可以将上述代码添加到你的模板文件中,通常是 `template/default/common/header.htm` 或 `template/default/common/footer.htm`。确保在适当的位置插入 HTML 和 JavaScript 代码。

例如,在 `header.htm` 中添加按钮:

  1. <button id="dark-mode-toggle">
  2.   <i class="icon-sun"></i>
  3.   <i class="icon-moon"></i>
  4. </button>
复制代码


在 `footer.htm` 中添加 JavaScript 代码:

  1. <script>
  2. document.addEventListener("DOMContentLoaded", function() {
  3.   var button = document.getElementById("dark-mode-toggle");
  4.   var theme = localStorage.getItem('theme'); // get saved theme, if any
  5.   if (theme) {
  6.     document.documentElement.classList.add(theme); // apply saved theme
  7.   }
  8.   button.addEventListener("click", function() {
  9.     document.documentElement.classList.toggle("dark");
  10.     var theme = document.documentElement.classList.contains('dark') ? 'dark' : '';
  11.     localStorage.setItem('theme', theme); // save current theme
  12.   });
  13. });
  14. </script>
复制代码


### 样式优化
你可以根据你的需求进一步优化样式,例如调整按钮的位置、大小、颜色等。你可以使用 CSS 来实现这些效果。

希望这些建议能帮助你解决问题!如果你有其他问题,欢迎继续提问。
-- 本回答由 人工智能 AI智能体 生成,内容仅供参考,请仔细甄别。
我要说一句 收起回复

回复

 懒得打字嘛,点击右侧快捷回复【查看最新发布】   【应用商城享更多资源】
您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

关闭

站长推荐上一条 /1 下一条

AI智能体
投诉/建议联系

discuzaddons@vip.qq.com

未经授权禁止转载,复制和建立镜像,
如有违反,按照公告处理!!!
  • 联系QQ客服
  • 添加微信客服

联系DZ插件网微信客服|最近更新|Archiver|手机版|小黑屋|DZ插件网! ( 鄂ICP备20010621号-1 )|网站地图 知道创宇云防御

您的IP:18.222.226.15,GMT+8, 2025-4-11 05:07 , Processed in 0.224003 second(s), 81 queries , Gzip On, Redis On.

Powered by Discuz! X5.0 Licensed

© 2001-2025 Discuz! Team.

关灯 在本版发帖
扫一扫添加微信客服
QQ客服返回顶部
快速回复 返回顶部 返回列表