js和css文件创建和引入问题看这篇文章:Hexo博客添加自定义css和js文件
前言
在糖果屋看到了这个底部运行时间,感觉还不错。
不过作者是把底部的运行时间和那个徽标一起做成了一个插件,我下载之后发现其实这个时间只是插件的附带,主要的还是加徽标方便,然后我就把这个时间单独提了出来。
正文
本来我还以为会写的比较复杂,后来发现其实是我想复杂了,就是简单的时间运算而已。
注意:部分代码中最前面的加号代表增加,减号代表删除,是为了高亮代码的,粘贴代码之后记得删除。删除之后不需要再补上一个空格,这样会出bug。
 
添加盒子
首先我们先在底部加一个盒子,用来盛放时间。
我们在主题的配置文件里找到footer下面的custom_text,添加如下代码:
1 2 3 4 5 6 7 8
   | footer:   owner:     enable: false     since: 2021 -  custom_text:  +  custom_text: <div id="runtime"></div>   copyright: false # Copyright of theme and framework
 
   | 
 
添加js
新建自定义js文件
在自定义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 29 30 31 32 33 34 35 36 37
   | setInterval(() => {               let create_time = Math.round(new Date('2021/10/15 00:00:00').getTime() / 1000);      let timestamp = Math.round((new Date().getTime()) / 1000);     let second = timestamp - create_time;     let time = new Array(0, 0, 0, 0, 0);
      var nol = function(h) {         return h > 9 ? h : '0' + h;     }     if (second >= 365 * 24 * 3600) {         time[0] = parseInt(second / (365 * 24 * 3600));         second %= 365 * 24 * 3600;     }     if (second >= 24 * 3600) {         time[1] = parseInt(second / (24 * 3600));         second %= 24 * 3600;     }     if (second >= 3600) {         time[2] = nol(parseInt(second / 3600));         second %= 3600;     }     if (second >= 60) {         time[3] = nol(parseInt(second / 60));         second %= 60;     }     if (second >= 0) {         time[4] = nol(second);     }     let currentTimeHtml = ""     if (time[0] != 0) {         currentTimeHtml += time[0] + ' YEAR '     }     currentTimeHtml += time[1] + ' DAYS ' + time[2] + ' : ' + time[3] + ' : ' + time[4];     document.getElementById("runtime").innerHTML = currentTimeHtml; }, 1000);
  | 
 
添加css
新建自定义css文件
在自定义css中添加如下代码:
1 2 3 4 5 6 7 8 9 10 11 12
   | div#runtime {     width: fit-content;     color: #fff;     padding: 0 10px;     border-radius: 10px;     background-color: rgba(0, 0, 0, 0.7); }
  [data-theme="dark"] div#runtime {     color: #28b4c8;     box-shadow: 0 0 5px rgba(28, 69, 218, 0.71); }
  |