深色模式對(duì)于任何網(wǎng)站都非常重要。不同興趣的用戶(hù)訪問(wèn)網(wǎng)站。有些人喜歡深色模式,有些人喜歡淺色模式。
根據(jù)一項(xiàng)調(diào)查,大約 70% 到 80% 的人喜歡深色模式,只有 20% 到 30% 的人喜歡淺色模式。因此,有必要為任何網(wǎng)站創(chuàng)建一個(gè)深色模式,允許用戶(hù)在深色和淺色模式之間切換。
下面,我們將使用 HTML、CSS 和 JavaScript 創(chuàng)建一個(gè)簡(jiǎn)單的網(wǎng)頁(yè)。此外,我們還將學(xué)習(xí)使用 JavaScript 和 CSS 實(shí)現(xiàn)明暗模式。
語(yǔ)法
用戶(hù)可以按照以下語(yǔ)法在深色和淺色主題之間切換。
body.classList.toggle("dark-theme");
登錄后復(fù)制
在上面的語(yǔ)法中,我們將深色主題類(lèi)名作為切換方法的參數(shù)傳遞。它在特定的 HTML 元素中添加和刪除類(lèi)。
算法
用戶(hù)可以按照以下算法在深色和淺色主題之間切換。
第 1 步 – 為網(wǎng)頁(yè)編寫(xiě) HTML 代碼,并像往常一樣為淺色主題應(yīng)用 CSS。
第 2 步 – 使用 CSS 創(chuàng)建深色主題的樣式。
第 3 步 – 創(chuàng)建一個(gè)按鈕,并在按鈕中添加 onclick 事件。當(dāng)用戶(hù)單擊該按鈕時(shí),它應(yīng)該在深色和淺色主題之間切換。
步驟 4 – 要在深色和淺色主題之間切換,我們可以從元素中添加和刪除特定的類(lèi)。例如,為深色主題創(chuàng)建一個(gè)類(lèi),并為深色主題創(chuàng)建 CSS。當(dāng)我們?cè)赽ody中添加dark-theme類(lèi)時(shí),深色主題應(yīng)該應(yīng)用于整個(gè)網(wǎng)站,當(dāng)我們刪除它時(shí),它應(yīng)該是正常的。
示例 1
在下面的示例中,當(dāng)用戶(hù)單擊按鈕時(shí),我們調(diào)用changeMode()函數(shù)。 changeMode() 函數(shù)根據(jù)主題更改按鈕的文本。
此外,我們使用 document.body 訪問(wèn)整個(gè)網(wǎng)頁(yè),并使用 JavaScript 將 dark-theme 類(lèi)添加到整個(gè)網(wǎng)頁(yè),以將主題更改為深色模式。
<html> <head> <style> body { color: black; background-color: rgb(241, 241, 241); text-align: center; justify-content: center; } .dark-theme { color: white; background-color: rgb(26, 20, 20); } button { width: 13rem; height: 2rem; font-size: 1.5rem; background-color: aqua; border: none; border-radius: 12px; } p { font-size: 1.2rem; } </style> </head> <body> <h2>Using the <i> toggle method to </i> toggle between light and dark mode in JavaScript. </h2> <p>This is the content of the webpage. </p> <button onclick = "changeMode()" id = "button"> Dark Mode </button> <script> function changeMode() { var body = document.body; // toggle the theme body.classList.toggle("dark-theme"); let button = document.getElementById('button'); // change the button text if (button.innerHTML == "Dark Mode") { button.innerHTML = "Normal Mode"; } else { button.innerHTML = "Dark Mode" } } </script> </body> </html>
登錄后復(fù)制
示例 2
在下面的示例中,我們使用 HTML 和 CSS 創(chuàng)建了切換開(kāi)關(guān)按鈕。我們使用了切換開(kāi)關(guān)的復(fù)選框。因此,每當(dāng)用戶(hù)選中該復(fù)選框時(shí),開(kāi)關(guān)就會(huì)打開(kāi)。因此,我們可以根據(jù)是否選中該復(fù)選框來(lái)添加和刪除深色主題的類(lèi)。
此外,我們還使用了JQuery的addClass()和removeClass()方法來(lái)添加和刪除主體中的類(lèi)。
<html> <head> <style> body { color: black; background-color: rgb(241, 241, 241); text-align: center; justify-content: center; } .dark-class { color: white; background-color: rgb(12, 10, 10); } p { font-size: 1.2rem; } .toggleButton { width: 5rem; height: 2rem; position: relative; display: inline-block; } .toggleButton input { opacity: 0; } .roundButton { background-color: black; top: 0; left: 0; position: absolute; right: 0; bottom: 0; cursor: pointer; } .roundButton:before { left: 0; bottom: 0; position: absolute; content: ""; background-color: grey; transition: 1s; height: 2rem; width: 2rem; } input:checked+.roundButton { background-color: white; } input:checked+.roundButton:before { transform: translateX(3rem); } .roundButton.circle { border-radius: 2rem; } .roundButton.circle:before { border-radius: 50%; } </style> <script src = "https://cdnjs.cloudflare.com/ajax/libs/jquery/3.6.3/jquery.min.js" Integrity = "sha512-STof4xm1wgkfm7heWqFJVn58Hm3EtS31XFaagaa8VMReCXAkQnJZ+jEy8PCC/iT18dFy95WcExNHFTqLyp72eQ==" crossorigin = "anonymous" referrerpolicy = "no-referrer"> </script> </head> <body> <h2>Using the <i> JQuery </i> to toggle between light and dark modes in JavaScript.</h2> <p>This is the content of the webpage.</p> <label class = "toggleButton"> <input type = "checkbox" id = "toggle"> <div class = "roundButton circle" ></div> </label> <script> // If the input checkbox is checked, activate dark mode by adding dark-class to the body $('#toggle').change(() => { if ($('#toggle').is(":checked")) { $("body").addClass("dark-class"); } else { $("body").removeClass("dark-class") } }) </script> </body> </html>
登錄后復(fù)制
我們學(xué)會(huì)了使用 JavaScript 和 JQuery 在深色模式和淺色模式之間進(jìn)行切換。我們需要更改網(wǎng)頁(yè)的 CSS 并將 CSS 應(yīng)用于深色主題。我們可以通過(guò)在網(wǎng)頁(yè)中添加和刪除類(lèi)來(lái)將 CSS 應(yīng)用到暗模式。
以上就是如何使用 JavaScript/jQuery 為網(wǎng)站創(chuàng)建暗/亮模式?的詳細(xì)內(nèi)容,更多請(qǐng)關(guān)注www.92cms.cn其它相關(guān)文章!