Argon主题年份季节进度条代码更新,修复冬季不显示的bug

Heart Lv462

之前在 WordPress Argon 主题里做了年份进度条和季节进度条,顺便加了个自定义鼠标光标,今天才发现不少bug

自定义鼠标光标在窗口变窄时消失

用 max-width 媒体查询控制隐藏,结果桌面浏览器窗口变窄也会触发隐藏。max-width 是按视口宽度判断的,不是按设备类型。

改成用设备能力判断,通过 CSS 的 hover 和 pointer 媒体特性,或者 JS 判断触摸能力。目标是桌面端显示、手机平板不显示。

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
<img id="custom-cursor" src="光标.gif" style="display:none;" />

<style>
#custom-cursor {
position: fixed;
width: 50px;
height: 50px;
pointer-events: none;
z-index: 9999;
}
@media (hover: none), (pointer: coarse) {
#custom-cursor { display: none !important; }
}
</style>

<script>
(function(){
const cursor = document.getElementById("custom-cursor");
const isTouch = window.matchMedia("(hover: none), (pointer: coarse)").matches;
if (!isTouch) {
cursor.style.display = "block";
document.addEventListener("mousemove", (e) => {
cursor.style.left = e.clientX + "px";
cursor.style.top = e.clientY + "px";
});
}
})();
</script>

季节进度条冬季算错

春夏秋正常,冬季(12月到次年2月)进度一直是0%或者跳变。

Date.getMonth() 返回 0-11,0 代表 1 月。冬季跨年时如果只把年份减一,但结束日期还是用同一个年份算,会导致结束时间比开始时间还早,进度变负数被 clamp 到 0。

冬季要拆成两个年份处理:12月的开始年是当年、结束年是次年;1-2月的开始年是上一年、结束年是当年。

UI 不显示报错 Unexpected token

进度条空白,控制台报 Unexpected token ‘<’。

两种常见原因:JS 里混入了 HTML 标签,解析器遇到 < 就报错;或者请求的 JS 文件实际返回了 HTML(比如 404 页面)。

排查方法:看 console 第一条红错,验证 jQuery 是否存在,验证 DOM 元素是否存在,手动写入测试看能不能显示。

完整代码

放在外观 → 小工具 → 自定义 HTML 里。

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
105
106
107
108
109
110
111
112
113
114
115
116
<div class="progress-wrapper" style="padding: 0">
<div class="progress-info">
<div class="progress-label">
<span id="yearprogress_yearname" style="font-size: 1.3em; font-weight: bold"></span>
</div>
<div id="yearprogress_text_container" class="progress-percentage">
<span id="yearprogress_progresstext" style="font-weight: bold"></span>
<span id="yearprogress_progresstext_full" style="font-weight: bold"></span>
</div>
</div>
<div class="progress">
<div id="yearprogress_progressbar" class="progress-bar bg-primary"></div>
</div>

<div class="progress-info" style="margin-top: 12px; display: flex; align-items: center; justify-content: space-between">
<div style="display: flex; align-items: center; gap: 8px">
<span id="season_icon" style="font-size: 1.2em">🌱</span>
<span id="season_name" style="font-weight: normal"></span>
</div>
<div id="season_text_container" class="progress-percentage">
<span id="season_progresstext" style="font-weight: normal"></span>
<span id="season_progresstext_full" style="font-weight: normal"></span>
</div>
</div>
<div class="progress">
<div id="season_progressbar" class="progress-bar"></div>
</div>
</div>

<script no-pjax="">
function getSeason(month) {
if (month >= 2 && month <= 4) return {name: "春季", startMonth: 2, endMonth: 4, color: '#4cff7e', icon: '🌱'};
if (month >= 5 && month <= 7) return {name: "夏季", startMonth: 5, endMonth: 7, color: '#dc4437', icon: '☀️'};
if (month >= 8 && month <= 10) return {name: "秋季", startMonth: 8, endMonth: 10, color: '#ffcc4c', icon: '🍂'};
return {name: "冬季", startMonth: 11, endMonth: 1, color: '#4ccbff', icon: '❄️'};
}

function yearprogress_refresh() {
if (!document.getElementById("yearprogress_yearname")) return;

let now = new Date();
let year = now.getFullYear();
$("#yearprogress_yearname").text(year);

let from = new Date(year, 0, 1, 0, 0, 0);
let to = new Date(year, 11, 31, 23, 59, 59);

let progress = (((now - from) / (to - from + 1)) * 100).toFixed(7);
let progressshort = (((now - from) / (to - from + 1)) * 100).toFixed(2);

$("#yearprogress_progresstext").text(progressshort + "%");
$("#yearprogress_progresstext_full").text(progress + "%");
$("#yearprogress_progressbar").css({"width": progress + "%", "background-color": "#607d8b"});

const yearColor = "#607d8b";
$("#yearprogress_yearname").css("color", yearColor);
$("#yearprogress_progresstext").css("color", yearColor);
$("#yearprogress_progresstext_full").css("color", yearColor);

const month = now.getMonth();
const season = getSeason(month);

let startYear = year;
let endYear = year;

if (season.name === "冬季") {
if (month === 11) {
startYear = year;
endYear = year + 1;
} else {
startYear = year - 1;
endYear = year;
}
}

let seasonStart = new Date(startYear, season.startMonth, 1, 0, 0, 0);
let seasonEnd = new Date(endYear, season.endMonth + 1, 0, 23, 59, 59);

let seasonProgress = ((now - seasonStart) / (seasonEnd - seasonStart)) * 100;
if (seasonProgress > 100) seasonProgress = 100;
if (seasonProgress < 0) seasonProgress = 0;

$("#season_name").text(season.name);
$("#season_progresstext").text(seasonProgress.toFixed(2) + "%");
$("#season_progresstext_full").text(seasonProgress.toFixed(7) + "%");
$("#season_progressbar").css("width", seasonProgress + "%");
$("#season_name").css("color", season.color);
$("#season_progresstext").css("color", season.color);
$("#season_progresstext_full").css("color", season.color);
$("#season_icon").text(season.icon);
$("#season_progressbar").css("background-color", season.color);
}

yearprogress_refresh();
if (window.__yearProgressIntervalId) clearInterval(window.__yearProgressIntervalId);
window.__yearProgressIntervalId = setInterval(yearprogress_refresh, 500);
</script>

<style>
#yearprogress_text_container, #season_text_container {
width: 100%;
height: 22px;
overflow: hidden;
user-select: none;
}
#yearprogress_text_container > span, #season_text_container > span {
transition: all 0.3s ease;
display: block;
}
#yearprogress_text_container:hover > span, #season_text_container:hover > span {
transform: translateY(-45px);
}
#season_progressbar {
transition: width 0.5s ease, background-color 0.5s ease;
}
</style>
  • Title: Argon主题年份季节进度条代码更新,修复冬季不显示的bug
  • Author: Heart
  • Created at : 2025-12-23 14:00:00
  • Updated at : 2026-01-01 14:59:00
  • Link: https://yhalo-wyh.github.io/2025/12/23/解决日记-年份季节进度条-Argon-2026-01-01/
  • License: This work is licensed under CC BY-NC-SA 4.0.
Comments
On this page
Argon主题年份季节进度条代码更新,修复冬季不显示的bug