WordPress添加夜间模式cookie版 -7b2主题修改

2022-09-04 0 1,250

教程开始:

将下列代码放入子主题 child.js 文件中

    //夜间模式
(function(){
    if(document.cookie.replace(/(?:(?:^|.*;\s*)night\s*\=\s*([^;]*).*$)|^.*$/, "$1") === ''){
        if(new Date().getHours() > 22 || 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');
        }
    }
})();
//夜间模式切换
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('夜间模式关闭');
    }
}

将下列代码加入到 主题或子主题 header.php 文件中
具体位置 在 17 行后

<?php echo($_COOKIE['night'] == '1' ? 'night' : ''); ?>

将下列代码放入想要展示的地方即可调用切换开关.

<a href="javascript:switchNightMode()" target="_self">查看效果</a>

比如跟本站一样的位置,

b2/Modules/Templates/Header.php 406 行插入下面代码,

     <div class="top-search-icon mobile-show"><a href="javascript:switchNightMode()" rel="external nofollow"  target="_self"><svg class="icon" aria-hidden="true"><use xlink:href="#icon-liangdu" rel="external nofollow" ></use></svg></a></div>

最后,调试 CSS,body.night xxx ,xxx 覆盖替换的css样式,body.night .site 是把背景颜色降低亮度,

body.night .site{
    background-color: #353535;
}

 

本文章已结束,如转载请注明:汇站网 » WordPress 添加夜间模式 cookie 版 -7b2 主题修改

收藏 (0)

微信支付 微信扫一扫

支付宝支付 支付宝扫一扫

打赏二维码
点赞 (0)

站长资源下载中心-找源码上汇站

常见问题
  • 如果付款后没有弹出下载页面,多刷新几下,有问题联系客服!
查看详情
  • 本站所有资源版权均属于原作者所有,这里所提供资源均只能用于参考学习用,请勿直接商用。若由于商用引起版权纠纷,一切责任均由使用者承担。
查看详情

相关文章

联系官方客服

为您解决烦忧 - 24小时在线 专业服务