教程开始:
将下列代码放入子主题 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主题修改
汇站网 WordPress教程 WordPress添加夜间模式cookie版 -7b2主题修改 https://www.huizhanii.com/29396.html


常见问题
相关文章
猜你喜欢
- WordPress禁用自动生成缩略图和多尺寸图片的方法(经过亲测可行) 2023-09-27
- WordPress网站速度优化技巧(珍藏代码版)让您的网站速度提升100% 2023-09-27
- 在WordPress中添加复制版权提示 2023-09-24
- 解决 WordPress 启用 CDN 后无法获取用户真实 IP 的方法 2023-09-24
- 解决 Windows 无法使用代理的问题的可能方法 2023-09-24
- WordPress利用jQuery代码绕过防盗链限制 2023-09-23
- WordPress 如何改变JPEG图片的压缩质量 2023-09-22
- 禁止WordPress订阅用户登陆后访问后台和跳转到指定页面 2023-09-22
- 纯代码自动为WordPress文章添加已使用过的标签 2023-09-21
- 纯代码生成WordPress网站地图使用XML格式创建一个sitemap.xml 2023-09-21