基于JavaScript或jQuery實(shí)現(xiàn)網(wǎng)站夜間/高亮模式
創(chuàng)建夜間/高亮模式的步驟:
創(chuàng)建一個(gè)HTML文檔。
為文檔文件以及黑暗模式創(chuàng)建CSS。
添加一個(gè)開(kāi)關(guān)轉(zhuǎn)換器按鈕,以在明暗模式之間進(jìn)行切換。
使用javascript或jQuery代碼向開(kāi)關(guān)轉(zhuǎn)換器添加功能,以在明暗模式之間切換。
示例1:以下示例演示了使用JQuery代碼在明暗模式之間進(jìn)行切換。它基本上通過(guò)使用函數(shù)hasClass(),addClass()和removeClass()方法來(lái)工作。
<!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:以下示例演示了通過(guò)在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>
以上就是本文的全部?jī)?nèi)容,希望對(duì)大家的學(xué)習(xí)有所幫助,也希望大家多多支持好吧啦網(wǎng)。
相關(guān)文章:
1. Ajax實(shí)現(xiàn)文件上傳功能(Spring MVC)2. 基于javaweb+jsp實(shí)現(xiàn)學(xué)生宿舍管理系統(tǒng)3. idea設(shè)置代碼格式化的方法步驟4. asp createTextFile生成文本文件支持utf85. Python 簡(jiǎn)介6. python使用timeit時(shí)間模塊7. Java Synchronized的使用詳解8. 使用EF Code First搭建簡(jiǎn)易ASP.NET MVC網(wǎng)站并允許數(shù)據(jù)庫(kù)遷移9. ASP.NET MVC使用jQuery ui的progressbar實(shí)現(xiàn)進(jìn)度條10. xml創(chuàng)建節(jié)點(diǎn)(根節(jié)點(diǎn)、子節(jié)點(diǎn))
