本文在Typecho下实装
新建darkmode.js文件
//点击启用
function switchdarkmodeMode(){
var darkmode = document.cookie.replace(/(?:(?:^|.*;\s*)darkmode\s*\=\s*([^;]*).*$)|^.*$/, "$1") || '0';
if(darkmode == '0'){
document.body.classList.add('darkmode');
document.cookie = "darkmode=1;path=/"
console.log('夜间模式开启');
}else{
document.body.classList.remove('darkmode');
document.cookie = "darkmode=0;path=/"
console.log('夜间模式关闭');
}
}
//22-7时间段自动启用
(function(){
if(document.cookie.replace(/(?:(?:^|.*;\s*)darkmode\s*\=\s*([^;]*).*$)|^.*$/, "$1") === ''){
if(new Date().getHours() > 22 || new Date().getHours() < 7){
document.body.classList.add('darkmode');
document.cookie = "darkmode=1;path=/";
console.log('夜间模式开启');
}else{
document.body.classList.remove('darkmode');
document.cookie = "darkmode=0;path=/";
console.log('夜间模式关闭');
}
}else{
var darkmode = document.cookie.replace(/(?:(?:^|.*;\s*)darkmode\s*\=\s*([^;]*).*$)|^.*$/, "$1") || '0';
if(darkmode == '0'){
document.body.classList.remove('darkmode');
}else if(darkmode == '1'){
document.body.classList.add('darkmode');
}
}
})();
页面头部调用深色css
<link rel="stylesheet" href="'.theurl.'css/darkmode.css" type="text/css" />
页脚调用js
<script type="text/javascript" src="'.theurl.'js/darkmode.js"></script>
新建darkmode.css文件
深色样式格式
.darkmode XXX
手动检查
<a href="javascript:switchdarkmodeMode()">Dark Mode</a>
Comments | NOTHING
该文章已经关闭评论