2023-01-13: 修改元宝index值(js代码内),默认999999会出现一些bug,设为1即可。
此教程基于butterfly主题,且最主要是提供一下代码。
代码本身不复杂,但是每个站的情况有所不同,例如有的站开启了pjax。所以需要有一些修改能力自行适配。。
效果图
- 倒计时大于一天会显示天数。
- 倒计时小于一天会显示时分秒进行倒计时。
- 如果已经过年了则显示新年快乐。
效果依次如下:
参考文档
- WordPress 全站飘元宝雨 – 红包雨
参考实现元宝飘落特效
- 兔圆圆2023跨年倒计时+史上最炫烟花秀,太美了吧
可爱的兔年背景图
就是从这里获取到的。感谢@Xlenco帮忙寻找素材,爱你♥。
教程
1. 添加侧边栏
路径:[Blogroot]\source\_data\widget.yml
建议查看butterfly官方文档:自定义侧边栏
1 2 3 4 5 6 7 8 9 10 11 12 13
|
top: - class_name: id_name: newYear name: icon: order: 1 html: '<div id="newYear-main"><div class="mask"></div> <p class="title"></p> <div class="newYear-time"></div> <p class="today" style="text-align: right;"></p> </div>'
|
2. 添加css
不会自定义css请查看:Hexo博客添加自定义css和js文件
因本人开启了防盗链,注意替换图中的https://cdn.leonus.cn/post/tunian3.webp
,下载下来放到自己图库即可。
自定义css中添加如下代码:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61
|
#newYear { color: white; padding: 0 !important; }
#newYear p, #newYear h3 { font-weight: normal; color: inherit; margin: 0; }
#newYear .item-headline { display: none; }
#newYear-main { min-height: 160px; padding: 1rem; position: relative; border-radius: 12px; background-image: url(https://cdn.leonus.cn/img/tunian3.webp); background-size: cover; background-position: center; }
#newYear-main * { position: relative; line-height: 1.3; }
#newYear-main .newYear-time { font-weight: bold; text-align: center; }
#newYear-main .time, #newYear-main .happyNewYear { font-size: 3.5rem; margin: 1rem 0; display: block; }
#newYear-main .day { font-size: 5rem; }
#newYear-main .day .unit { font-size: 1rem; }
#newYear-main .mask { position: absolute; left: 0; top: 0; height: 100%; width: 100%; background-color: rgba(0, 0, 0, .1); }
|
3. 引入JQuery和元宝库
在_config.butterfly.yml
的inject
下的bottom里添加如下代码:
1 2 3 4 5 6
| - <script src="https://lf3-cdn-tos.bytecdntp.com/cdn/expire-1-M/jquery/3.6.0/jquery.min.js" type="application/javascript"></script>
- <script src="https://cdn.wpon.cn/2022-sucai/Gold-ingot.js"></script>
- <script src="/js/xxx.js"></script>
|
3. 添加js
最后添加一下js:
因本人开启了防盗链,注意替换图中的['https://cdn.leonus.cn/img/yb1.webp', 'https://cdn.leonus.cn/img/yb2.webp', 'https://cdn.leonus.cn/img/yb3.webp']
,下载下来放到自己图库即可。
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61
| let newYearTimer = null; var newYear = () => { clearTimeout(newYearTimer); if (!document.querySelector('#newYear')) return; let newYear = new Date('2023-01-22 00:00:00').getTime() / 1000, week = { 0: '周日', 1: '周一', 2: '周二', 3: '周三', 4: '周四', 5: '周五', 6: '周六' }
time();
function nol(h) { return h > 9 ? h : '0' + h; };
function time() { let now = new Date();
document.querySelector('#newYear .today').innerHTML = now.getFullYear() + '-' + (now.getMonth() + 1) + '-' + now.getDate() + ' ' + week[now.getDay()]
let second = newYear - Math.round(now.getTime() / 1000);
if (second < 0) { document.querySelector('#newYear .title').innerHTML = 'Happy New Year!'; document.querySelector('#newYear .newYear-time').innerHTML = '<span class="happyNewYear">新年快乐</span>'; } else { document.querySelector('#newYear .title').innerHTML = '距离2023年春节:'
if (second > 86400) { document.querySelector('#newYear .newYear-time').innerHTML = `<span class="day">${Math.ceil(second / 86400)}<span class="unit">天</span></span>` } else { let h = nol(parseInt(second / 3600)); second %= 3600; let m = nol(parseInt(second / 60)); second %= 60; let s = nol(second); document.querySelector('#newYear .newYear-time').innerHTML = `<span class="time">${h}:${m}:${s}</span></span>`; newYearTimer = setTimeout(time, 1000); } } }
jQuery(document).ready(function($) { $('#newYear').wpSuperSnow({ flakes: ['https://cdn.leonus.cn/img/yb1.webp', 'https://cdn.leonus.cn/img/yb2.webp', 'https://cdn.leonus.cn/img/yb3.webp'], totalFlakes: '100', zIndex: '1', maxSize: '30', maxDuration: '20', useFlakeTrans: false }); }); } newYear();
|
最后
我记得好像就这么多,如果有问题的话欢迎反馈,我再进行修改。