日韩成人免费在线_国产成人一二_精品国产免费人成电影在线观..._日本一区二区三区久久久久久久久不

當(dāng)前位置:首頁 > 科技  > 軟件

如何使用CSS和JavaScript實施暗模式?

來源: 責(zé)編: 時間:2023-10-10 18:30:56 282觀看
導(dǎo)讀譯者 | 布加迪審校 | 重樓近年來,暗模式作為用戶界面選項備受追捧。它提供了更暗的背景和更亮的文本,不僅可以減輕眼睛疲勞,還可以節(jié)省電池續(xù)航時間,尤其是在OLED屏幕上。不妨了解如何結(jié)合使用CSS和JavaScript為網(wǎng)站和Web

譯者 | 布加迪fhA28資訊網(wǎng)——每日最新資訊28at.com

審校 | 重樓fhA28資訊網(wǎng)——每日最新資訊28at.com

近年來,暗模式作為用戶界面選項備受追捧。它提供了更暗的背景和更亮的文,不僅可以減輕眼睛疲勞,還可以節(jié)省電池續(xù)航時間,尤其是在OLED屏幕上。fhA28資訊網(wǎng)——每日最新資訊28at.com

fhA28資訊網(wǎng)——每日最新資訊28at.com

使用CSS和JavaScript實施暗模式

為了實施暗模式,您將使用CSS定義外觀。然后,您將使用JavaScript來處理暗模式和亮模式之間的切換。fhA28資訊網(wǎng)——每日最新資訊28at.com

創(chuàng)建主題類

為每個主題使用一個類,這樣您就可以在兩種模式之間輕松切換。對于較完整的項目而言,您應(yīng)該考慮暗模式如何影響設(shè)計的方面面。fhA28資訊網(wǎng)——每日最新資訊28at.com

.dark {              background: #1f1f1f;              color: #fff;     }     .light {              background: #fff;              color: #333;     }

選擇交互元素

將以下JavaScript添加到script.js文件中。第一部分代碼只是選擇用于處理切換的元素。fhA28資訊網(wǎng)——每日最新資訊28at.com

// Get a reference to the theme switcher element and the document body    const themeToggle = document.getElementById("theme__switcher");    const bodyEl = document.body;

添加切換功能

下一步,使用下列JavaScript在亮模式(light)類與暗模式(dark)類之間切換。注意,改變切換開關(guān)以表明當(dāng)前模式也是個好主意。該代碼使用CSS過濾器來實現(xiàn)這功能。fhA28資訊網(wǎng)——每日最新資訊28at.com

// Function to set the theme     function setTheme(theme) {             // If the theme is "dark," add the "dark" class, remove "light" class,             // and adjust filter style             bodyEl.classList.toggle("dark", theme === "dark");            // If the theme is "light," add the "light" class, remove "dark" class,             bodyEl.classList.toggle("light", theme !== "dark");            // adjust filter of the toggle switch            themeToggle.style.filter = theme === "dark" ? "invert(75%)" : "none";    }    // Function to toggle the theme between light and dark    function toggleTheme() {           setTheme(bodyEl.classList.contains("dark") ? "light" : "dark");    }    themeToggle.addEventListener("click", toggleTheme);

這使得您的頁面可以通過點(diǎn)擊切換容器來更改主題。fhA28資訊網(wǎng)——每日最新資訊28at.com

fhA28資訊網(wǎng)——每日最新資訊28at.com

使用JavaScript增強(qiáng)暗模式

考慮以下兩個改進(jìn),可以使您的暗模式網(wǎng)站被訪客更易于使用。fhA28資訊網(wǎng)——每日最新資訊28at.com

檢測用戶偏好

需要在網(wǎng)站加載之前檢查用戶的系統(tǒng)主題,并調(diào)整您的網(wǎng)站進(jìn)行匹配。下面介紹如何使用matchMedia函數(shù)來做到這一點(diǎn)fhA28資訊網(wǎng)——每日最新資訊28at.com

// Function to detect user's preferred theme    function detectPreferredTheme() {            // Check if the user prefers a dark color scheme using media queries            const prefersDarkMode = window.matchMedia("(prefers-color-scheme: dark)").matches;            setTheme(prefersDarkMode);    }    // Run the function to detect the user's preferred theme    detectPreferredTheme();

現(xiàn)在,任何訪問您網(wǎng)站的用戶都會看到一個與他們設(shè)備當(dāng)前主題相匹配的設(shè)計。fhA28資訊網(wǎng)——每日最新資訊28at.com

使用本地存儲持久化用戶首選項

為了進(jìn)一步增強(qiáng)用戶體驗,可以使用本地存儲記住用戶選擇模式。這確保了他們在面對會話時不必重復(fù)選擇偏愛的模式。fhA28資訊網(wǎng)——每日最新資訊28at.com

function setTheme(theme) {              bodyEl.classList.toggle("dark", theme === "dark");              bodyEl.classList.toggle("light", theme !== "dark");              themeToggle.style.filter = theme === "dark" ? "invert(75%)" : "none";             // Setting the theme in local storage              localStorage.setItem("theme", theme);    }    // Check if the theme is stored in local storage    const storedTheme = localStorage.getItem("theme");    if (storedTheme) {             setTheme(storedTheme);    }    function detectPreferredTheme() {              const prefersDarkMode = window.matchMedia("(prefers-color-scheme: dark)").matches;                  // Getting the value from local storage              const storedTheme = localStorage.getItem("theme");              setTheme(prefersDarkMode && storedTheme !== "light" ? "dark" : "light");  }

擁抱以用戶為中心的設(shè)計

暗模式不僅限于外觀,而是把用戶的舒適和偏好放在第一位。如果遵循這種方法,您可以創(chuàng)建用戶友好的界面鼓勵訪客重復(fù)訪問。您在編程和設(shè)計時,應(yīng)優(yōu)先考慮用戶便利,并為訪客提供更好的數(shù)字體驗。fhA28資訊網(wǎng)——每日最新資訊28at.com

原文標(biāo)題:How to Implement Dark Mode Using CSS and JS,作者:DAVID JAJAfhA28資訊網(wǎng)——每日最新資訊28at.com

本文鏈接:http://www.www897cc.com/showinfo-26-12700-0.html如何使用CSS和JavaScript實施暗模式?

聲明:本網(wǎng)頁內(nèi)容旨在傳播知識,若有侵權(quán)等問題請及時與本網(wǎng)聯(lián)系,我們將在第一時間刪除處理。郵件:2376512515@qq.com

上一篇: 玩轉(zhuǎn)SpringBoot—Starter組件

下一篇: Golang 中的 Bufio 包詳解之 Bufio.Scanner

標(biāo)簽:
  • 熱門焦點(diǎn)
Top 主站蜘蛛池模板: 琼海市| 车致| 若羌县| 聂荣县| 曲松县| 武义县| 鹤岗市| 临西县| 芷江| 米脂县| 武夷山市| 萍乡市| 锦屏县| 乌海市| 马尔康县| 奈曼旗| 海口市| 临沧市| 平阳县| 禹州市| 玛沁县| 屯门区| 交口县| 明光市| 平阳县| 庆安县| 壶关县| 松溪县| 福贡县| 诸城市| 北票市| 靖州| 乐陵市| 曲沃县| 平凉市| 绍兴市| 罗田县| 鄂托克旗| 安平县| 柳江县| 图们市|