document.writeln('');
進入主題,首先第一步,在您的頁腳文件: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("夜間模式關閉")}} (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("夜間模式關閉")}} 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("夜間模式關閉") } } (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("夜間模式關閉") } } 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("夜間模式關閉")}} (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("夜間模式關閉")}} 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>
加好后在您的頁面body處加入php判斷,這樣當檢測到cookie相關字段時直接輸出body的class為night,已防止頁面閃爍。在頭部文件: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=“”里面。
最后再將網站中所有需要變暗的地方調整其css,已達到變暗效果,具體css調整示例可看下方。
body.night 需要調整的區塊{background-color: #000000;color: #aaa;}body.night img {filter: brightness(30%);}body.night 需要調整的區塊{ background-color: #000000; color: #aaa; } body.night img { filter: brightness(30%); }body.night 需要調整的區塊{background-color: #000000;color: #aaa;}body.night img {filter: brightness(30%);}
由于每個網站或者博客CSS的命名不一樣,所以請根據自己的網站/博客CSS進行修改,大家可以通過右鍵“審查元素”來獲取CLASS的名字。
每個CLASS的區塊都要用“body.night 需要調整的區塊”書寫,如“body.night h2”、“body.night .post-tags”等。
這樣當晚上9點到白天6點之間,網站打開時就自動會變成暗黑模式。當然,你也可以加一個按鈕,來手動控制打開關閉暗黑模式。如下代碼。
<a href="javascript:switchNightMode()" target="_self">Dark</a><a href="javascript:switchNightMode()" target="_self">Dark</a><a href="javascript:switchNightMode()" target="_self">Dark</a>