小言_互联网的博客

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

285人阅读  评论(0)

一、效果展示

用javascript写的一个太空人表盘。源码请从文章末尾获取下载

 二、源代码

html代码


  
  1. <html>
  2. <head>
  3. <title>太空人表盘 </title>
  4. <meta charset="UTF-8">
  5. <link href="./assets/css/style.css" rel="stylesheet">
  6. <script src="./assets/js/timeGeneration.js"> </script>
  7. </head>
  8. <body>
  9. <div class="jun-meter">
  10. <div class="jun-time-h-h" id="hh"> </div>
  11. <div class="jun-time-h-l" id="hl"> </div>
  12. <div class="jun-time-rect"> </div>
  13. <div class="jun-human"> </div>
  14. <div class="jun-time-m-h" id="mh"> </div>
  15. <div class="jun-time-m-l" id="ml"> </div>
  16. <div class="jun-time-s-h" id="sh"> </div>
  17. <div class="jun-time-s-l" id="sl"> </div>
  18. <div class="jun-date" id="date"> </div>
  19. <div class="jun-calendar-date" id="calendarDate"> </div>
  20. </div>
  21. </body>
  22. <script>
  23. function WatchMeter() {
  24. // 初始化dom
  25. this._initDom()
  26. // 更新
  27. this.update()
  28. this.date = new TimeGeneration()
  29. this._render( this.date.getDate(), this.date.getCalendarDate(), this.date.getTime())
  30. }
  31. // 修改原型
  32. WatchMeter.prototype = {
  33. constructor: WatchMeter,
  34. // 初始化Dom
  35. _initDom: function () {
  36. this.elem = {}
  37. this.elem.hh = document.getElementById( 'hh')
  38. this.elem.hl = document.getElementById( 'hl')
  39. this.elem.mh = document.getElementById( 'mh')
  40. this.elem.ml = document.getElementById( 'ml')
  41. this.elem.sh = document.getElementById( 'sh')
  42. this.elem.sl = document.getElementById( 'sl')
  43. this.elem.date = document.getElementById( 'date')
  44. this.elem.calendarDate = document.getElementById( 'calendarDate')
  45. },
  46. // 更新
  47. update: function () {
  48. var _this = this
  49. setInterval( function () {
  50. _this._render(_this.date.getDate(), _this.date.getCalendarDate(), _this.date.getTime())
  51. }, 1000)
  52. },
  53. // 渲染
  54. _render: function (date, calendarDate, time) {
  55. this._setNumberImage( this.elem.hh, time[ 0])
  56. this._setNumberImage( this.elem.hl, time[ 1])
  57. this._setNumberImage( this.elem.mh, time[ 2])
  58. this._setNumberImage( this.elem.ml, time[ 3])
  59. this._setNumberImage( this.elem.sh, time[ 4])
  60. this._setNumberImage( this.elem.sl, time[ 5])
  61. this.elem.date.innerText = date[ 2] + " " +date[ 0] + "-" +date[ 1]
  62. this.elem.calendarDate.innerText = calendarDate
  63. },
  64. // 设置数字图片
  65. _setNumberImage: function (elem, value) {
  66. elem.style.backgroundImage = "url(./assets/img/" + value + ".svg)";
  67. }
  68. }
  69. var myWatchMeter = new WatchMeter()
  70. </script>
  71. </html>

js代码


  
  1. // 生成时间 农历 公历 时间
  2. function TimeGeneration() {
  3. }
  4. TimeGeneration.prototype = {
  5. constructor: TimeGeneration,
  6. WEEKDAY_NAME: [ "周日", "周一", "周二", "周三", "周四", "周五", "周六"],
  7. NUMBER_STRING: "一二三四五六七八九十",
  8. MONTH_STRING: "正二三四五六七八九十冬腊",
  9. MONTH_ADD: [ 0, 31, 59, 90, 120, 151, 181, 212, 243, 273, 304, 334],
  10. CALENDAR_DATA: [ 0xA4B, 0x5164B, 0x6A5, 0x6D4, 0x415B5, 0x2B6, 0x957, 0x2092F, 0x497, 0x60C96, 0xD4A, 0xEA5, 0x50DA9, 0x5AD, 0x2B6, 0x3126E, 0x92E, 0x7192D, 0xC95, 0xD4A, 0x61B4A, 0xB55, 0x56A, 0x4155B, 0x25D, 0x92D, 0x2192B, 0xA95, 0x71695, 0x6CA, 0xB55, 0x50AB5, 0x4DA, 0xA5B, 0x30A57, 0x52B, 0x8152A, 0xE95, 0x6AA, 0x615AA, 0xAB5, 0x4B6, 0x414AE, 0xA57, 0x526, 0x31D26, 0xD95, 0x70B55, 0x56A, 0x96D, 0x5095D, 0x4AD, 0xA4D, 0x41A4D, 0xD25, 0x81AA5, 0xB54, 0xB6A, 0x612DA, 0x95B, 0x49B, 0x41497, 0xA4B, 0xA164B, 0x6A5, 0x6D4, 0x615B4, 0xAB6, 0x957, 0x5092F, 0x497, 0x64B, 0x30D4A, 0xEA5, 0x80D65, 0x5AC, 0xAB6, 0x5126D, 0x92E, 0xC96, 0x41A95, 0xD4A, 0xDA5, 0x20B55, 0x56A, 0x7155B, 0x25D, 0x92D, 0x5192B, 0xA95, 0xB4A, 0x416AA, 0xAD5, 0x90AB5, 0x4BA, 0xA5B, 0x60A57, 0x52B, 0xA93, 0x40E95],
  11. _getBit: function (m, n) {
  12. return (m >> n) & 1;
  13. },
  14. // 获取时间 array
  15. getTime: function () {
  16. var time = new Date();
  17. return [ parseInt(time.getHours() / 10),
  18. parseInt(time.getHours() % 10),
  19. parseInt(time.getMinutes() / 10),
  20. parseInt(time.getMinutes() % 10),
  21. parseInt(time.getSeconds() / 10),
  22. parseInt(time.getSeconds() % 10)]
  23. },
  24. // 获取公历日期 array
  25. getDate: function () {
  26. var date = new Date();
  27. return [
  28. date.getMonth() + 1,
  29. date.getDate(),
  30. this.WEEKDAY_NAME[date.getDay()]
  31. ]
  32. },
  33. // 获取农历日期 string
  34. getCalendarDate: function () {
  35. var calendar = new Date();
  36. var tmp = calendar.getFullYear();
  37. if (tmp < 1900) {
  38. tmp += 1900;
  39. }
  40. var total = (tmp - 1921) * 365 + Math.floor((tmp - 1921) / 4) + this.MONTH_ADD[calendar.getMonth()] + calendar.getDate() - 38;
  41. if (calendar.getFullYear() % 4 == 0 && calendar.getMonth() > 1) {
  42. total++;
  43. }
  44. var isEnd = false;
  45. var n, m, k
  46. for (m = 0; ; m++) {
  47. k = ( this.CALENDAR_DATA[m] < 0xfff) ? 11 : 12;
  48. for (n = k; n >= 0; n--) {
  49. if (total <= 29 + this._getBit( this.CALENDAR_DATA[m], n)) {
  50. isEnd = true;
  51. break;
  52. }
  53. total = total - 29 - this._getBit( this.CALENDAR_DATA[m], n);
  54. }
  55. if (isEnd) break;
  56. }
  57. var month = k - n + 1;
  58. var day = total;
  59. if (k == 12) {
  60. if (month == Math.floor( this.CALENDAR_DATA[m] / 0x10000) + 1) {
  61. month = 1 - month;
  62. }
  63. if (month > Math.floor( this.CALENDAR_DATA[m] / 0x10000) + 1) {
  64. month--;
  65. }
  66. }
  67. var tmp = "";
  68. if (month < 1) {
  69. tmp += "(闰)";
  70. tmp += this.MONTH_STRING.charAt(-month - 1);
  71. } else {
  72. tmp += this.MONTH_STRING.charAt(month - 1);
  73. }
  74. tmp += "月";
  75. tmp += (day < 11) ? "初" : ((day < 20) ? "十" : ((day < 30) ? "廿" : "三十"));
  76. if (day % 10 != 0 || day == 10) {
  77. tmp += this.NUMBER_STRING.charAt((day - 1) % 10);
  78. }
  79. return tmp;
  80. }
  81. }

CSS代码


  
  1. .jun-meter {
  2. position: relative;
  3. width: 640px;
  4. height: 640px;
  5. background-image: url( "../img/ring.svg");
  6. background-repeat: no-repeat;
  7. background-size: 100%;
  8. margin: auto;
  9. margin-top: 7%;
  10. }
  11. .jun-time-rect {
  12. position: absolute;
  13. width: 30px;
  14. height: 80px;
  15. left: 275px;
  16. top: 180px;
  17. background-image: url( "../img/rect.svg");
  18. background-size: 40px 40px;
  19. }
  20. .jun-time-h-h {
  21. position: absolute;
  22. width: 100px;
  23. height: 100px;
  24. left: 140px;
  25. top: 170px;
  26. background-image: url( "../img/8.svg");
  27. background-repeat: no-repeat;
  28. background-size: 100%;
  29. }
  30. .jun-time-h-l {
  31. position: absolute;
  32. width: 100px;
  33. height: 100px;
  34. left: 200px;
  35. top: 170px;
  36. background-image: url( "../img/8.svg");
  37. background-repeat: no-repeat;
  38. background-size: 100%;
  39. }
  40. .jun-time-m-h {
  41. position: absolute;
  42. width: 100px;
  43. height: 100px;
  44. left: 290px;
  45. top: 170px;
  46. background-image: url( "../img/8.svg");
  47. background-repeat: no-repeat;
  48. background-size: 100%;
  49. }
  50. .jun-time-m-l {
  51. position: absolute;
  52. width: 100px;
  53. height: 100px;
  54. left: 350px;
  55. top: 170px;
  56. background-image: url( "../img/8.svg");
  57. background-repeat: no-repeat;
  58. background-size: 100%;
  59. }
  60. .jun-time-s-h {
  61. position: absolute;
  62. width: 60px;
  63. height: 60px;
  64. left: 430px;
  65. top: 208px;
  66. background-image: url( "../img/8.svg");
  67. background-repeat: no-repeat;
  68. background-size: 100%;
  69. }
  70. .jun-time-s-l {
  71. position: absolute;
  72. width: 60px;
  73. height: 60px;
  74. left: 470px;
  75. top: 208px;
  76. background-image: url( "../img/8.svg");
  77. background-repeat: no-repeat;
  78. background-size: 100%;
  79. }
  80. .jun-calendar-date {
  81. position: absolute;
  82. width: 120px;
  83. height: 30px;
  84. left: 460px;
  85. top: 310px;
  86. line-height: 30px;
  87. font-size: 20px;
  88. text-align: center;
  89. }
  90. .jun-date {
  91. position: absolute;
  92. width: 120px;
  93. height: 30px;
  94. left: 460px;
  95. top: 345px;
  96. line-height: 30px;
  97. font-size: 20px;
  98. text-align: center;
  99. }
  100. .jun-human{
  101. position: absolute;
  102. width: 150px;
  103. height: 150px;
  104. left: 250px;
  105. top: 280px;
  106. background-image: url( "../img/human.gif");
  107. background-repeat: no-repeat;
  108. background-size: 100%;
  109. }

三、源码下载

CSDN下载地址: https://download.csdn.net/download/weixin_43532890/16011429

百度网盘下载地址:https://pan.baidu.com/s/1SFOFQCIW1Jn9J5u0HjTQKg  密码: 5mv9


转载:https://blog.csdn.net/weixin_43532890/article/details/115047940
查看评论
* 以上用户言论只代表其个人观点,不代表本网站的观点或立场