qzuser1212 發表於 2023-3-26 23:51:28

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

<br /><br />用的还是3.5版本的, 没有付费不能用最新版本,听说最新版更强大。<br />因为不会提问,问了很多个问题才得到想要的效果<br />这里我直接把贴出全部过程和效果图。<br /><br />先上效果图,还不是很完美,有些细节还需要优化<br /><font size="5"><strong>效果图</strong></font><br /> <br /><img title="mi.xcxgy.cn_forum.php (1).png" id="aimg_21184" aid="21184" src1="static/image/common/none.gif" zoom="https://www.dismall.com/data/attachment/forum/202303/26/233402o71974ibiir692u1.png" src="https://www.dismall.com/data/attachment/forum/202303/26/233402o71974ibiir692u1.png" class="zoom" onclick="zoom(this, this.src, 0, 0, 0)" width="600" inpost="1" onmouseover="showMenu({'ctrlid':this.id,'pos':'12'})" /><img title="mi.xcxgy.cn_forum.php.png" id="aimg_21183" aid="21183" src1="static/image/common/none.gif" zoom="https://www.dismall.com/data/attachment/forum/202303/26/233402wr4aff4cfibwhzf0.png" src="https://www.dismall.com/data/attachment/forum/202303/26/233402wr4aff4cfibwhzf0.png" class="zoom" onclick="zoom(this, this.src, 0, 0, 0)" width="600" inpost="1" onmouseover="showMenu({'ctrlid':this.id,'pos':'12'})" /><img title="mi.xcxgy.cn_forum.php_mod=viewthread&amp;amp;tid=61&amp;amp;extra=page=1 (1).png" id="aimg_21188" aid="21188" src1="static/image/common/none.gif" zoom="https://www.dismall.com/data/attachment/forum/202303/26/233404arslwrsjg7jsojlk.png" src="https://www.dismall.com/data/attachment/forum/202303/26/233404arslwrsjg7jsojlk.png" class="zoom" onclick="zoom(this, this.src, 0, 0, 0)" width="600" inpost="1" onmouseover="showMenu({'ctrlid':this.id,'pos':'12'})" /><img title="mi.xcxgy.cn_forum.php_mod=viewthread&amp;amp;tid=61&amp;amp;extra=page=1.png" id="aimg_21187" aid="21187" src1="static/image/common/none.gif" zoom="https://www.dismall.com/data/attachment/forum/202303/26/233403h5bi58n8tj65hn01.png" src="https://www.dismall.com/data/attachment/forum/202303/26/233403h5bi58n8tj65hn01.png" class="zoom" onclick="zoom(this, this.src, 0, 0, 0)" width="600" inpost="1" onmouseover="showMenu({'ctrlid':this.id,'pos':'12'})" /><img title="mi.xcxgy.cn_index.php(iPhone XR) (1).png" id="aimg_21189" aid="21189" src1="static/image/common/none.gif" zoom="https://www.dismall.com/data/attachment/forum/202303/27/000429kysz3qal6zl22qqx.png" src="https://www.dismall.com/data/attachment/forum/202303/27/000429kysz3qal6zl22qqx.png" class="zoom" onclick="zoom(this, this.src, 0, 0, 0)" width="600" inpost="1" onmouseover="showMenu({'ctrlid':this.id,'pos':'12'})" /><img title="mi.xcxgy.cn_index.php(iPhone XR).png" id="aimg_21190" aid="21190" src1="static/image/common/none.gif" zoom="https://www.dismall.com/data/attachment/forum/202303/27/000429ig02ija3u3230opa.png" src="https://www.dismall.com/data/attachment/forum/202303/27/000429ig02ija3u3230opa.png" class="zoom" onclick="zoom(this, this.src, 0, 0, 0)" width="600" inpost="1" onmouseover="showMenu({'ctrlid':this.id,'pos':'12'})" /><br /> <br /><font size="5"><strong>提问</strong></font><br /><br /><ul><li><font size="3">如何快速让网页支持暗黑模式</font><li><font color="#343541"><font face="Söhne, ui-sans-serif, system-ui, -apple-system, &amp;quot;"><font style="font-size:16px">怎么实现切换呢</font></font></font><li>可以弄好看一点么<li>怎么保存用户选择是否启用暗黑模式<li>可以把check换成两个图标切换么 样式更美观一些<li>用了你的代码 提示uncaught TypeError: Cannot read properties of null (reading 'addEventListener')<br /></ul>哈哈,能看到我提问了很多废话,其实有时候提问真的需要技巧,会提问也是一种能力<br />但是最终它还是给了我完整代码<br /><ol><li>&lt;!DOCTYPE html&gt;<br /><li>&lt;html lang=&quot;en&quot;&gt;<br /><li>&lt;head&gt;<br /><li>&nbsp;&nbsp;&lt;meta charset=&quot;UTF-8&quot;&gt;<br /><li>&nbsp;&nbsp;&lt;title&gt;Dark Mode Toggle&lt;/title&gt;<br /><li>&nbsp;&nbsp;&lt;link rel=&quot;stylesheet&quot; href=&quot;https://cdnjs.cloudflare.com/ajax/libs/font-awesome/5.15.3/css/all.min.css&quot;&gt;<br /><li>&nbsp;&nbsp;&lt;style&gt;<br /><li>&nbsp; &nbsp; :root {<br /><li>&nbsp; &nbsp;&nbsp; &nbsp;--background-color: #fff;<br /><li>&nbsp; &nbsp;&nbsp; &nbsp;--text-color: #333;<br /><li>&nbsp; &nbsp; }<br /><li><br /><li>&nbsp; &nbsp; .dark {<br /><li>&nbsp; &nbsp;&nbsp; &nbsp;--background-color: #222;<br /><li>&nbsp; &nbsp;&nbsp; &nbsp;--text-color: #fff;<br /><li>&nbsp; &nbsp; }<br /><li><br /><li>&nbsp; &nbsp; body {<br /><li>&nbsp; &nbsp;&nbsp; &nbsp;background-color: var(--background-color);<br /><li>&nbsp; &nbsp;&nbsp; &nbsp;color: var(--text-color);<br /><li>&nbsp; &nbsp; }<br /><li><br /><li>&nbsp; &nbsp; #dark-mode-toggle {<br /><li>&nbsp; &nbsp;&nbsp; &nbsp;background: transparent;<br /><li>&nbsp; &nbsp;&nbsp; &nbsp;border: none;<br /><li>&nbsp; &nbsp;&nbsp; &nbsp;color: #333;<br /><li>&nbsp; &nbsp;&nbsp; &nbsp;cursor: pointer;<br /><li>&nbsp; &nbsp;&nbsp; &nbsp;font-size: 1.5rem;<br /><li>&nbsp; &nbsp;&nbsp; &nbsp;position: fixed;<br /><li>&nbsp; &nbsp;&nbsp; &nbsp;right: 20px;<br /><li>&nbsp; &nbsp;&nbsp; &nbsp;top: 20px;<br /><li>&nbsp; &nbsp;&nbsp; &nbsp;z-index: 9999;<br /><li>&nbsp; &nbsp; }<br /><li><br /><li>&nbsp; &nbsp; #dark-mode-toggle i {<br /><li>&nbsp; &nbsp;&nbsp; &nbsp;display: none;<br /><li>&nbsp; &nbsp; }<br /><li><br /><li>&nbsp; &nbsp; #dark-mode-toggle.dark i:first-child {<br /><li>&nbsp; &nbsp;&nbsp; &nbsp;display: block;<br /><li>&nbsp; &nbsp; }<br /><li><br /><li>&nbsp; &nbsp; #dark-mode-toggle:not(.dark) i:last-child {<br /><li>&nbsp; &nbsp;&nbsp; &nbsp;display: block;<br /><li>&nbsp; &nbsp; }<br /><li>&nbsp;&nbsp;&lt;/style&gt;<br /><li>&lt;/head&gt;<br /><li>&lt;body&gt;<br /><li>&nbsp;&nbsp;&lt;h1&gt;Dark Mode Toggle&lt;/h1&gt;<br /><li>&nbsp;&nbsp;&lt;p&gt;This is a simple example of how to toggle dark mode using JavaScript and CSS.&lt;/p&gt;<br /><li>&nbsp;&nbsp;&lt;button id=&quot;dark-mode-toggle&quot;&gt;<br /><li>&nbsp; &nbsp; &lt;i class=&quot;fas fa-sun&quot;&gt;&lt;/i&gt;<br /><li>&nbsp; &nbsp; &lt;i class=&quot;fas fa-moon&quot;&gt;&lt;/i&gt;<br /><li>&nbsp;&nbsp;&lt;/button&gt;<br /><li>&nbsp;&nbsp;&lt;script&gt;<br /><li>&nbsp; &nbsp; var button = document.getElementById(&quot;dark-mode-toggle&quot;);<br /><li>&nbsp; &nbsp; var theme = localStorage.getItem('theme'); // get saved theme, if any<br /><li>&nbsp; &nbsp; if (theme) {<br /><li>&nbsp; &nbsp;&nbsp; &nbsp;document.documentElement.classList.add(theme); // apply saved theme<br /><li>&nbsp; &nbsp; }<br /><li>&nbsp; &nbsp; button.addEventListener(&quot;click&quot;, function() {<br /><li>&nbsp; &nbsp;&nbsp; &nbsp;document.documentElement.classList.toggle(&quot;dark&quot;);<br /><li>&nbsp; &nbsp;&nbsp; &nbsp;var theme = document.documentElement.classList.contains('dark') ? 'dark' : '';<br /><li>&nbsp; &nbsp;&nbsp; &nbsp;localStorage.setItem('theme', theme); // save current theme<br /><li>&nbsp; &nbsp; });<br /><li>&nbsp;&nbsp;&lt;/script&gt;<br /><li>&lt;/body&gt;<br /><li>&lt;/html&gt;<br /><li></ol><br /> <br /><strong><font size="5">应用到dz模板</font></strong><br /><br /><br />1、<font style="background-color:rgb(247, 247, 247)">&lt;link rel=&quot;stylesheet&quot; href=&quot;https://cdnjs.cloudflare.com/ajax/libs/font-awesome/5.15.3/css/all.min.css&quot;&gt;</font><br />这段代码是引入<font style="background-color:rgb(247, 247, 247)">font-awesome图标,一般放在common/header.htm里</font><br />2、&lt;<font style="background-color:rgb(247, 247, 247)">style&gt;...&lt;/style&gt;代码是切换主题图标的css,放</font><font style="background-color:rgb(247, 247, 247)">common/</font><font style="background-color:rgb(247, 247, 247)">extend_common.css里</font><br />3、<font style="background-color:rgb(247, 247, 247)">&lt;button id=&quot;dark-mode-toggle&quot;&gt;...</font><font style="background-color:rgb(247, 247, 247)">&lt;/button&gt;是切换主题图标的代码,放到你想显示的位置,我是放到</font><font style="background-color:rgb(247, 247, 247)">common/header.htm导航栏的右侧</font><br />4、<font style="background-color:rgb(247, 247, 247)">&lt;script&gt;...</font><font style="background-color:rgb(247, 247, 247)">&lt;/script&gt;是用来切换主题、图标及保存用户选择的主题的代码,放到</font><font style="background-color:rgb(247, 247, 247)">common/footer.htm里的&lt;/body&gt;之前,最后一个问题就是因为我放到了header导致了错误,ai纠正了我</font><br /><br /><strong><font size="3">我问:</font></strong>用了你的代码 提示uncaught TypeError: Cannot read properties of null (reading 'addEventListener')<br /><br /><font size="4"><strong>AI:</strong></font>这个错误通常表示JavaScript代码尝试访问一个不存在的DOM元素。可能是因为DOM元素尚未加载到页面中,或者元素ID或类名错误。您可以检查以下内容:<br />&nbsp; &nbsp;&nbsp; &nbsp;&nbsp; &nbsp;确保&lt;button&gt;元素具有正确的ID属性,即id=&quot;dark-mode-toggle&quot;。<br />&nbsp; &nbsp;&nbsp; &nbsp;&nbsp; &nbsp;确保您的JavaScript代码位于&lt;body&gt;元素的末尾,以确保在DOM元素加载完毕之后再运行代码。<br />&nbsp; &nbsp;&nbsp; &nbsp;&nbsp; &nbsp;如果问题仍然存在,您可以在浏览器的开发者工具中打开控制台,查看更详细的错误信息。<br /><br /><br /><br />代码<em>, </em>rgb<em>, </em>切换                              <img title="mi.xcxgy.cn_index.php(iPhone XR).png" id="aimg_21185" aid="21185" src1="static/image/common/none.gif" zoom="https://www.dismall.com/data/attachment/forum/202303/26/233403i4etfxmm8w3cibwt.png" src="https://www.dismall.com/data/attachment/forum/202303/26/233403i4etfxmm8w3cibwt.png" class="zoom" onclick="zoom(this, this.src, 0, 0, 0)" width="600" alt="mi.xcxgy.cn_forum.php_mod=forumdisplay&amp;amp;fid=3 (1).png" title="mi.xcxgy.cn_forum.php_mod=forumdisplay&amp;amp;fid=3 (1).png" w="2880" /><img title="mi.xcxgy.cn_index.php(iPhone XR).png" id="aimg_21186" aid="21186" src1="static/image/common/none.gif" zoom="https://www.dismall.com/data/attachment/forum/202303/26/233403o84zxgxmghkav1v8.png" src="https://www.dismall.com/data/attachment/forum/202303/26/233403o84zxgxmghkav1v8.png" class="zoom" onclick="zoom(this, this.src, 0, 0, 0)" width="600" alt="mi.xcxgy.cn_forum.php_mod=forumdisplay&amp;amp;fid=3.png" title="mi.xcxgy.cn_forum.php_mod=forumdisplay&amp;amp;fid=3.png" w="2880" />

s77 發表於 2023-3-27 15:06:31

学习学习。

小花 發表於 2023-3-27 16:30:34

谢谢谢谢谢寻寻

276397082 發表於 2023-3-27 21:06:29

以后都不会有站长了

gulaibai 發表於 2023-3-27 22:06:14

感谢分享

yezirui 發表於 2023-3-29 02:35:22

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

Guoyuh78 發表於 2023-3-29 22:04:18

我搭建了一个ChatGPT 网页 可以直接用&nbsp; &nbsp;ailiao.wang

Guoyuh78 發表於 2023-3-29 22:07:57

<br />好的 我的经常掉线
頁: [1]
查看完整版本: 利用chatgpt5分钟实现了网站的暗黑主题切换功能