詳解如何簡單實現(xiàn)CSS主題的切換

我最近在我的個人網(wǎng)站上添加了一個非常簡單的配色方案(主題)切換器。您可以在網(wǎng)站的頁腳中切換此簡單的顏色切換器,以查看其實際效果。萬一其他人希望將這樣的功能添加到自己的站點/項目中,我想我會寫一篇簡短的文章解釋如何做。讓我們開始吧。
HTML
首先,我們需要包含“按鈕”,這些按鈕將觸發(fā)主題根據(jù)選擇的主題進(jìn)行切換。(注:你總是可以使這些作為 options
一個 select
元素,如果你最好的方法)
<div class="color-select"> <button onclick="toggleDefaultTheme()"></button> <button onclick="toggleSecondTheme()"></button> <button onclick="toggleThirdTheme()"></button> </div>
而已!現(xiàn)在不必太擔(dān)心 onclick
參數(shù),我們將在添加JavaScript時再回到這一點。剩下的唯一一項是向 html
元素添加默認(rèn)主題類,如下所示:
<html class="theme-default">
CSS
接下來,我們需要為兩個 color-select
按鈕設(shè)置樣式,并使用將更改整個網(wǎng)站的自定義配色方案。我們將從配色方案開始。
為了使這些主題之間能夠無縫切換,我們將更改的顏色集設(shè)置為CSS變量:
.theme-default { --accent-color: #72f1b8; --font-color: #34294f; } .theme-second { --accent-color: #FFBF00; --font-color: #59316B; } .theme-third { --accent-color: #d9455f; --font-color: #303960; } body { background-color: var(--accent-color); color: var(--font-color); }
最后,我們設(shè)置面向用戶的色板的樣式:
.color-select button { -moz-appearance: none; appearance: none; border: 2px solid; border-radius: 9999px; cursor: pointer; height: 20px; margin: 0 0.8rem 0.8rem 0; outline: 0; width: 20px; } /* Style each swatch to match the corresponding theme */ .color-select button:nth-child(1) { background: #72f1b8; border-color: #34294f; } .color-select button:nth-child(2) { background: #FFBF00; border-color: #59316B; } .color-select button:nth-child(3) { background: #d9455f; border-color: #303960; }
JavaScript
我們需要使每個色樣按鈕觸發(fā)其相應(yīng)的主題,并交換出 theme-default
我們最初附加到主 html
元素上的類。我們還需要存儲用戶選擇的內(nèi)容 localStorage
,因此在重新加載或?qū)Ш降狡渌撁鏁r,他們的選擇仍然存在。
// Set a given theme/color-scheme function setTheme(themeName) { localStorage.setItem('theme', themeName); document.documentElement.className = themeName; } // Toggle between color themes function toggleDefaultTheme() { if (localStorage.getItem('theme') !== 'theme-default'){ setTheme('theme-default'); } } function toggleSecondTheme() { if (localStorage.getItem('theme') !== 'theme-second'){ setTheme('theme-second'); } } function toggleThirdTheme() { if (localStorage.getItem('theme') !== 'theme-third'){ setTheme('theme-third'); } } // Immediately set the theme on initial load (function () { if (localStorage.getItem('theme') === 'theme-default') { setTheme('theme-default'); } if (localStorage.getItem('theme') === 'theme-second') { setTheme('theme-second'); } if (localStorage.getItem('theme') === 'theme-third') { setTheme('theme-third'); } })();
就是這樣!現(xiàn)在,這僅取決于您希望每種主題樣式的自定義程度??赡苄允菬o止境!
到此這篇關(guān)于詳解如何簡單實現(xiàn)CSS主題的切換的文章就介紹到這了,更多相關(guān)CSS主題的切換內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章,希望大家以后多多支持腳本之家!
相關(guān)文章
- 本文主要介紹了CSS變量實現(xiàn)主題切換的方法,文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2021-06-23
- 這篇文章主要介紹了基于Css Variable的主題切換完美解決方案,本文通過實例代碼圖文相結(jié)合給大家介紹的非常詳細(xì),對大家的學(xué)習(xí)或工作具有一定的參考借鑒價值,需要的朋友參2020-06-18
- 今天,CSS預(yù)處理器是Web開發(fā)的標(biāo)準(zhǔn)。 預(yù)處理器的一個主要優(yōu)點是它們使您能夠使用變量, 這有助于您避免復(fù)制和粘貼代碼,并簡化了開發(fā)和重構(gòu)。今天通過本文給大家分享如何將2018-11-14
- 這篇文章主要介紹了CSS自定義屬性與前端頁面的主題切換,CSS自定義屬性在一個css選擇器內(nèi)部進(jìn)行定義,使用兩個短橫連接線 -- 作為開頭命名的名稱即被稱為自定義屬性,本文結(jié)2022-03-21