主要看思路,自己增删改查,里面样式只针对我的博客
一.触发点击
二.修改按钮样式以及对于类名
三.将模式信息存入sessionStorage中
四.更具按钮的类名,自己博客样式进行跟换
<!--夜间模式点击事件js-->
<script >
let night_close = document.querySelector('.night-close')||document.querySelector('.night-open');
night_close.onclick = function () {
let button_classname = this.className;
button_classname == 'night-close' ? sessionStorage.model = 'night' : sessionStorage.model = 'sun'
button_classname == 'night-close' ? this.className = 'night-open' : this.className = 'night-close'
button_classname == 'night-close' ? this.innerHTML = '夜间模式:<span style="color:greenyellow">开启</span>' : this.innerHTML = '夜间模式:<span style="color:red">关闭</span>'
let body = document.querySelector('body');
let post = document.querySelector('.post');
let header = document.querySelector('#header');
let days = document.querySelectorAll('.day');
let tiankong = document.querySelector('.tiankong');
let calendar = document.querySelector('#blog-calendar');
//页首图片
button_classname == 'night-close' ? tiankong.style.display = 'none' : tiankong.style.display = 'block'
//整体颜色
button_classname == 'night-close' ? body.style.background = '#0B1226' : body.style.background = '#fff'
//日历
calendar.style.backgroundColor = '#ffffff'
//头部
button_classname == 'night-close' ? header.style.background = 'url(https://images.cnblogs.com/cnblogs_com/pythonywy/1516412/o_sleep2.png) no-repeat 898px -65px' : header.style.background = 'url(https://images.cnblogs.com/cnblogs_com/pythonywy/1455150/o_1kongbai.png)'
if (button_classname != 'night-close') {
header.style.backgroundSize = '100% 100%'
}
if (days) {
for (var days_index = 0, a = days.length; days_index < a; days_index++) {
days[days_index].style.backgroundColor = '#ffffff'
}
}
//正文
if (post) {
post.style.backgroundColor = '#ffffff'
}
};
</script>
考虑情况:
1.时间到了提醒是不是要切换
2.已经是夜间模式了就用切换
3.有些人不喜欢这些提示
这段代码解决这3中情况,内容自己理解
<!--夜间模式分支,根据时间通过session中存储的信息修改-->
<script>
var time_obj = new Date();
var time_hour = time_obj.getHours();
var night_model = sessionStorage.getItem('model');
var chiose = sessionStorage.getItem('chiose');
console.log(!chiose);
console.log(night_model != 'night');
console.log(parseInt(time_hour) >= 18 || parseInt(time_hour) <= 6);
if (!chiose) {
if (night_model != 'night') {
if (parseInt(time_hour) >= 18 || parseInt(time_hour) <= 6) {
if (confirm("晚上好(*/ω\*),辛苦啦~是否开启夜间模式?")) {
sessionStorage.model = 'night';
if (confirm('以后要不要提醒您呢')) {
sessionStorage.removeItem('chiose')
} else {
sessionStorage.chiose = 1
}
} else {
sessionStorage.model = 'sun';
if (confirm('以后要不要提醒您呢(づ ̄3 ̄)づ╭❤~')) {
sessionStorage.chiose.removeItem('chiose')
} else {
sessionStorage.chiose = 1
}
}
}
}
}
var time_obj = new Date();
var time_hour = time_obj.getHours();
var night_model = sessionStorage.getItem('model');
var chiose = sessionStorage.getItem('chiose');
console.log(!chiose);
console.log(night_model != 'night');
console.log(parseInt(time_hour) >= 18 && parseInt(time_hour) <= 6);
if (!chiose) {
if (night_model == 'night') {
if (parseInt(time_hour) < 18 && parseInt(time_hour) > 6) {
if (confirm("早上好(*/ω\*),昨晚辛苦啦( • ̀ω•́ )✧,昨晚开着夜间模式,早上我帮你关了,要不要保持这个状态,取消的话变回夜间模式")) {
sessionStorage.model = 'sun';
if (confirm('以后要不要提醒您呢(づ ̄3 ̄)づ╭❤~')) {
sessionStorage.removeItem('chiose')
} else {
sessionStorage.chiose = 1
}
} else {
sessionStorage.model = 'night';
if (confirm('以后要不要提醒您呢')) {
sessionStorage.chiose.removeItem('chiose')
} else {
sessionStorage.chiose = 1
}
}
}
}
}
</script>
为了解决问题:
1.跳转页面后要重复切换模式
2.提示后进行切换
注意点:这段代码必须放在时间控制后面,不然时间那段没啥用
<!--夜间模式分支,session中有night切换夜间模式-->
<script >
letmodel = sessionStorage.getItem('model')
if (model == 'night') {
let body_night = document.querySelector('body');
let post_night = document.querySelector('.post');
let header_night = document.querySelector('#header');
let days_night = document.querySelectorAll('.day');
let tiankong_night = document.querySelector('.tiankong');
let calendar_night = document.querySelector('#blog-calendar');
night_close.className = 'night-open'
night_close.innerHTML = '夜间模式:<span style="color:greenyellow">开启</span>'
tiankong_night.style.display = 'none'
body_night.style.background = '#0B1226'
calendar_night.style.backgroundColor = '#ffffff'
header_night.style.background = 'url(https://images.cnblogs.com/cnblogs_com/pythonywy/1516412/o_sleep2.png) no-repeat 898px -65px'
if (days_night) {
for (var days_index = 0, c = days_night.length; days_index < c; days_index++) {
console.log(days_night[days_index])
days_night[days_index].style.backgroundColor = '#ffffff'
}
if (post_night) {
post_night.style.backgroundColor = '#ffffff'
}
}
}
</script>
代码仅作为参考