温馨提示×

温馨提示×

您好,登录后才能下订单哦!

密码登录×
登录注册×
其他方式登录
点击 登录注册 即表示同意《亿速云用户服务条款》

如何基于JavaScript实现网红太空人表盘

发布时间:2021-03-24 09:33:08 阅读:202 作者:小新 栏目:开发技术
前端开发者测试专用服务器限时活动,0元免费领,库存有限,领完即止! 点击查看>>

这篇文章主要介绍如何基于JavaScript实现网红太空人表盘,文中介绍的非常详细,具有一定的参考价值,感兴趣的小伙伴们一定要看完!

一、效果展示

用javascript写的一个太空人表盘。

http://xiazai.jb51.net/202103/yuanma/Watch_jb51.rar

如何基于JavaScript实现网红太空人表盘

 二、源代码

html代码

<html>
<head>
 <title>太空人表盘</title>
 <meta charset="UTF-8">
 <link href="./assets/css/style.css" rel="external nofollow" rel="stylesheet">
 <script src="./assets/js/timeGeneration.js"></script>
</head>
<body>
<div class="jun-meter">
 <div class="jun-time-h-h" id="hh"></div>
 <div class="jun-time-h-l" id="hl"></div>
 <div class="jun-time-rect"></div>
 <div class="jun-human"></div>
 <div class="jun-time-m-h" id="mh"></div>
 <div class="jun-time-m-l" id="ml"></div>
 <div class="jun-time-s-h" id="sh"></div>
 <div class="jun-time-s-l" id="sl"></div>
 <div class="jun-date" id="date"></div>
 <div class="jun-calendar-date" id="calendarDate"></div>
</div>
</body>
 
<script>
 
 function WatchMeter() {
  // 初始化dom
  this._initDom()
  // 更新
  this.update()
 
  this.date = new TimeGeneration()
 
  this._render(this.date.getDate(), this.date.getCalendarDate(), this.date.getTime())
 
 }
 
 // 修改原型
 WatchMeter.prototype = {
  constructor: WatchMeter,
  // 初始化Dom
  _initDom: function () {
   this.elem = {}
   this.elem.hh = document.getElementById('hh')
   this.elem.hl = document.getElementById('hl')
   this.elem.mh = document.getElementById('mh')
   this.elem.ml = document.getElementById('ml')
   this.elem.sh = document.getElementById('sh')
   this.elem.sl = document.getElementById('sl')
   this.elem.date = document.getElementById('date')
   this.elem.calendarDate = document.getElementById('calendarDate')
  },
 
  // 更新
  update: function () {
   var _this = this
   setInterval(function () {
    _this._render(_this.date.getDate(), _this.date.getCalendarDate(), _this.date.getTime())
   }, 1000)
  },
 
  // 渲染
  _render: function (date, calendarDate, time) {
   this._setNumberImage(this.elem.hh, time[0])
   this._setNumberImage(this.elem.hl, time[1])
   this._setNumberImage(this.elem.mh, time[2])
   this._setNumberImage(this.elem.ml, time[3])
   this._setNumberImage(this.elem.sh, time[4])
   this._setNumberImage(this.elem.sl, time[5])
   this.elem.date.innerText = date[2] + " " +date[0] + "-" +date[1]
   this.elem.calendarDate.innerText = calendarDate
  },
 
  // 设置数字图片
  _setNumberImage: function (elem, value) {
   elem.style.backgroundImage = "url(./assets/img/" + value + ".svg)";
  }
 }
 
 var myWatchMeter = new WatchMeter()
</script>
 
</html>

js代码

// 生成时间 农历 公历 时间
function TimeGeneration() {
 
}
 
TimeGeneration.prototype = {
 constructorTimeGeneration,
 
 WEEKDAY_NAME: ["周日""周一""周二""周三""周四""周五""周六"],
 NUMBER_STRING"一二三四五六七八九十",
 MONTH_STRING"正二三四五六七八九十冬腊",
 MONTH_ADD: [0315990120151181212243273304334],
 CALENDAR_DATA: [0xA4B0x5164B0x6A50x6D40x415B50x2B60x9570x2092F0x4970x60C960xD4A0xEA50x50DA90x5AD0x2B60x3126E0x92E0x7192D0xC950xD4A0x61B4A0xB550x56A0x4155B0x25D0x92D0x2192B0xA950x716950x6CA0xB550x50AB50x4DA0xA5B0x30A570x52B0x8152A0xE950x6AA0x615AA0xAB50x4B60x414AE0xA570x5260x31D260xD950x70B550x56A0x96D0x5095D0x4AD0xA4D0x41A4D0xD250x81AA50xB540xB6A0x612DA0x95B0x49B0x414970xA4B0xA164B0x6A50x6D40x615B40xAB60x9570x5092F0x4970x64B0x30D4A0xEA50x80D650x5AC0xAB60x5126D0x92E0xC960x41A950xD4A0xDA50x20B550x56A0x7155B0x25D0x92D0x5192B0xA950xB4A0x416AA0xAD50x90AB50x4BA0xA5B0x60A570x52B0xA930x40E95],
 
 _getBitfunction (m, n) {
  return (m >> n) & 1;
 },
 
 
 // 获取时间 array
 getTimefunction () {
  var time = new Date();
  return [parseInt(time.getHours() / 10),
   parseInt(time.getHours() % 10),
   parseInt(time.getMinutes() / 10),
   parseInt(time.getMinutes() % 10),
   parseInt(time.getSeconds() / 10),
   parseInt(time.getSeconds() % 10)]
 },
 
 // 获取公历日期 array
 getDatefunction () {
  var date = new Date();
  return [
   date.getMonth() + 1,
   date.getDate(),
   this.WEEKDAY_NAME[date.getDay()]
  ]
 },
 
 // 获取农历日期 string
 getCalendarDatefunction () {
  var calendar = new Date();
  var tmp = calendar.getFullYear();
 
  if (tmp < 1900) {
   tmp += 1900;
  }
 
  var total = (tmp - 1921) * 365 + Math.floor((tmp - 1921) / 4) + this.MONTH_ADD[calendar.getMonth()] + calendar.getDate() - 38;
  if (calendar.getFullYear() % 4 == 0 && calendar.getMonth() > 1) {
   total++;
  }
 
  var isEnd = false;
  var n, m, k
  for (m = 0; ; m++) {
   k = (this.CALENDAR_DATA[m] < 0xfff) ? 11 : 12;
   for (n = k; n >= 0; n--) {
    if (total <= 29 + this._getBit(this.CALENDAR_DATA[m], n)) {
     isEnd = true;
     break;
    }
    total = total - 29 - this._getBit(this.CALENDAR_DATA[m], n);
   }
   if (isEnd) break;
  }
 
  var month = k - n + 1;
  var day = total;
 
  if (k == 12) {
   if (month == Math.floor(this.CALENDAR_DATA[m] / 0x10000) + 1) {
    month = 1 - month;
   }
   if (month > Math.floor(this.CALENDAR_DATA[m] / 0x10000) + 1) {
    month--;
   }
  }
 
  var tmp = "";
  if (month < 1) {
   tmp += "(闰)";
   tmp += this.MONTH_STRING.charAt(-month - 1);
  } else {
   tmp += this.MONTH_STRING.charAt(month - 1);
  }
 
  tmp += "月";
  tmp += (day < 11) ? "初" : ((day < 20) ? "十" : ((day < 30) ? "廿" : "三十"));
  if (day % 10 != 0 || day == 10) {
   tmp += this.NUMBER_STRING.charAt((day - 1) % 10);
  }
  return tmp;
 }
 
}

CSS代码

.jun-meter {
 position: relative;
 width640px;
 height640px;
 background-imageurl("../img/ring.svg");
 background-repeat: no-repeat;
 background-size100%;
 margin: auto;
 margin-top7%;
}
 
.jun-time-rect {
 position: absolute;
 width30px;
 height80px;
 left275px;
 top180px;
 background-imageurl("../img/rect.svg");
 background-size40px 40px;
}
 
.jun-time-h-h {
 position: absolute;
 width100px;
 height100px;
 left140px;
 top170px;
 background-imageurl("../img/8.svg");
 background-repeat: no-repeat;
 background-size100%;
}
 
.jun-time-h-l {
 position: absolute;
 width100px;
 height100px;
 left200px;
 top170px;
 background-imageurl("../img/8.svg");
 background-repeat: no-repeat;
 background-size100%;
}
 
.jun-time-m-h {
 position: absolute;
 width100px;
 height100px;
 left290px;
 top170px;
 background-imageurl("../img/8.svg");
 background-repeat: no-repeat;
 background-size100%;
}
 
.jun-time-m-l {
 position: absolute;
 width100px;
 height100px;
 left350px;
 top170px;
 background-imageurl("../img/8.svg");
 background-repeat: no-repeat;
 background-size100%;
}
 
.jun-time-s-h {
 position: absolute;
 width60px;
 height60px;
 left430px;
 top208px;
 background-imageurl("../img/8.svg");
 background-repeat: no-repeat;
 background-size100%;
}
 
.jun-time-s-l {
 position: absolute;
 width60px;
 height60px;
 left470px;
 top208px;
 background-imageurl("../img/8.svg");
 background-repeat: no-repeat;
 background-size100%;
}
 
.jun-calendar-date {
 position: absolute;
 width120px;
 height30px;
 left460px;
 top310px;
 line-height30px;
 font-size20px;
 text-align: center;
}
 
.jun-date {
 position: absolute;
 width120px;
 height30px;
 left460px;
 top345px;
 line-height30px;
 font-size20px;
 text-align: center;
}
 
.jun-human{
 position: absolute;
 width150px;
 height150px;
 left250px;
 top280px;
 background-imageurl("../img/human.gif");
 background-repeat: no-repeat;
 background-size100%;
}

以上是“如何基于JavaScript实现网红太空人表盘”这篇文章的所有内容,感谢各位的阅读!希望分享的内容对大家有帮助,更多相关知识,欢迎关注亿速云行业资讯频道!

亿速云「云服务器」,即开即用、新一代英特尔至强铂金CPU、三副本存储NVMe SSD云盘,价格低至29元/月。点击查看>>

向AI问一下细节

免责声明:本站发布的内容(图片、视频和文字)以原创、转载和分享为主,文章观点不代表本网站立场,如果涉及侵权请联系站长邮箱:is@yisu.com进行举报,并提供相关证据,一经查实,将立刻删除涉嫌侵权内容。

AI

开发者交流群×