基于JavaScript或jQuery實(shí)現(xiàn)網(wǎng)站夜間/高亮模式
創(chuàng)建夜間/高亮模式的步驟:
創(chuàng)建一個(gè)HTML文檔。
為文檔文件以及黑暗模式創(chuàng)建CSS。
添加一個(gè)開關(guān)轉(zhuǎn)換器按鈕,以在明暗模式之間進(jìn)行切換。
使用javascript或jQuery代碼向開關(guān)轉(zhuǎn)換器添加功能,以在明暗模式之間切換。
示例1:以下示例演示了使用JQuery代碼在明暗模式之間進(jìn)行切換。它基本上通過使用函數(shù)hasClass(),addClass()和removeClass()方法來工作。
<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8"> <meta name="viewport" content="width=device-width, initial-scale=1.0"> <title> Dark Mode </title> <script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.4.0/jquery.min.js"> </script> <style> body{ padding:10% 3% 10% 3%; text-align:center; } img{ height:140px; width:140px; } h1{ color: #32a852; } .mode { float:right; } .change { cursor: pointer; border: 1px solid #555; border-radius: 40%; width: 20px; text-align: center; padding: 5px; margin-left: 8px; } .dark{ color: #e6e6e6; } </style> </head> <body> <div class="mode"> Dark mode: <span class="change"> OFF </span> </div> <div> <h1> GeeksforGeeks </h1> <p> <i> A Computer Science Portal for Geeks </i> </p> <h3> Light and Dark Mode </h3> <img src="https://media.geeksforgeeks.org/wp-content/uploads/20200122115631/GeeksforGeeks210.png"> <p> Click on the switch on top-right to move to dark mode. </p> </div> <script> $(".change").on("click", function() { if ($("body").hasClass("dark")) { $("body").removeClass("dark"); $(".change").text("OFF"); } else { $("body").addClass("dark"); $(".change").text("ON"); } }); </script> </body> </html>
示例2:以下示例演示了通過在JavaScript代碼中使用toggle()函數(shù)在高亮模式和夜間模式之間進(jìn)行切換。
<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8"> <meta name="viewport" content="width=device-width, initial-scale=1.0"> <title> Dark Mode </title> <style> body{ padding:0% 3% 10% 3%; text-align:center; } h1{ color: #32a852; margin-top:30px; } button{ cursor: pointer; border: 1px solid #555; text-align: center; padding: 5px; margin-left: 8px; } .dark{ color: #e6e6e6; } </style> </head> <body> <h1> GeeksforGeeks </h1> <p> <i> A Computer Science Portal for Geeks </i> </p> <h3> Light and Dark Mode </h3> <button onclick="myFunction()"> Switch mode </button> <script> function myFunction() { var element = document.body; element.classList.toggle("dark"); } </script> </body> </html>
以上就是本文的全部內(nèi)容,希望對大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。
相關(guān)文章
javascript中利用數(shù)組實(shí)現(xiàn)的循環(huán)隊(duì)列代碼
javascript中利用數(shù)組實(shí)現(xiàn)的循環(huán)隊(duì)列代碼,需要的朋友可以參考下。2010-01-01使用Nginx部署前端項(xiàng)目的詳細(xì)步驟記錄
在實(shí)際開發(fā)中我們通常會使用Nginx來部署前端靜態(tài)頁面,以提供快速訪問速度和良好的用戶體驗(yàn),這篇文章主要給大家介紹了關(guān)于使用Nginx部署前端項(xiàng)目的詳細(xì)步驟,需要的朋友可以參考下2024-08-08JS實(shí)現(xiàn)仿餓了么在瀏覽器標(biāo)簽頁失去焦點(diǎn)時(shí)網(wǎng)頁Title改變
這篇文章主要介紹了JS實(shí)現(xiàn)仿餓了么在瀏覽器標(biāo)簽頁失去焦點(diǎn)時(shí)網(wǎng)頁Title改變,需要的朋友可以參考下2017-06-06fixedBox固定div漂浮代碼支持ie6以上大部分主流瀏覽器
本例為大家分享的是fixedBox固定div漂浮代碼支持ie6以上大部分瀏覽器,需要的朋友可以參考下2014-06-06javascript實(shí)現(xiàn)數(shù)字時(shí)鐘特效
這篇文章主要為大家詳細(xì)介紹了javascript實(shí)現(xiàn)數(shù)字時(shí)鐘,文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2021-09-09javascript和jquery實(shí)現(xiàn)用戶登錄驗(yàn)證
這篇文章主要為大家詳細(xì)介紹了javascript和jquery分別實(shí)現(xiàn)用戶登錄驗(yàn)證的方法,文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2016-05-05JS實(shí)現(xiàn)懸浮移動窗口(懸浮廣告)的特效
頁面加載完成之后向頁面插入窗口,之后向窗口插入關(guān)閉按鈕,使用setInterval()函數(shù)觸發(fā)moves()函數(shù)開始動畫2013-03-03使用GruntJS鏈接與壓縮多個(gè)JavaScript文件過程詳解
怎么把多個(gè)JS文件搞成一個(gè)啊,GruntJS – JavaScript多文件編譯,風(fēng)格檢查,鏈接與壓縮神器,使用過程如下,有需要的朋友可以參考下,希望對大家有所幫助2013-08-08微信小程序與webview交互實(shí)現(xiàn)支付功能
這篇文章主要介紹了微信小程序與webview交互實(shí)現(xiàn)支付功能,文中通過示例代碼介紹的非常詳細(xì),對大家學(xué)習(xí)或者使用小程序具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面來一起學(xué)習(xí)學(xué)習(xí)吧2019-06-06