前端實現(xiàn)界面切換主題代碼示例
更新時間:2025年02月26日 10:39:43 作者:江戶川亮仔
這篇文章主要介紹了前端實現(xiàn)界面切換主題的相關(guān)資料,文中介紹了兩種方法,通過link標(biāo)簽的rel屬性和通過變量設(shè)置,前者適用于確定的主題樣式切換,后者則適用于在拾色器中任意選擇顏色更換主題的情況,需要的朋友可以參考下
樣式切換主題
- 常用的主題切換實現(xiàn)方式之一,就是通過
link
標(biāo)簽的rel
屬性來實現(xiàn)的 - 當(dāng)
rel
標(biāo)簽的值是alternate
,就代表該樣式是可以替換的 title
屬性要加就全加上或者全不加,因為title
會導(dǎo)致系統(tǒng)直接識別成樣式文件,意思就是如果兩個樣式文件,第一個沒有加該屬性,第二個加了該屬性,系統(tǒng)會直接使用有該屬性的樣式進行載入- 假如此時頁面有三個樣式文件,分別是:
default.css
、dark.css
、light.css
- 可以通過激活可替換樣式即可實現(xiàn)主題的切換,適合確定的主題樣式之間切換
/* default.css */ body { background-color: white; } /* dark.css */ body { background-color: black; } /* light.css */ body { background-color: lightcyan; }
<!DOCTYPE html> <html lang="en" class="dark"> <head> <meta charset="UTF-8"> <meta name="viewport" content="width=device-width, initial-scale=1.0"> <title>主題切換方案一</title> <link type="text/css" href="css/default.css" rel="external nofollow" rel="stylesheet" title="default"> <link type="text/css" href="css/dark.css" rel="external nofollow" rel="stylesheet alternate" title="dark"> <link type="text/css" href="css/light.css" rel="external nofollow" rel="stylesheet alternate" title="light"> </head> <body> <select name="主題" onchange="changeTheme(this.value)"> <option value="default">默認(rèn)</option> <option value="dark">夜晚</option> <option value="light">白天</option> </select> </body> <script src="static/js/jquery.min.js"></script> <script> function changeTheme(theme) { $('link').attr('disabled', true) $(`link[title=${theme}]`).attr('disabled', false) } </script> </html>
變量設(shè)置主題
- 上面是主題樣式文件之間的切換,對于那些在拾色器中任意選擇顏色更換主題的就不大適用了
- 這時候切換主題更適合通過變量去設(shè)置系統(tǒng)的主題
- 現(xiàn)在
:root
中定義全局變量,然后通過var()
在樣式中去使用變量,然后通過去setProperty
修改該變量值即可更換主題樣式了
<!DOCTYPE html> <html lang="en" class="dark"> <head> <meta charset="UTF-8"> <meta name="viewport" content="width=device-width, initial-scale=1.0"> <title>主題切換方案二</title> <style> :root { --theme-color: #333333; } html { background-color: var(--theme-color, "#E65A65"); } </style> </head> <body> <select name="主題" onchange="changeTheme(this.value)"> <option value="#333333">默認(rèn)</option> <option value="#1a7efc">藍色</option> <option value="#16d46b">綠色</option> <option value="#f1ce6b">黃色</option> </select> </body> <script> function changeTheme(theme) { document.documentElement.style.setProperty('--theme-color', theme) } </script> </html>
總結(jié)
到此這篇關(guān)于前端實現(xiàn)界面切換主題的文章就介紹到這了,更多相關(guān)前端界面切換主題內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
您可能感興趣的文章:
相關(guān)文章
js操作CheckBoxList實現(xiàn)全選/反選(在客服端完成)
對于CheckBoxList控件來說,一方面要實現(xiàn)大量數(shù)據(jù)在服務(wù)器端的綁定工作,另一方面往往要求實現(xiàn)全選、反選等功能,接下來將介紹js操作CheckBoxList實現(xiàn)全選/反選,感興趣的朋友可以了解下,或許對你有所幫助2013-02-02兼容IE和Firefox火狐的上下、左右循環(huán)無間斷滾動JS代碼
html里的marqueen也能實現(xiàn)內(nèi)容的滾動,但滾動是間斷的,運用JavaScript可以使這一問題得到改觀,實現(xiàn)無間斷的滾動,讓頁面看起來更美觀2013-04-04在js代碼拼接dom對象到頁面上去的模板總結(jié)(必看)
下面小編就為大家?guī)硪黄趈s代碼拼接dom對象到頁面上去的模板總結(jié)(必看)。小編覺得挺不錯的,現(xiàn)在就分享給大家,也給大家做個參考。一起跟隨小編過來看看吧2017-02-02JavaScript通過改變文字透明度實現(xiàn)的文字閃爍效果實例
這篇文章主要介紹了JavaScript通過改變文字透明度實現(xiàn)的文字閃爍效果,結(jié)合完整實例形式分析了javascript基于定時器周期性動態(tài)修改頁面元素屬性的相關(guān)操作技巧,需要的朋友可以參考下2017-04-04