Native JS Timer

2021-06-11 11:13:48 浏览数 (1)

魔改步骤

  1. 新建 [Blogroot]themesbutterflysourcejsruntime.js, 此处用到了 shield.io 生成徽标,更多内容请参看站内教程:博客添加 github 徽标方案:
代码语言:javascript复制
setInterval(() => {
  let create_time = Math.round(new Date('2019-04-17 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);
  //格式规范化,个位数前面加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);
  }//秒
  //早上7点到晚上10点营业
  if ((Number(time[2])<22) && (Number(time[2])>7)){
    currentTimeHtml ="<img class='boardsign' src='https://img.shields.io/badge/糖果屋-营业中-6adea8?style=social&logo=cakephp' title='距离百年老店也就差不到一百年~'><div id='runtime'>"   time[0]   ' YEAR '   time[1]   ' DAYS '   time[2]   ' : '   time[3]   ' : '   time[4]   '</div>';
  } //徽标内容参考站内教程
  //其余时间打烊
  else{
    currentTimeHtml ="<img class='boardsign' src='https://img.shields.io/badge/糖果屋-打烊了-6adea8?style=social&logo=coffeescript' title='这个点了应该去睡觉啦,熬夜对身体不好哦'><div id='runtime'>"   time[0]   ' YEAR '   time[1]   ' DAYS '   time[2]   ' : '   time[3]   ' : '   time[4]   '</div>'; //徽标内容参考站内教程
  }
  //覆写挂载标签的内容
  document.getElementById("workboard").innerHTML = currentTimeHtml;
}, 1000);
  1. 修改 [Blogroot]themesbutterflylayoutincludesfooter.pug, 添加计时器的挂载标签:
代码语言:javascript复制
  if theme.footer.copyright
    .framework-info
      span= _p('footer.framework')   ' '
      a(href='https://hexo.io')= 'Hexo'
      span.footer-separator |
      span= _p('footer.theme')   ' '
      a(href='https://github.com/jerryc127/hexo-theme-butterfly')= 'Butterfly'
  if theme.footer.custom_text
    .footer_custom_text!=`${theme.footer.custom_text}`
  #workboard
  script(async src=url_for(theme.CDN.runtime))
  1. 新建 [Blogroot]themesbutterflysourcecss_layoutruntime.styl,
代码语言:javascript复制
/*电子钟字体*/
@font-face
  font-family 'UnidreamLED'
  src url('https://cdn.jsdelivr.net/gh/Akilarlxh/Akilarlxh.github.io@v3.3.3_3/clock/fonts/UnidreamLED.ttf')
  font-display swap
//页脚计时器
div#runtime
  width 180px
  margin auto
  color white
  padding-inline 5px
  border-radius 10px
  background-color rgba(0,0,0,0.7)
  font-family 'UnidreamLED'

// 夜间全局透明度
[data-theme="dark"]
  div#runtime
      color rgb(40, 180, 200)
      box-shadow 0 0 5px rgba(28, 69, 218, 0.71)
      animation flashlight 1s linear infinite alternate
@keyframes flashlight
  from
    box-shadow 0 0 5px rgb(20, 120, 210)
  to
    box-shadow 0 0 2px rgb(20, 120, 210)
  1. 修改_config.butterfly.yml, 添加 CDN 链接:
代码语言:javascript复制
  CDN:
    # main
    main_css: /css/index.css
    jquery: https://cdn.jsdelivr.net/npm/jquery@latest/dist/jquery.min.js
    main: /js/main.js
    utils: /js/utils.js
    # 页脚计时器
    runtime: /js/runtime.js

TO DO

夜间模式适配

原生 js 实现

添加营业中,打烊了牌子

0 人点赞