2020 以水文开篇,为任意站点添加生存时间,效果见博客页脚,大佬自行跳过。
引入 JS
将以下代码加入到 <head>
标签中。对于本主题,依次进入 控制台 - 外观 - 设置外观 - 主题自定义扩展
,加入到 自定义 HTML 元素拓展 - 标签: head 头部 (meta 元素后)
,也可直接加入到主题对应的 header.php
中的 </head>
标签前。开始时间和中文提示可自定义。
- <script>
- document.addEventListener('DOMContentLoaded', initLiveDay);
- function initLiveDay() {
- const birthTime = '2015/2/21 10:23:12';
- const template = (A, B, C, D) => `本站已勉强生存 ${A}天 ${B}时 ${C}分 ${D}秒.`;
-
- /* 锚点开始 */
- const container = footer.querySelector('.container');
- const p = document.createElement('p');
- container.insertBefore(p, container.firstElementChild);
- /* 锚点结束*/
-
- const msoad = 24 * 60 * 60 * 1000;
- const warp = n => n > 9 ? n : '0' + n;
- const toInt = n => warp(Math.floor(n));
- setInterval(() => {
- const lived = new Date() - new Date(birthTime);
- const days = lived / msoad;
- const intDays = toInt(days);
- const hours = (days - intDays) * 24;
- const intHours = toInt(hours);
- const minutes = (hours - intHours) * 60;
- const intMinutes = toInt(minutes);
- const seconds = (minutes - intMinutes) * 60;
- const intSeconds = toInt(seconds);
- p.innerHTML = template(intDays, intHours, intMinutes, intSeconds);
- }, 1000);
- }
- </script>
其他主题
其他主题需要通过 F12 找到对应锚点,替换代码中那三行,不知道怎么找的,留下 博客链接
和 要加入的位置
,我看到后会将锚点代码更新到此。
CREAMY
演示地址
- const footer = document.body.querySelector('.site-footer');
- footer.insertBefore(document.createRange().createContextualFragment(`
- <div class="container d-flex justify-content-sm-between justify-content-center text-center">
- <div class="copyright"><p style="font-family: Consolas"></p></div>
- </div>`.trim()).firstElementChild, footer.firstElementChild);
- const p = footer.firstElementChild.querySelector('p');
如有问题请在下方留言,文章转载请注明出处,详细交流请加下方群组!请大佬不要屏蔽文中广告,因为它将帮我分担服务器开支,如果能帮忙点击我将万分感谢。
强调几点:(该留言由系统自动生成!)
1. 请不要刷广告,本站没有流量!
2. 我不回复虚假邮箱,因为回复了你也看不到!
3. 存在必须回复的隐藏内容时,可以直接使用表情框里的阿鲁表情!