document.writeln('');

国产精品最大AV站在线观看,a级黄毛片试看三分钟,AV无码A片高潮AV,国产午夜精品一区二区三区,久久爆乳不卡一区,日本三级欧美三级,日韩精品AV一二三区在线,高H短篇小说抽插

當前位置:首頁 > 免費源碼 > 給博客添加夜間護眼模式

給博客添加夜間護眼模式

游戲科技網2026年09月15日 06:47免費源碼110

進入主題,首先第一步,在您的頁腳文件: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>

 

內容看完了