document.writeln('');
進(jìn)入主題,首先第一步,在您的頁(yè)腳文件:footer.php上面加入以下js代碼。
< script type = "text/javascript" >function switchNightMode() {var night = document.cookie.replace(/(?:(?:^|.*;\s*)night\s*\=\s*([^;]*).*$)|^.*$/, "$1") || "0";if (night == "0") {document.body.classList.add("night");document.cookie = "night=1;path=/";console.log("夜間模式開啟")} else {document.body.classList.remove("night");document.cookie = "night=0;path=/";console.log("夜間模式關(guān)閉")}} (function() {if (document.cookie.replace(/(?:(?:^|.*;\s*)night\s*\=\s*([^;]*).*$)|^.*$/, "$1") === "") {if (new Date().getHours() > 21 || new Date().getHours() < 6) {document.body.classList.add("night");document.cookie = "night=1;path=/";console.log("夜間模式開啟")} else {document.body.classList.remove("night");document.cookie = "night=0;path=/";console.log("夜間模式關(guān)閉")}} else {var night = document.cookie.replace(/(?:(?:^|.*;\s*)night\s*\=\s*([^;]*).*$)|^.*$/, "$1") ||"0";if (night == "0") {document.body.classList.remove("night")} else {if (night == "1") {document.body.classList.add("night")}}}})();</script>< script type = "text/javascript" > function switchNightMode() { var night = document.cookie.replace(/(?:(?:^|.*;\s*)night\s*\=\s*([^;]*).*$)|^.*$/, "$1") || "0"; if (night == "0") { document.body.classList.add("night"); document.cookie = "night=1;path=/"; console.log("夜間模式開啟") } else { document.body.classList.remove("night"); document.cookie = "night=0;path=/"; console.log("夜間模式關(guān)閉") } } (function() { if (document.cookie.replace(/(?:(?:^|.*;\s*)night\s*\=\s*([^;]*).*$)|^.*$/, "$1") === "") { if (new Date().getHours() > 21 || new Date().getHours() < 6) { document.body.classList.add("night"); document.cookie = "night=1;path=/"; console.log("夜間模式開啟") } else { document.body.classList.remove("night"); document.cookie = "night=0;path=/"; console.log("夜間模式關(guān)閉") } } else { var night = document.cookie.replace(/(?:(?:^|.*;\s*)night\s*\=\s*([^;]*).*$)|^.*$/, "$1") || "0"; if (night == "0") { document.body.classList.remove("night") } else { if (night == "1") { document.body.classList.add("night") } } } })(); </script>< script type = "text/javascript" >function switchNightMode() {var night = document.cookie.replace(/(?:(?:^|.*;\s*)night\s*\=\s*([^;]*).*$)|^.*$/, "$1") || "0";if (night == "0") {document.body.classList.add("night");document.cookie = "night=1;path=/";console.log("夜間模式開啟")} else {document.body.classList.remove("night");document.cookie = "night=0;path=/";console.log("夜間模式關(guān)閉")}} (function() {if (document.cookie.replace(/(?:(?:^|.*;\s*)night\s*\=\s*([^;]*).*$)|^.*$/, "$1") === "") {if (new Date().getHours() > 21 || new Date().getHours() < 6) {document.body.classList.add("night");document.cookie = "night=1;path=/";console.log("夜間模式開啟")} else {document.body.classList.remove("night");document.cookie = "night=0;path=/";console.log("夜間模式關(guān)閉")}} else {var night = document.cookie.replace(/(?:(?:^|.*;\s*)night\s*\=\s*([^;]*).*$)|^.*$/, "$1") ||"0";if (night == "0") {document.body.classList.remove("night")} else {if (night == "1") {document.body.classList.add("night")}}}})();</script>
加好后在您的頁(yè)面body處加入php判斷,這樣當(dāng)檢測(cè)到cookie相關(guān)字段時(shí)直接輸出body的class為night,已防止頁(yè)面閃爍。在頭部文件:header.php文件中加入以下代碼
<body class="<?php echo($_COOKIE[night] == 1 ? night : ); ?>"><body class="<?php echo($_COOKIE[night] == 1 ? night : ); ?>"><body class="<?php echo($_COOKIE[night] == 1 ? night : ); ?>">
如果原中已有了class的話就將<?php echo($_COOKIE[night] == 1 ? night : ); ?>放在class=“”里面。
最后再將網(wǎng)站中所有需要變暗的地方調(diào)整其css,已達(dá)到變暗效果,具體css調(diào)整示例可看下方。
body.night 需要調(diào)整的區(qū)塊{background-color: #000000;color: #aaa;}body.night img {filter: brightness(30%);}body.night 需要調(diào)整的區(qū)塊{ background-color: #000000; color: #aaa; } body.night img { filter: brightness(30%); }body.night 需要調(diào)整的區(qū)塊{background-color: #000000;color: #aaa;}body.night img {filter: brightness(30%);}
由于每個(gè)網(wǎng)站或者博客CSS的命名不一樣,所以請(qǐng)根據(jù)自己的網(wǎng)站/博客CSS進(jìn)行修改,大家可以通過右鍵“審查元素”來(lái)獲取CLASS的名字。
每個(gè)CLASS的區(qū)塊都要用“body.night 需要調(diào)整的區(qū)塊”書寫,如“body.night h2”、“body.night .post-tags”等。
這樣當(dāng)晚上9點(diǎn)到白天6點(diǎn)之間,網(wǎng)站打開時(shí)就自動(dòng)會(huì)變成暗黑模式。當(dāng)然,你也可以加一個(gè)按鈕,來(lái)手動(dòng)控制打開關(guān)閉暗黑模式。如下代碼。
<a href="javascript:switchNightMode()" target="_self">Dark</a><a href="javascript:switchNightMode()" target="_self">Dark</a><a href="javascript:switchNightMode()" target="_self">Dark</a>