Featured image of post 【Hugo】hugo-stack主题魔改-博客运行时间

【Hugo】hugo-stack主题魔改-博客运行时间

文章共454字

博客运行时间

1. 完成样式

image.png

2. footer.html文件修改

layouts\partials\footer\footer.html目录下新增以下代码:(没有该文件则自己创建)

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
    {{ with .Site.Params.footer.runtimeSince }}
    <section class="runtime" style="margin-top:8px;font-size:1.3rem">
        <span id="site-runtime">站点已运行计算中...</span>
    </section>
    <script>
      (function(){
        var sinceStr = '{{ . }}'.replace('T',' ').replace('t',' ');
        var start = new Date(sinceStr);
        function update(){
          var now = new Date();
          var diff = Math.max(0, now - start);
          var days = Math.floor(diff / (24*60*60*1000));
          var hours = Math.floor((diff % (24*60*60*1000)) / (60*60*1000));
          var mins = Math.floor((diff % (60*60*1000)) / (60*1000));
          var secs = Math.floor((diff % (60*1000)) / 1000);
          var el = document.getElementById('site-runtime');
          if(el){ el.textContent = '站点已运行 ' + days + ' 天 ' + hours + ' 小时 ' + mins + ' 分 ' + secs + ' 秒'; }
        }
        update();
        setInterval(update, 1000);
      })();
    </script>
    {{ end }}

image.png

3. 修改config.yaml文件

config.yaml文件下新增该内容即可

1
runtimeSince: 2025-08-025T00:00:00   # 站点开始运行的时间(本地时区),用于页脚运行时间

image.png

4. 修改custom.scss文件按

让页脚运行时间颜色与版权一致。修改\assets\scss\custom.scss文件。(没有自己创建即可)

1
2
3
4
5
6
/* 页脚运行时间颜色与版权一致 */
footer.site-footer {
  .runtime {
    color: var(--accent-color);
    font-weight: bold;
  }

image.png

站点已运行计算中...