前序

忘了在哪看到的这个东西,总之看到了一次就忘不掉了
然后就拿我那差的要命的技术写了个这

屎山这个东西还没到这个程度

建议你先将文章过一遍,这样可以更清楚的知道自己下一步要干什么。

这东东貌似有小BUG

%ThemeSource%
位于~\themes\Butterfly\source\
%Source%
位于~\source\
%ThemeLayout%
位于~\themes\Butterfly\layout\

预览

页面正常的话,标题后面不就是的吗?

单独页面

https://blog.admincmd.xyz/favi/

特性

  • 支持夜间模式,需要 HTML 标签
  • 支持自定义每行精度

引入

引入 JS

这里的代码有可以自己启动、循环的形式,适用于整个页面没有主循环控制器的版本
也有需要在每次循环/触发的形式,适用于有主循环控制器的版本

请根据自己的页面作选择,如果不知道选那种,请选择前者。

代码块单号的是已压缩的代码,多行的是原带注释代码。

适用于自启动、自循环

这个代码随便找个能在页面加载时运行的位置扔着就行,不过建议找个地方放好,以后别忘了( ̄y▽, ̄)╭

1
let UPDATE_PROGRESS_BARS_INIT=false;function updateProgressBars(){try{let now=new Date();if(UPDATE_PROGRESS_BARS_INIT===false){for(let i=0;i<document.getElementsByClassName('time-flies').length;i++){let length=document.getElementsByClassName('time-flies')[i];length.innerHTML=`<div class="progress-container"><div class="progress-label">今年已经过了<span class="year-progress"></span></div><div class="progress-bar"><div class="year-progress-bar"><span class="year-progress-bar-fill"></span></div></div></div><div class="progress-container"><div class="progress-label">这个月过去了<span class="month-progress"></span></div><div class="progress-bar"><div class="month-progress-bar"></div></div></div><div class="progress-container"><div class="progress-label">今天过去了<span class="day-progress"></span></div><div class="progress-bar">div class="day-progress-bar"></div></div></div><div class="progress-container"><div class="progress-label">这一个小时过了<span class="hour-progress"></span></div><div class="progress-bar"><div class="hour-progress-bar"></div></div></div><div class="progress-container"><div class="progress-label">本分钟过了<span class="minute-progress"></span></div><div class="progress-bar"><div class="minute-progress-bar"></div></div></div><p>珍惜时间,时光飞逝。</p>`}UPDATE_PROGRESS_BARS_INIT=false}const yearStart=new Date(now.getFullYear(),0,1).getTime();const yearEnd=new Date(now.getFullYear()+1,0,1).getTime();const yearProgress=((now.getTime()-yearStart)/(yearEnd-yearStart))*100;const monthStart=new Date(now.getFullYear(),now.getMonth(),1).getTime();const monthEnd=new Date(now.getFullYear(),now.getMonth()+1,1).getTime();const monthProgress=((now.getTime()-monthStart)/(monthEnd-monthStart))*100;const dayStart=new Date(now.getFullYear(),now.getMonth(),now.getDate()).getTime();const dayEnd=new Date(now.getFullYear(),now.getMonth(),now.getDate()+1).getTime();const dayProgress=((now.getTime()-dayStart)/(dayEnd-dayStart))*100;const hourStart=new Date(now.getFullYear(),now.getMonth(),now.getDate(),now.getHours()).getTime();const hourEnd=new Date(now.getFullYear(),now.getMonth(),now.getDate(),now.getHours()+1).getTime();const hourProgress=((now.getTime()-hourStart)/(hourEnd-hourStart))*100;const minuteProgress=(now.getSeconds()*1000+now.getMilliseconds())/1000/60*100;updateDisplay('year',yearProgress,7);updateDisplay('month',monthProgress,6);updateDisplay('day',dayProgress,5);updateDisplay('hour',hourProgress,3);updateDisplay('minute',minuteProgress,2)}catch(error){console.error('更新模块:时光飞逝 时发生错误:',error)}function updateDisplay(period,progress,decimalPlaces){let lengthDiv=document.getElementsByClassName('time-flies');let lengthProgress=document.getElementsByClassName(`${period}-progress`);let lengthProgressBar=document.getElementsByClassName(`${period}-progress-bar`);for(let i=0;i<lengthDiv.length;i++){lengthProgress[i].textContent=progress.toFixed(decimalPlaces)+'%';lengthProgressBar[i].style.width=progress.toFixed(decimalPlaces)+'%'}}}setInterval(updateProgressBars,1000);
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
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
let UPDATE_PROGRESS_BARS_INIT = false;
function updateProgressBars() {
try {
let now = new Date();
if (UPDATE_PROGRESS_BARS_INIT === false) {// 初始化
for (let i = 0; i < document.getElementsByClassName('time-flies').length; i++) {
let length = document.getElementsByClassName('time-flies')[i];
length.innerHTML = `
<div class="progress-container">
<div class="progress-label">今年已经过了 <span class="year-progress"></span></div>
<div class="progress-bar">
<div class="year-progress-bar"><span class="year-progress-bar-fill"></span></div>
</div>
</div>
<div class="progress-container">
<div class="progress-label">这个月过去了 <span class="month-progress"></span> </div>
<div class="progress-bar">
<div class="month-progress-bar"></div>
</div>
</div>
<div class="progress-container">
<div class="progress-label"> 今天过去了 <span class="day-progress"></span> </div>
<div class="progress-bar">
<div class="day-progress-bar"></div>
</div>
</div>
<div class="progress-container">
<div class="progress-label"> 这一个小时过了 <span class="hour-progress"></span> </div>
<div class="progress-bar">
<div class="hour-progress-bar"></div>
</div>
</div>
<div class="progress-container">
<div class="progress-label">本分钟过了 <span class="minute-progress"></span></div>
<div class="progress-bar">
<div class="minute-progress-bar"></div>
</div>
</div>
<p>珍惜时间,时光飞逝。</p>`;
}
UPDATE_PROGRESS_BARS_INIT = true;
}

const yearStart = new Date(now.getFullYear(), 0, 1).getTime(); // 计算这个时间单位的起始位置
const yearEnd = new Date(now.getFullYear() + 1, 0, 1).getTime(); // 计算这个时间单位的终止位置
const yearProgress = ((now.getTime() - yearStart) / (yearEnd - yearStart)) * 100; // 然后将当前时间与起始位置的差值除以终止位置与起始位置的差值,得到百分比

const monthStart = new Date(now.getFullYear(), now.getMonth(), 1).getTime();
const monthEnd = new Date(now.getFullYear(), now.getMonth() + 1, 1).getTime();
const monthProgress = ((now.getTime() - monthStart) / (monthEnd - monthStart)) * 100;

const dayStart = new Date(now.getFullYear(), now.getMonth(), now.getDate()).getTime();
const dayEnd = new Date(now.getFullYear(), now.getMonth(), now.getDate() + 1).getTime();
const dayProgress = ((now.getTime() - dayStart) / (dayEnd - dayStart)) * 100;

const hourStart = new Date(now.getFullYear(), now.getMonth(), now.getDate(), now.getHours()).getTime();
const hourEnd = new Date(now.getFullYear(), now.getMonth(), now.getDate(), now.getHours() + 1).getTime();
const hourProgress = ((now.getTime() - hourStart) / (hourEnd - hourStart)) * 100; // 上面均通过计算当前时间与起始位置的差值除以起始位置与终止位置的差值得到百分比

const minuteProgress = (
now.getSeconds() * 1000 +
now.getMilliseconds()
) / 1000 / 60 * 100; // 计算一个分钟已过秒数,精确到毫秒,除以60,乘以100,得到百分比

// 更新进度条和文本显示
updateDisplay('year', yearProgress, 7);
updateDisplay('month', monthProgress, 6);
updateDisplay('day', dayProgress, 5);
updateDisplay('hour', hourProgress, 3);
updateDisplay('minute', minuteProgress, 2);

} catch (error) {
console.error('更新模块:时光飞逝 时发生错误:', error);
}

// 更新显示函数
function updateDisplay(period, progress, decimalPlaces) {
// 进度条文本,值,精度(小数点后 x 位)
let lengthDiv = document.getElementsByClassName('time-flies');
let lengthProgress = document.getElementsByClassName(`${period}-progress`);
let lengthProgressBar = document.getElementsByClassName(`${period}-progress-bar`);
for (let i = 0; i < lengthDiv.length; i++) {
lengthProgress[i].textContent = progress.toFixed(decimalPlaces) + '%';
lengthProgressBar[i].style.width = progress.toFixed(decimalPlaces) + '%';
}
}
}
setInterval(updateProgressBars, 1000);// 每秒更新一次

主循环的附属

此方式需要你手动将主函数updateProgressBars引入主循环的更新运行块中

有关主循环的

本站的主 JS 是 i.js,在浏览器控制台中可以找到主循环启动的输出,单击索引便可以看到本站的主循环模块。

1
let UPDATE_PROGRESS_BARS_INIT=false;function updateProgressBars(){try{let now=new Date();if(UPDATE_PROGRESS_BARS_INIT===false){for(let i=0;i<document.getElementsByClassName('time-flies').length;i++){let length=document.getElementsByClassName('time-flies')[i];length.innerHTML=`<div class="progress-container"><div class="progress-label">今年已经过了<span class="year-progress"></span></div><div class="progress-bar"><div class="year-progress-bar"><span class="year-progress-bar-fill"></span></div></div></div><div class="progress-container"><div class="progress-label">这个月过去了<span class="month-progress"></span></div><div class="progress-bar"><div class="month-progress-bar"></div></div></div><div class="progress-container"><div class="progress-label">今天过去了<span class="day-progress"></span></div><div class="progress-bar"><div class="day-progress-bar"></div></div></div><div class="progress-container"><div class="progress-label">这一个小时过了<span class="hour-progress"></span></div><div class="progress-bar"><div class="hour-progress-bar"></div></div></div><div class="progress-container"><div class="progress-label">本分钟过了<span class="minute-progress"></span></div><div class="progress-bar"><div class="minute-progress-bar"></div></div></div><p>珍惜时间,时光飞逝。</p>`}UPDATE_PROGRESS_BARS_INIT=false}const yearStart=new Date(now.getFullYear(),0,1).getTime();const yearEnd=new Date(now.getFullYear()+1,0,1).getTime();const yearProgress=((now.getTime()-yearStart)/(yearEnd-yearStart))*100;const monthStart=new Date(now.getFullYear(),now.getMonth(),1).getTime();const monthEnd=new Date(now.getFullYear(),now.getMonth()+1,1).getTime();const monthProgress=((now.getTime()-monthStart)/(monthEnd-monthStart))*100;const dayStart=new Date(now.getFullYear(),now.getMonth(),now.getDate()).getTime();const dayEnd=new Date(now.getFullYear(),now.getMonth(),now.getDate()+1).getTime();const dayProgress=((now.getTime()-dayStart)/(dayEnd-dayStart))*100;const hourStart=new Date(now.getFullYear(),now.getMonth(),now.getDate(),now.getHours()).getTime();const hourEnd=new Date(now.getFullYear(),now.getMonth(),now.getDate(),now.getHours()+1).getTime();const hourProgress=((now.getTime()-hourStart)/(hourEnd-hourStart))*100;const minuteProgress=(now.getSeconds()*1000+now.getMilliseconds())/1000/60*100;a('year',yearProgress,7);a('month',monthProgress,6);a('day',dayProgress,5);a('hour',hourProgress,3);a('minute',minuteProgress,2)}catch(error){console.error('更新模块:时光飞逝 时发生错误:',error)}function a(c,b,d){let lengthDiv=document.getElementsByClassName('time-flies');let lengthProgress=document.getElementsByClassName(`${c}-b`);let lengthProgressBar=document.getElementsByClassName(`${c}-b-bar`);for(let i=0;i<lengthDiv.length;i++){lengthProgress[i].textContent=b.toFixed(d)+'%';lengthProgressBar[i].style.width=b.toFixed(d)+'%'}}}
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
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
let UPDATE_PROGRESS_BARS_INIT = false;
function updateProgressBars() {
try {
let now = new Date();
if (UPDATE_PROGRESS_BARS_INIT === false) {// 初始化
for (let i = 0; i < document.getElementsByClassName('time-flies').length; i++) {
let length = document.getElementsByClassName('time-flies')[i];
length.innerHTML = `
<div class="progress-container">
<div class="progress-label">今年已经过了 <span class="year-progress"></span></div>
<div class="progress-bar">
<div class="year-progress-bar"><span class="year-progress-bar-fill"></span></div>
</div>
</div>
<div class="progress-container">
<div class="progress-label">这个月过去了 <span class="month-progress"></span> </div>
<div class="progress-bar">
<div class="month-progress-bar"></div>
</div>
</div>
<div class="progress-container">
<div class="progress-label"> 今天过去了 <span class="day-progress"></span> </div>
<div class="progress-bar">
<div class="day-progress-bar"></div>
</div>
</div>
<div class="progress-container">
<div class="progress-label"> 这一个小时过了 <span class="hour-progress"></span> </div>
<div class="progress-bar">
<div class="hour-progress-bar"></div>
</div>
</div>
<div class="progress-container">
<div class="progress-label">本分钟过了 <span class="minute-progress"></span></div>
<div class="progress-bar">
<div class="minute-progress-bar"></div>
</div>
</div>
<p>珍惜时间,时光飞逝。</p>
`;
}
UPDATE_PROGRESS_BARS_INIT = false;
}

const yearStart = new Date(now.getFullYear(), 0, 1).getTime(); // 计算这个时间单位的起始位置
const yearEnd = new Date(now.getFullYear() + 1, 0, 1).getTime(); // 计算这个时间单位的终止位置
const yearProgress = ((now.getTime() - yearStart) / (yearEnd - yearStart)) * 100; // 然后将当前时间与起始位置的差值除以终止位置与起始位置的差值,得到百分比

const monthStart = new Date(now.getFullYear(), now.getMonth(), 1).getTime();
const monthEnd = new Date(now.getFullYear(), now.getMonth() + 1, 1).getTime();
const monthProgress = ((now.getTime() - monthStart) / (monthEnd - monthStart)) * 100;

const dayStart = new Date(now.getFullYear(), now.getMonth(), now.getDate()).getTime();
const dayEnd = new Date(now.getFullYear(), now.getMonth(), now.getDate() + 1).getTime();
const dayProgress = ((now.getTime() - dayStart) / (dayEnd - dayStart)) * 100;

const hourStart = new Date(now.getFullYear(), now.getMonth(), now.getDate(), now.getHours()).getTime();
const hourEnd = new Date(now.getFullYear(), now.getMonth(), now.getDate(), now.getHours() + 1).getTime();
const hourProgress = ((now.getTime() - hourStart) / (hourEnd - hourStart)) * 100; // 上面均通过计算当前时间与起始位置的差值除以起始位置与终止位置的差值得到百分比

const minuteProgress = (
now.getSeconds() * 1000 +
now.getMilliseconds()
) / 1000 / 60 * 100; // 计算一个分钟已过秒数,精确到毫秒,除以60,乘100,得到百分比

// 更新进度条和文本显示
updateDisplay('year', yearProgress, 7);
updateDisplay('month', monthProgress, 6);
updateDisplay('day', dayProgress, 5);
updateDisplay('hour', hourProgress, 3);
updateDisplay('minute', minuteProgress, 2);

} catch (error) {
console.error('更新模块:时光飞逝 时发生错误:', error);
}

// 更新显示函数
function updateDisplay(period, progress, decimalPlaces) {
// 进度条文本,值,精度(小时点后 x 位)
let lengthDiv = document.getElementsByClassName('time-flies');
let lengthProgress = document.getElementsByClassName(`${period}-progress`);
let lengthProgressBar = document.getElementsByClassName(`${period}-progress-bar`);
for (let i = 0; i < lengthDiv.length; i++) {
lengthProgress[i].textContent = progress.toFixed(decimalPlaces) + '%';
lengthProgressBar[i].style.width = progress.toFixed(decimalPlaces) + '%';
}
}
}

引入 CSS

放在合适的位置即可

1
.time-flies .progress-container{margin-bottom:10px;}.time-flies .progress-label{font-size:14px;   margin-bottom:5px;}.time-flies .year-progress-bar,.time-flies .month-progress-bar,.time-flies .day-progress-bar,.time-flies .hour-progress-bar,.time-flies .minute-progress-bar{   width:100%;   color:hsla(0,0%,31%,0.24);   border-radius:5px;   overflow:hidden;text-align:center;line-height:20px;transition:width 0.5s;height:12px;}.time-flies .year-progress-bar,.time-flies .month-progress-bar,.time-flies .day-progress-bar,.time-flies .hour-progress-bar,.time-flies .minute-progress-bar{   content:"";   position:absolute;background-image:linear-gradient(-45deg,rgba(255,255,255,0.2) 25%,transparent 25%,transparent 50%,rgba(255,255,255,0.2) 50%,rgba(255,255,255,0.2) 75%,transparent 75%,transparent);z-index:1;background-size:50px 50px;animation:move 5s linear infinite;border-top-right-radius:8px;border-bottom-right-radius:8px;border-top-left-radius:20px;border-bottom-left-radius:20px;   overflow:hidden;}.time-flies .year-progress-bar{background-color:#4caf50;}.time-flies .month-progress-bar{background-color:#4caf50;}.time-flies .day-progress-bar{background-color:#4caf50;}.time-flies .hour-progress-bar{background-color:#4caf50;}.time-flies .minute-progress-bar{background-color:#4caf50;}html[data-theme="dark"] .time-flies .year-progress-bar{background-color:#2e8330;}html[data-theme="dark"] .time-flies .month-progress-bar{background-color:#2e8330;}html[data-theme="dark"] .time-flies .day-progress-bar{background-color:#2e8330;}html[data-theme="dark"] .time-flies .hour-progress-bar{background-color:#2e8330;}html[data-theme="dark"] .time-flies .minute-progress-bar{background-color:#2e8330;#4caf50;}.time-flies .progress-bar{width:100%;background-color:$color-theme-light-2background;#dddborder-radius:5px;overflow:hidden;}html[data-theme="dark"] .time-flies .progress-bar{background-color:$color-theme-dark-2background}@keyframes move{0%{background-position:0 0;}100%{background-position:50px 50px;}}@-webkit-keyframes move{0%{background-position:0 0;}100%{background-position:50px 50px;}}
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
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
/* 时光飞逝 Home -*/
.time-flies .progress-container {
margin-bottom: 10px;
}
.time-flies .progress-label {
font-size: 14px;
margin-bottom: 5px;
}
.time-flies .year-progress-bar,.time-flies .month-progress-bar,.time-flies .day-progress-bar,.time-flies .hour-progress-bar,.time-flies .minute-progress-bar {
width: 100%;
color: hsla(0, 0%, 31%, 0.24); /* 进度条背景色 */
border-radius: 5px;/*圆角*/
overflow: hidden;
text-align: center;
line-height: 20px;
transition: width 0.5s;/*一次到目标位置的速度*/
height: 12px;/*进度条高度*/
}

.time-flies .year-progress-bar, .time-flies .month-progress-bar, .time-flies .day-progress-bar, .time-flies .hour-progress-bar, .time-flies .minute-progress-bar
{
content: "";
position: absolute;
background-image: linear-gradient(
-45deg,
rgba(255, 255, 255, 0.2) 25%,
transparent 25%,
transparent 50%,
rgba(255, 255, 255, 0.2) 50%,
rgba(255, 255, 255, 0.2) 75%,
transparent 75%,
transparent
);
z-index: 1;
background-size: 50px 50px;
animation: move 5s linear infinite;
border-top-right-radius: 8px;
border-bottom-right-radius: 8px;
border-top-left-radius: 20px;
border-bottom-left-radius: 20px;
overflow: hidden;
}

.time-flies .year-progress-bar {
background-color: #4caf50;/*进度条颜色*/
}
.time-flies .month-progress-bar {
background-color: #4caf50;
}
.time-flies .day-progress-bar {
background-color: #4caf50;
}
.time-flies .hour-progress-bar {
background-color: #4caf50;
}
.time-flies .minute-progress-bar {
background-color: #4caf50;
}

html[data-theme="dark"] .time-flies .year-progress-bar {
background-color: #2e8330;/*进度条颜色*/
}
html[data-theme="dark"] .time-flies .month-progress-bar {
background-color: #2e8330;
}
html[data-theme="dark"] .time-flies .day-progress-bar {
background-color: #2e8330;
}
html[data-theme="dark"] .time-flies .hour-progress-bar {
background-color: #2e8330;
}
html[data-theme="dark"] .time-flies .minute-progress-bar {
background-color: #2e8330; //#4caf50;
}

.time-flies .progress-bar {
width: 100%;
background-color: $color-theme-light-2background;/*进度条背景色*/ //#ddd
border-radius: 5px;/*圆角*/
overflow: hidden;
}
html[data-theme="dark"] .time-flies .progress-bar {
background-color: $color-theme-dark-2background
}

@keyframes move {
0% {
background-position: 0 0;
}
100% {
background-position: 50px 50px;
}
}

@-webkit-keyframes move {
0% {
background-position: 0 0;
}
100% {
background-position: 50px 50px;
}
}

/* 时光飞逝 End -*/

添加

单独页面(适用于Hexo + Butterfly)

创建%ThemeLayout%\includes\page\time.pug
放入以下内容

1
.time-flies

找到%ThemeLayout%\page.pug
放入以下内容

1
2
3
4
5
6
7
8
9
10
extends includes/layout.pug

[...]
when 'about'
include includes/page/about.pug
+ when 'time'
+ include includes/page/time.pug
[...]

comments/index', {}, {cache: true})

插入 + 后面的内容即可,删除 + 号后无需添加空格

创建%Source%\time\index.md
放入以下内容

1
2
3
4
5
6
7
8
---
title: 时光飞逝
date: 2021-03-30 15:57:51
aside: false
top_img: false
comments: false
type: "time"
---

hexo g + s 即可看到效果


单独页面或边栏之类的

1
<div class="time-flies"></div>

License

(仅包括文中的 JavaScript 和 CSS 代码块)

The MIT License (MIT)

Copyright © 2026 AdminCmd(http://admincmd.xyz)

Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the “Software”), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED “AS IS”, WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.