0.设计原理
①给一系列的css属性起名字如dark表示黑夜,默认为失效不显示 ②页面上增加左侧主题切换按钮,按钮处理逻辑为将所有dark对应的css属性启用,并设置标记位
1.增加主题切换按钮及其js处理逻辑
位置:themes/sakura/layout/layout.ejs 这个文件会在所有页面生效
内容如下:(ps:多了个大红灯笼的div)
<!-- 8.增加红灯笼按钮-->
<!--深夜模式按钮-->
<a onclick="switchNightMode()" id="sma">
<i class="fa fa-moon-o" id="nightMode" aria-hidden="true"></i>
</a>
<!--圣诞模式按钮-->
<script>
if (localStorage.getItem('themechris') === '1') {
document.body.classList.add('themechris');
}
else if (matchMedia('(prefers-color-scheme: themechris)').matches) {
document.body.classList.add('themechris');
}
</script>
<a onclick="switchChrisThemes()" id="switchchristmas">
<img class="theme-buttonchrismas" src="https://hesifan.top/medias/christmas-img/ball.jpg" title="圣诞模式">
</a>
<!--春节模式按钮-->
<script>
if (localStorage.getItem('themespring') === '1') {
document.body.classList.add('themespring');
}
else if (matchMedia('(prefers-color-scheme: themespring)').matches) {
document.body.classList.add('themespring');
}
</script>
<div onclick="switchSpringThemes()" id="switchspring">
<img class="theme-button" src="https://hesifan.top/medias/spring-img/denglong.png" title="春节模式">
</div>
<!--春节主题的大红灯笼-->
<div class="denglong">
<div class="deng-box">
<div class="deng">
<div class="xian"></div>
<div class="deng-a">
<div class="deng-b"><div class="deng-t">喜迎</div></div>
</div>
<div class="shui shui-a"><div class="shui-c"></div><div class="shui-b"></div></div>
</div>
</div>
<div class="deng-box1">
<div class="deng">
<div class="xian"></div>
<div class="deng-a">
<div class="deng-b"><div class="deng-t">元旦</div></div>
</div>
<div class="shui shui-a"><div class="shui-c"></div><div class="shui-b"></div></div>
</div>
</div>
</div>
<!--春节主题大红灯笼-->
<script>
// 切换深色模式JS
function switchNightMode() {
var body = document.body;
if(body.classList.contains('dark')){
document.body.classList.remove('dark');
localStorage.setItem('dark','0');
$('#nightMode').removeClass("fa-lightbulb-o").addClass("fa-moon-o");
return;
} else {
document.body.classList.add('dark');
localStorage.setItem('dark','1');
$('#nightMode').removeClass("fa-moon-o").addClass("fa-lightbulb-o");
return;
}
}
// 中国节日按钮切换
function switchSpringThemes() {
var body = document.body;
if(body.classList.contains('themechris')){
document.body.classList.remove('themechris');
localStorage.setItem('themechris','0');
document.body.classList.add('themespring');
localStorage.setItem('themespring','1');
return;
}
if(body.classList.contains('themespring')){
document.body.classList.remove('themespring');
localStorage.setItem('themespring','0');
return;
} else {
document.body.classList.add('themespring');
localStorage.setItem('themespring','1');
return;
}
};
// 圣诞节按钮切换
function switchChrisThemes() {
var body = document.body;
if(body.classList.contains('themespring')){
document.body.classList.remove('themespring');
localStorage.setItem('themespring','0');
document.body.classList.add('themechris');
localStorage.setItem('themechris','1');
return;
}
if(body.classList.contains('themechris')){
document.body.classList.remove('themechris');
localStorage.setItem('themechris','0');
return;
} else {
document.body.classList.add('themechris');
localStorage.setItem('themechris','1');
return;
}
};
</script>
2.增加对应主题的css样式和按钮的css样式
位置:thems/sakura/source/css/style.css
内容如下:
/*节日切换按钮*/
/* 深色模式按钮设置 */
#sma {
background: #000;
width: 38px;
height: 38px;
display: block;
position: fixed;
border-radius: 50%;
right: 15px;
bottom: 250px;
padding-top: 15px;
margin-bottom: 0;
z-index: 998;
cursor: pointer;
}
#sma .fa-moon-o {
position: absolute;
right: 8px;
bottom: 8px;
font-size: 1.48rem!important; }
#sma .fa-lightbulb-o {
position: absolute;
right: 12px;
bottom: 3px;
font-size: 1.8rem!important;
.fa-moon-o:before { content: "\f186"; }
.fa-comments:before { content: "\f086"; }
/*深夜模式按钮样式结束*/
/*圣诞、春节按钮*/
#switchspring {
background: #dc8f03;
width: 38px;
height: 38px;
display: block;
position: fixed;
border-radius: 50%;
right: 15px;
bottom: 295px;
padding-top: 15px;
margin-bottom: 0;
z-index: 998;
cursor: pointer;
}
#switchchristmas {
background: #FFF;
width: 38px;
height: 38px;
display: block;
position: fixed;
border-radius: 50%;
right: 15px;
bottom: 340px;
padding-top: 15px;
margin-bottom: 0;
z-index: 998;
cursor: pointer;
}
.theme-button {
width: 38px;
height: 38px;
border-radius: 50%;
position: absolute;
bottom: 0px;
}
.theme-buttonchrismas {
width: 38px;
height: 38px;
border-radius: 50%;
position: absolute;
bottom: 0px;
right: 1.7px
}
/*圣诞、春节按钮样式结束*/
/*深夜模式背景样式*/
/*字体变灰白*/
body.dark .fas,
body.dark .title,
body.dark .row .text,
body.dark article .article-content .summary,
body.dark .card .card-image .card-title,
body.dark .fa-moon-o:before,
body.dark .fa-lightbulb-o:before,
body.dark article .article-tags .chip,
body.dark .chip-container .tag-title,
body.dark div.jqcloud a,
body.dark .friends-container .tag-title,
body.dark .frind-ship .title h1,
body.dark .card .card-content p,
body.dark .card .card-content .dss,
body.dark .v[data-class=v] .vcount,
body.dark .v[data-class=v] .vcount .vnum,
body.dark pre code,
body.dark h1,
body.dark h2,
body.dark h3,
body.dark h4,
body.dark h5,
body.dark h6,
body.dark li,
body.dark p,
body.dark header
.side-nav
.mobile-head
.logo-name,
body.dark
header .side-nav .mobile-head .logo-desc,
body.dark header .side-nav .menu-list a,
body.dark .bg-cover .post-title,
body.dark a { color: rgba(255, 255, 255, 0.6); }
/* 背景颜色变灰色 */
body.dark .card, body.dark .block-with-text:after { background-color: #282c34; }
/* 背景颜色变黑色 */
body.dark, body.dark .v[data-class=v] .vcount, body.dark #rewardModal .modal-content, body.dark .modal, body.dark header .side-nav, body.dark header .side-nav .menu-list .m-nav-show { background-color: #12121c; }
/* 改变透明度*/
body.dark .aplayer{background: #2f3742!important;} body.dark img, body.dark strong { filter: brightness(.7); }
/*深夜模式背景样式结束*/*
/*春节模式*/
/*节日主题 挂件åŠçº¿*/
/*春节*/
.caishenxianleft {
left: 16%!important;
position: absolute;
top: 0px;
width: 2px;
height: 20px;
background: #dc8f03;
transform-origin: 50% -100px;
animation: swing 3.5s infinite ease-in-out;
-webkit-transform-origin: 50% -100px;
-webkit-animation: swing 3.5s infinite ease-in-out;
}
.caishenxian {
position: absolute;
top: 0px;
right: 20%;
width: 2px;
height: 20px;
background: #dc8f03;
transform-origin: 50% -100px;
animation: swing 3.5s infinite ease-in-out;
-webkit-transform-origin: 50% -100px;
-webkit-animation: swing 3.5s infinite ease-in-out;
}
.guguxian {
position: absolute;
top: 0px;
left: 20%;
width: 2px;
height: 20px;
background: #dc8f03;
transform-origin: 50% -100px;
animation: swing 3.5s infinite ease-in-out;
-webkit-transform-origin: 50% -100px;
-webkit-animation: swing 3.5s infinite ease-in-out;
}
.wenzhangxian {
left: 50%!important;
z-index: 998;
}
/*春节主题 挂绳吊线*/
.caishenxianleftsd {
left: 16%!important;
position: absolute;
top: 0px;
width: 2px;
height: 20px;
background:#5be4db;
transform-origin: 50% -100px;
animation: swing 3.5s infinite ease-in-out;
-webkit-transform-origin: 50% -100px;
-webkit-animation: swing 3.5s infinite ease-in-out;
}
.caishenxiansd {
position: absolute;
top: 0px;
right: 20%;
width: 2px;
height: 20px;
background:#5be4db;
transform-origin: 50% -100px;
animation: swing 3.5s infinite ease-in-out;
-webkit-transform-origin: 50% -100px;
-webkit-animation: swing 3.5s infinite ease-in-out;
}
.guguxiansd {
position: absolute;
top: 0px;
left: 20%;
width: 2px;
height: 20px;
background: #5be4db;
transform-origin: 50% -100px;
animation: swing 3.5s infinite ease-in-out;
-webkit-transform-origin: 50% -100px;
-webkit-animation: swing 3.5s infinite ease-in-out;
}
.wenzhangxiansd {
left: 50%!important;
z-index: 998;
}
/*春节主题 挂件*/
.fudai {
top: 14px!important;
left: -22px!important;
}
.caishenpic {
width: 48px;
height: 48px;
max-width: 48px; //这个参数是为了覆盖763\963行的img css样式
border-radius: 50%;
position: absolute;
top: 12px;
right: -22px;
z-index: 9;
/* transform-origin: 50% -100px; */
animation: swing 3.5s infinite ease-in-out;
/* -webkit-transform-origin: 50% -100px; */
-webkit-animation: swing 3.5s infinite ease-in-out;
}
.lala {
width: 32px!important;
height: 32px!important;
opacity: 1!important;
left: -4px!important;
top: 14px!important;
}
.chunp {
width: 216px!important;
height: 96px!important;
left: -80px!important;
top: 6px!important;
}
.fudao {
width: 38px;
height: 38px;
top: 18px!important;
left: -19px!important;
}
.lions {
width: 32px!important;
height: 32px!important;
opacity: 1!important;
left: -15px!important;
top: 14px!important;
}
.fenleiss {
position: absolute;
top: -64px;
left: 20%;
width: 2px;
height: 20px;
background: #dc8f03;
transform-origin: 50% -100px;
animation: swing 3.5s infinite ease-in-out;
-webkit-transform-origin: 50% -100px;
-webkit-animation: swing 3.5s infinite ease-in-out;
}
.gugus {
width: 32px!important;
height: 32px!important;
opacity: 1!important;
left: -15px!important;
top: 9px!important;
}
.huadengpic {
width: 40px!important;
height: 40px!important;
opacity: 1!important;
left: -18px!important;
top: 9px!important;
}
.shanzipic {
width: 40px!important;
height: 40px!important;
opacity: 1!important;
left: -18px!important;
top: 18px!important;
}
/*圣诞小挂件图片*/
.caishenpicsd {
width: 48px;
height: 48px;
max-width: 48px; // 用于覆盖763\963等行的img css样式
border-radius: 50%;
position: absolute;
top: 12px;
right: -22px;
z-index: 9;
/* transform-origin: 50% -100px; */
animation: swing 3.5s infinite ease-in-out;
/* -webkit-transform-origin: 50% -100px; */
-webkit-animation: swing 3.5s infinite ease-in-out;
}
/*主题 帽å*/
.labelmz {
width: 34px;
height: 34px;
top: 7px;
right: -84px;
transform: rotate(40deg);
position: absolute;
z-index: 99;
-webkit-transform-origin: 50% -100px;
-webkit-animation: mzanimate 5s infinite ease-in-out;
}
.weilemz {
position: relative;
}
.friendmz {
width: 50px!important;
height: 50px!important;
position: absolute;
top: 8px;
right: -90px;
transform: rotate(44deg);
position: absolute;
z-index: 99;
-webkit-transform-origin: 50% -100px;
-webkit-animation: mzanimate 1.2s infinite ease-in-out;
}
.xiaomaozitx {
width: 60px;
height: 60px;
position: absolute;
top: -127px;
right: 25%;
transform: rotate(40deg);
position: absolute;
z-index: 99;
-webkit-animation: mzanimate 1.2s infinite ease-in-out;
}
.labelmzsd {
width: 34px;
height: 34px;
top: 10px;
right: -95px;
transform: rotate(40deg);
position: absolute;
z-index: 99;
-webkit-transform-origin: 50% -100px;
-webkit-animation: mzanimate 5s infinite ease-in-out;
}
/* 点击显示或隐藏总样式开始 */
.caishendeng,
.xiaomaozitx,
.labelmz,
.friendmz,
.caishendengsd,
.labelmzsd
{
display: none;
}
body.themespring .caishendeng,
body.themespring .xiaomaozitx,
body.themespring .labelmz,
body.themespring .friendmz,
body.themechris .caishendengsd,
body.themechris .labelmzsd
{
display: block;
}
/* 点击显示或隐藏总样式结束 */
.toupic {
position: relative;
}
.denglong {
display: none;
}
body.themespring .denglong {
display: block;
}
@media only screen and (min-width: 1124px) {
.nav-menu {
padding-right: 0px;
}
body.themespring .nav-menu {
padding-right: 96px;
}
}
@media only screen and (max-width: 760px) {
.denglong .deng-box ,.denglong .deng-box1{
display: none;
}
body.themespring .right {
float: left!important;
}
}
@media only screen and (min-width: 768px) and (max-width: 1024px) {
.right {
float: left!important;
}
}
/*春节主题 ç¯ç¬¼*/
.deng-box {
position: fixed;
top: -40px;
right: -20px;
z-index: 999;
}
.deng-box1 {
position: fixed;
top: -30px;
right: 10px;
z-index: 999;
}
.deng-box1 .deng {
position: relative;
width: 120px;
height: 90px;
margin: 50px;
background: #d8000f;
background: rgba(216, 0, 15, 0.8);
border-radius: 50% 50%;
-webkit-transform-origin: 50% -100px;
-webkit-animation: swing 5s infinite ease-in-out;
box-shadow: -5px 5px 30px 4px rgba(252, 144, 61, 1);
}
.deng {
position: relative;
width: 120px;
height: 90px;
margin: 50px;
background: #d8000f;
background: rgba(216, 0, 15, 0.8);
border-radius: 50% 50%;
-webkit-transform-origin: 50% -100px;
-webkit-animation: swing 3s infinite ease-in-out;
box-shadow: -5px 5px 50px 4px rgba(250, 108, 0, 1);
}
.deng-a {
width: 100px;
height: 90px;
background: #d8000f;
background: rgba(216, 0, 15, 0.1);
margin: 12px 8px 8px 10px;
border-radius: 50% 50%;
border: 2px solid #dc8f03;
}
.deng-b {
width: 45px;
height: 90px;
background: #d8000f;
background: rgba(216, 0, 15, 0.1);
margin: -4px 8px 8px 26px;
border-radius: 50% 50%;
border: 2px solid #dc8f03;
}
.xian {
position: absolute;
top: -20px;
left: 60px;
width: 2px;
height: 20px;
background: #dc8f03;
}
.shui-a {
position: relative;
width: 5px;
height: 20px;
margin: -5px 0 0 59px;
-webkit-animation: swing 4s infinite ease-in-out;
-webkit-transform-origin: 50% -45px;
background: #ffa500;
border-radius: 0 0 5px 5px;
}
.shui-b {
position: absolute;
top: 14px;
left: -2px;
width: 10px;
height: 10px;
background: #dc8f03;
border-radius: 50%;
}
.shui-c {
position: absolute;
top: 18px;
left: -2px;
width: 10px;
height: 35px;
background: #ffa500;
border-radius: 0 0 0 5px;
}
.deng:before {
position: absolute;
top: -7px;
left: 29px;
height: 12px;
width: 60px;
content: " ";
display: block;
z-index: 999;
border-radius: 5px 5px 0 0;
border: solid 1px #dc8f03;
background: #ffa500;
background: linear-gradient(to right, #dc8f03, #ffa500, #dc8f03, #ffa500, #dc8f03);
}
.deng:after {
position: absolute;
bottom: -7px;
left: 10px;
height: 12px;
width: 60px;
content: " ";
display: block;
margin-left: 20px;
border-radius: 0 0 5px 5px;
border: solid 1px #dc8f03;
background: #ffa500;
background: linear-gradient(to right, #dc8f03, #ffa500, #dc8f03, #ffa500, #dc8f03);
}
.deng-t {
font-family: åŽæ–‡è¡Œæ¥·;
font-size: 26px;
color: #dc8f03;
font-weight: bold;
line-height: 44px;
text-align: center;
}
.night .deng-t,
.night .deng-box,
.night .deng-box1 {
background: transparent !important;
}
@-moz-keyframes swing {
0% {
-moz-transform: rotate(-10deg)
}
50% {
-moz-transform: rotate(10deg)
}
100% {
-moz-transform: rotate(-10deg)
}
}
@-webkit-keyframes swing {
0% {
-webkit-transform: rotate(-10deg)
}
50% {
-webkit-transform: rotate(10deg)
}
100% {
-webkit-transform: rotate(-10deg)
}
}
/*春节模式结束*/
3.在需要的位置加上挂件
当我们点击切换按钮时就会显示对应模式下的挂件,//挂件图片可自己替换
挂件示例:
<!--可以在你想要展示小挂件的地方加上这个挂件,还没有完善等之后再进行吧-->
<!--圣诞主题小挂件-->
<div class="caishendengsd">
<div class="caishenxiansd">
<img class="caishenpicsd" src="https://hesifan.top/medias/christmas-img/2.png">
</div>
</div>
<!-圣诞主题小挂件结束-->
<!--春节主题小挂件-->
<div class="caishendeng">
<div class="caishenxianleft">
<img class="caishenpic fudai" src="https://hesifan.top/medias/spring-img/3.png">
</div>
</div>
<!-春节主题小挂件结束-->