适用主题:OneNav(onenav)及其子主题
说明:本教程面向普通用户,通过主题自带的小工具 + 一段代码即可实现,无需修改任何主题文件、无需建子主题小工具。
效果预览
侧边栏显示一个「人生倒计时」卡片,包含四个进度条:
| 维度 | 显示内容 | 进度条颜色 |
|---|---|---|
| 今日 | 已经过去 N 小时 | 蓝 |
| 本周 | 已经过去 N 天 | 黄 |
| 本月 | 已经过去 N 天 | 红 |
| 今年 | 已经过去 N 个月 | 绿 |
- 加载时数字从 0 滚动到当前值,进度条依次填充
- 进度条带动态斜条纹流动效果
- 每 60 秒自动刷新,全程纯前端计算,服务器零负载
一、准备工作
1.1 确认可用的小工具
OneNav 主题为插入自定义代码提供了两种小工具,任选其一:
- IO 嵌入代码(推荐):
外观 → 小工具 → 添加小工具 → IO 嵌入代码 - 自定义 HTML:WordPress 自带,OneNav 未屏蔽,
外观 → 小工具 → 添加小工具 → 自定义 HTML
推荐使用 IO 嵌入代码,它是 OneNav 专门用来插入 HTML/CSS/JS 的小工具,视觉与主题一致,且自带标题图标选择器。
1.2 放置位置
OneNav 的侧边栏有多个区域,按页面类型区分:
– 文章正文侧边栏(sidebar-s)— 文章页
– 页面侧边栏(sidebar-page)— 页面
– 分类归档侧边栏(sidebar-a)— 分类/搜索/404
– 首页内容模块侧边栏
把「人生倒计时」小工具拖到你想显示的侧边栏区域即可。
二、添加代码
把下面这段代码完整复制,粘贴到小工具的代码框中保存。
⚠️ 注意:如果使用 自定义 HTML 小工具,JS 里的
</script>一定要保留反斜杠写法,否则脚本会被提前截断;使用 IO 嵌入代码 小工具时同样建议保留。
<style>
/* ===== 人生倒计时 · OneNav 适配版 ===== */
.io-timelife { padding: 12px 16px; }
.io-timelife .tl-item { margin-bottom: 18px; }
.io-timelife .tl-item:last-child { margin-bottom: 0; }
.io-timelife .tl-title {
font-size: 13px;
color: var(--main-color, #5f6473);
margin-bottom: 7px;
display: flex;
align-items: baseline;
gap: 4px;
}
.io-timelife .tl-title .tl-num {
display: inline-block;
min-width: 24px;
text-align: center;
color: var(--theme-color, #f1404b);
font-weight: 700;
font-size: 17px;
line-height: 1.1;
font-variant-numeric: tabular-nums;
}
.io-timelife .tl-title .tl-num.tl-bounce { animation: tl-bounce .5s ease; }
@keyframes tl-bounce {
0% { transform: scale(1); }
35% { transform: scale(1.45); color: #ff4757; }
100% { transform: scale(1); }
}
.io-timelife .tl-progress {
display: flex;
align-items: center;
gap: 10px;
}
.io-timelife .tl-bar {
position: relative;
flex: 1;
height: 9px;
background: rgba(0,0,0,.06);
border-radius: 10px;
overflow: hidden;
}
.io-timelife .tl-inner {
position: relative;
height: 100%;
border-radius: 10px;
background-color: var(--bar-color, #f1404b);
background-image: linear-gradient(
135deg,
rgba(255,255,255,.28) 25%, transparent 25%,
transparent 50%, rgba(255,255,255,.28) 50%,
rgba(255,255,255,.28) 75%, transparent 75%, transparent 100%
);
background-size: 24px 24px;
animation: tl-stripes .9s linear infinite;
will-change: width;
}
@keyframes tl-stripes {
from { background-position: 0 0; }
to { background-position: 24px 0; }
}
/* 四个维度颜色:日/周/月/年 = 蓝/黄/红/绿 */
.io-timelife .tl-inner-0 { --bar-color: #3a7afe; }
.io-timelife .tl-inner-1 { --bar-color: #f5a623; }
.io-timelife .tl-inner-2 { --bar-color: #f1404b; }
.io-timelife .tl-inner-3 { --bar-color: #67c23a; }
.io-timelife .tl-pct {
font-size: 12px;
color: #999;
min-width: 36px;
text-align: right;
font-variant-numeric: tabular-nums;
}
/* 暗色模式适配 */
.io-black-mode .io-timelife .tl-title { color: #a9a9b0; }
.io-black-mode .io-timelife .tl-bar { background: rgba(255,255,255,.1); }
.io-black-mode .io-timelife .tl-pct { color: #888; }
.io-black-mode .io-timelife .tl-inner {
background-image: linear-gradient(
135deg,
rgba(255,255,255,.18) 25%, transparent 25%,
transparent 50%, rgba(255,255,255,.18) 50%,
rgba(255,255,255,.18) 75%, transparent 75%, transparent 100%
);
}
</style>
<div class="card-body io-timelife">
<div class="tl-item">
<div class="tl-title">今日已经过去 <span class="tl-num" data-tl="day">0</span> 小时</div>
<div class="tl-progress">
<div class="tl-bar"><div class="tl-inner tl-inner-0" style="width:0%"></div></div>
<div class="tl-pct" data-tl-pct="day">0%</div>
</div>
</div>
<div class="tl-item">
<div class="tl-title">本周已经过去 <span class="tl-num" data-tl="week">0</span> 天</div>
<div class="tl-progress">
<div class="tl-bar"><div class="tl-inner tl-inner-1" style="width:0%"></div></div>
<div class="tl-pct" data-tl-pct="week">0%</div>
</div>
</div>
<div class="tl-item">
<div class="tl-title">本月已经过去 <span class="tl-num" data-tl="month">0</span> 天</div>
<div class="tl-progress">
<div class="tl-bar"><div class="tl-inner tl-inner-2" style="width:0%"></div></div>
<div class="tl-pct" data-tl-pct="month">0%</div>
</div>
</div>
<div class="tl-item">
<div class="tl-title">今年已经过去 <span class="tl-num" data-tl="year">0</span> 个月</div>
<div class="tl-progress">
<div class="tl-bar"><div class="tl-inner tl-inner-3" style="width:0%"></div></div>
<div class="tl-pct" data-tl-pct="year">0%</div>
</div>
</div>
</div>
<script>
(function () {
'use strict';
var KEY = ['day', 'week', 'month', 'year'];
function compute() {
var now = new Date();
var y = now.getFullYear();
var m = now.getMonth(); // 0-11
var d = now.getDate();
var dow = now.getDay(); // 0(周日)-6
var h = now.getHours();
var min = now.getMinutes();
var sec = now.getSeconds();
var daysInMonth = new Date(y, m + 1, 0).getDate();
var nowInDay = h + min / 60 + sec / 3600;
return {
day: { num: Math.floor(nowInDay), pct: nowInDay / 24 * 100 },
week: { num: (dow === 0 ? 6 : dow - 1) + 1, pct: ((dow === 0 ? 6 : dow - 1) + nowInDay / 24) / 7 * 100 },
month: { num: d, pct: (d - 1 + nowInDay / 24) / daysInMonth * 100 },
year: { num: m + 1, pct: (m + (d - 1 + nowInDay / 24) / daysInMonth) / 12 * 100 }
};
}
function animateNum(el, to, first) {
var from = first ? 0 : (parseInt(el.dataset.v, 10) || 0);
if (from === to) return;
el.dataset.v = to;
var start = null;
function step(ts) {
if (start === null) start = ts;
var t = Math.min(1, (ts - start) / 700);
var v = from + (to - from) * (1 - Math.pow(1 - t, 3));
el.textContent = Math.round(v);
if (t < 1) { requestAnimationFrame(step); }
else {
el.textContent = to;
el.classList.remove('tl-bounce');
void el.offsetWidth;
el.classList.add('tl-bounce');
}
}
requestAnimationFrame(step);
}
function animatePct(el, to, first) {
var from = first ? 0 : (parseFloat(el.dataset.v) || 0);
if (Math.abs(from - to) < .2) { el.textContent = Math.round(to) + '%'; el.dataset.v = to; return; }
el.dataset.v = to;
var start = null;
function step(ts) {
if (start === null) start = ts;
var t = Math.min(1, (ts - start) / 700);
var v = from + (to - from) * (1 - Math.pow(1 - t, 3));
el.textContent = Math.round(v) + '%';
if (t < 1) requestAnimationFrame(step);
}
requestAnimationFrame(step);
}
function animateBar(el, to, first, i) {
var from = first ? 0 : (parseFloat(el.dataset.v) || 0);
to = Math.min(100, to);
el.dataset.v = to;
var delay = first ? i * 120 : 0;
var start = null;
setTimeout(function () {
function step(ts) {
if (start === null) start = ts;
var t = Math.min(1, (ts - start) / 700);
var v = from + (to - from) * (1 - Math.pow(1 - t, 3));
el.style.width = v + '%';
if (t < 1) requestAnimationFrame(step);
}
requestAnimationFrame(step);
}, delay);
}
var root = document.querySelector('.io-timelife');
if (!root) return;
function update(first) {
var data = compute();
KEY.forEach(function (k, i) {
var num = root.querySelector('[data-tl="' + k + '"]');
var pct = root.querySelector('[data-tl-pct="' + k + '"]');
var bar = root.querySelector('.tl-inner-' + i);
if (num) animateNum(num, data[k].num, first);
if (pct) animatePct(pct, data[k].pct, first);
if (bar) animateBar(bar, data[k].pct, first, i);
});
}
update(true);
setInterval(function () { update(false); }, 60000);
})();
</script>
三、标题美化(可选)
如果想让卡片顶部显示带图标的「人生倒计时」标题,可以在上面的 HTML 前追加一段标题 HTML:
<div class="sidebar-header">
<div class="card-header widget-header">
<h3 class="text-md mb-0">
<i class="mr-2 iconfont icon-time"></i>人生倒计时
</h3>
</div>
</div>
说明:
– 这段用到了 OneNav 的主题样式类 sidebar-header、card-header widget-header、iconfont,与主题自带小工具标题一致
– 图标可替换为其他 iconfont 类名(如 icon-chart、icon-fire),或在 OneNav 后台的图标选择器里挑选
提示:如果使用 IO 嵌入代码 小工具,建议直接在它的「标题」和「标题图标」设置项里填,无需手写这段标题 HTML,效果更原生。
四、参数与自定义
| 想改什么 | 改哪里 |
|---|---|
| 刷新频率 | JS 末尾 60000(毫秒),改为 10000 即每 10 秒刷新 |
| 数字滚动速度 | JS 中 700(毫秒) |
| 进度条颜色 | CSS 中 .tl-inner-0~3 的 --bar-color 值 |
| 条纹疏密 | CSS 中 background-size: 24px 24px 的尺寸 |
| 条纹流动快慢 | CSS 中 tl-stripes .9s 的时间 |
| 暗色模式条纹 | CSS 中 .io-black-mode 部分 |
| 文案 | HTML 中「今日已经过去…」「小时/天/月」等文字 |
五、常见问题
Q1:进度条不流动 / 不显示?
– 确认 CSS 完整粘贴、无语法错误
– 检查主题是否有其他同名 .tl-inner 样式冲突(本教程已加 io-timelife 前缀避免冲突)
Q2:数字不更新 / 进度条卡住?
– 检查浏览器控制台(F12)是否有 JS 报错
– 确认 JS 里的 </script> 反斜杠没有被去除(自定义 HTML 小工具里若被改成 </script> 会截断脚本)
Q3:暗色模式下条纹过曝?
– 已内置 .io-black-mode 适配,若仍异常,检查主题暗色类名是否为 io-black-mode(OneNav 默认是这个)
Q4:放在多个侧边栏会重复吗?
– 代码用 document.querySelector('.io-timelife') 只会取第一个实例。若需多个,请改用 querySelectorAll 遍历(不建议,通常一个即可)
Q5:不想用 jQuery,主题必须装吗?
– 本教程使用原生 JavaScript,不依赖 jQuery,任何 OneNav 版本都能运行
六、原理简述
- HTML:四个
.tl-item分别对应日/周/月/年,每项含标题、进度条、百分比 - CSS:进度条用
linear-gradient斜条纹 +background-position动画制造流动感;四条分别用 CSS 变量--bar-color设定蓝/黄/红/绿 - JS:用
Date对象实时计算 - 今日 = 当前小时(含分钟秒)
- 本周 = 距周一的天数(周一为一周起点)
- 本月 = 当前日 ÷ 当月总天数
- 今年 = 当前月 ÷ 12
- 数字/百分比/进度条宽度用
requestAnimationFrame做缓动动画,setInterval每 60 秒刷新
全程纯前端计算,不请求任何接口,服务器零负载,对访问量无压力。
教程完 · 祝你用得开心 ⏳

