基于JavaScript实现网红太空人表盘的完整代码

一、效果展示

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

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

二、源代码

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 = {
 constructor: TimeGeneration,

 WEEKDAY_NAME: ["周日", "周一", "周二", "周三", "周四", "周五", "周六"],
 NUMBER_STRING: "一二三四五六七八九十",
 MONTH_STRING: "正二三四五六七八九十冬腊",
 MONTH_ADD: [0, 31, 59, 90, 120, 151, 181, 212, 243, 273, 304, 334],
 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],

 _getBit: function (m, n) {
  return (m >> n) & 1;
 },

 // 获取时间 array
 getTime: function () {
  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
 getDate: function () {
  var date = new Date();
  return [
   date.getMonth() + 1,
   date.getDate(),
   this.WEEKDAY_NAME[date.getDay()]
  ]
 },

 // 获取农历日期 string
 getCalendarDate: function () {
  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;
 width: 640px;
 height: 640px;
 background-image: url("../img/ring.svg");
 background-repeat: no-repeat;
 background-size: 100%;
 margin: auto;
 margin-top: 7%;
}

.jun-time-rect {
 position: absolute;
 width: 30px;
 height: 80px;
 left: 275px;
 top: 180px;
 background-image: url("../img/rect.svg");
 background-size: 40px 40px;
}

.jun-time-h-h {
 position: absolute;
 width: 100px;
 height: 100px;
 left: 140px;
 top: 170px;
 background-image: url("../img/8.svg");
 background-repeat: no-repeat;
 background-size: 100%;
}

.jun-time-h-l {
 position: absolute;
 width: 100px;
 height: 100px;
 left: 200px;
 top: 170px;
 background-image: url("../img/8.svg");
 background-repeat: no-repeat;
 background-size: 100%;
}

.jun-time-m-h {
 position: absolute;
 width: 100px;
 height: 100px;
 left: 290px;
 top: 170px;
 background-image: url("../img/8.svg");
 background-repeat: no-repeat;
 background-size: 100%;
}

.jun-time-m-l {
 position: absolute;
 width: 100px;
 height: 100px;
 left: 350px;
 top: 170px;
 background-image: url("../img/8.svg");
 background-repeat: no-repeat;
 background-size: 100%;
}

.jun-time-s-h {
 position: absolute;
 width: 60px;
 height: 60px;
 left: 430px;
 top: 208px;
 background-image: url("../img/8.svg");
 background-repeat: no-repeat;
 background-size: 100%;
}

.jun-time-s-l {
 position: absolute;
 width: 60px;
 height: 60px;
 left: 470px;
 top: 208px;
 background-image: url("../img/8.svg");
 background-repeat: no-repeat;
 background-size: 100%;
}

.jun-calendar-date {
 position: absolute;
 width: 120px;
 height: 30px;
 left: 460px;
 top: 310px;
 line-height: 30px;
 font-size: 20px;
 text-align: center;
}

.jun-date {
 position: absolute;
 width: 120px;
 height: 30px;
 left: 460px;
 top: 345px;
 line-height: 30px;
 font-size: 20px;
 text-align: center;
}

.jun-human{
 position: absolute;
 width: 150px;
 height: 150px;
 left: 250px;
 top: 280px;
 background-image: url("../img/human.gif");
 background-repeat: no-repeat;
 background-size: 100%;
}

到此这篇关于基于JavaScript实现网红太空人表盘的完整代码的文章就介绍到这了,更多相关JavaScript实现网红太空人表盘内容请搜索我们以前的文章或继续浏览下面的相关文章希望大家以后多多支持我们!

(0)

相关推荐

  • JavaScript通如何过RGraph实现动态仪表盘

    目前针对于统计图的制作方法有很多,可以直接利用快逸报表中的自带统计图,还可以通过自定义统计图个性化定制,当然除此之外,在新的HTML5标准中,新增了一个非常重要的元素-canvas元素.使用该元素,可以在页面中直接进行各种复杂图形的制作.因此,如果使用该元素绘制统计图,比之前使用服务器端控件来生成统计图的方法更加具有优越性,因为使用了该元素之后,绘制统计图的工作是直接在客户端进行的,而不再是在服务器端所完成的了.这不仅意味着不再占用服务器端的资源,而且意味着可以直接利用客户端计算机的强大资源,绘

  • js canvas仿支付宝芝麻信用分仪表盘

    这是一个仿支付宝芝麻信用分的一个canvas,其实就是一个动画仪表盘. 首先, 上原图: 这个是在下支付宝上的截图,分低各位见笑了.然后看下我用canvas实现的效果图: <canvas id="canvas" width="400" height="700" data-score='724'></canvas> <!-- 设置data-score,分数区间[400, 900] --> 唉,总感觉不像.这个是G

  • js实现手表表盘时钟与圆周运动

    苹果手表表盘时钟与js圆周运动 实现结果 需求分析: 1.时钟时间按照北京时间进行显示; 2.时针.分针.秒针按照时钟运转标准进行运转: 3.小球跟随秒表围绕表盘进行圆周运动. 代码分析 1.html结构:时针.分针.秒针.小球分别用一个div,将他们一起放到一个大的div中: 2.css样式:表盘布局多使用相对定位与绝对定位,将表针与各时刻标刻移动到特定的位置: 3.js行为:为了实现动态获取时间,可以使用var now=new Date(),再利用定时器setInterval,实现每经过1s

  • js canvas实现适用于移动端的百分比仪表盘dashboard

    本文为大家分享了canvas实现适用于移动端的百分比仪表盘,供大家参考,具体内容如下 由于最近工作中,经常会遇到一些动态百分比的仪表盘,一开始都是用图片样式方式实现: 但是随着越来越多的项目,决定用canvas绘制一个简易的仪表盘,便于以后项目中直接使用: 现版本只是书写为方法形式,也许之后会有时间对其优化为插件形式. 简简单单而已,以下直接给出代码和执行过程中的三张截图: <!doctype html> <html lang="en"> <head>

  • D3.js实现简洁实用的动态仪表盘的示例

    本文介绍了D3.js实现简洁实用的动态仪表盘的示例,分享给大家,具体如下: 动态效果图: 仪表盘效果图 细看上面的动态效果图,可以发现: 一个值变换到一个新的值时,是一个渐变的过程: 圆弧末尾有一个竖线,作为仪表盘的指针,在仪表盘数值变化时,有一个弹性的动画效果. 一开始,我是用Echarts来实现仪表盘,但是它无法满足上面的两点需求.所以后来改成用D3.js. D3.js可以完美地实现图表的定制,从细节上,完美地满足我们的需求. 初始化仪表盘 1.首先定义一个svg元素: <svg id=&quo

  • 基于JavaScript实现网红太空人表盘的完整代码

    一.效果展示 用javascript写的一个太空人表盘. http://xiazai.jb51.net/202103/yuanma/Watch_jb51.rar 二.源代码 html代码 <html> <head> <title>太空人表盘</title> <meta charset="UTF-8"> <link href="./assets/css/style.css" rel="exte

  • 基于Json序列化和反序列化通用的封装完整代码

    1. 最近项目已经上线了 ,闲暇了几天 想将JSON的序列化以及反序列化进行重新的封装一下本人定义为JSONHelp,虽然Microsoft 已经做的很好了.但是我想封装一套为自己开发的项目使用.方便后期的扩展以及开发使用. 2. 什么是 JSON ? JSON:JavaScript 对象表示法(JavaScript Object Notation).JSON 是存储和交换文本信息的语法.类似 XML.JSON 比 XML 更小.更快,更易解析.  现在开发Web应用程序 JSON 是 必不可少

  • javascript+css3开发打气球小游戏完整代码

    效果知识点: css3画气球, 自定义属性运用,随机阵列, DOM元素操作,高级回调函数与参数复传,动态布局,鼠标事件,定时器运用,CSS3新增样式等. css代码如下: <style> {margin:0;padding:0;} body{background:#434343;overflow:hidden} .balloon{ position:absolute; left:0; top:0; margin:auto; width:160px; height:160px; 圆角: 左上 右

  • Java编程实现基于图的深度优先搜索和广度优先搜索完整代码

    为了解15puzzle问题,了解了一下深度优先搜索和广度优先搜索.先来讨论一下深度优先搜索(DFS),深度优先的目的就是优先搜索距离起始顶点最远的那些路径,而广度优先搜索则是先搜索距离起始顶点最近的那些路径.我想着深度优先搜索和回溯有什么区别呢?百度一下,说回溯是深搜的一种,区别在于回溯不保留搜索树.那么广度优先搜索(BFS)呢?它有哪些应用呢?答:最短路径,分酒问题,八数码问题等.言归正传,这里笔者用java简单实现了一下广搜和深搜.其中深搜是用图+栈实现的,广搜使用图+队列实现的,代码如下:

  • 基于JavaScript实现 网页切出 网站title变化代码

    废话不多说了,直接给大家贴代码了,具体代码如下所示: <!DOCTYPE html> <html> <head> <meta charset="UTF-"> <title>这里是网站标题</title> </head> <body> <script type="text/javascript"> document.body.onfocus = function

  • javascript圆盘抽奖程序实现原理和完整代码例子

    效果预览: 一.模拟抽奖的实现过程 旋转原理:当支持CSS3属性采用transform: rotate(角度deg)设置,当角度为正数时顺时针旋转,当为负数时逆时针旋转.如果是IE8及其以下,采用采用绝对定位设置top和left,模拟角度旋转. run方法,参数angle指角度 复制代码 代码如下: function run(angle) {                    if (isIE) {                        cosDeg = Math.cos(angle

  • 基于JavaScript实现简单的随机抽奖小程序

    对于抽奖这样的小程序使用诸如VB,Delphi等工具来实现会比较的方便,由于本人机器上没有装这样的应用程序,所以只能另寻其道.为了使抽奖程序能够无需配置平台直接可以在任何一台机器上运行,开发工具和编译运行工具也能够经可能简单(诸如text文本即可编辑,window系统自带的浏览器即可编译运行的情况),决定尝试使用javascript来做.本人对javascript的研究不深,平时主要用于网站开发中对来自客户端的数据进行有效性判断(基于安全性的考虑,安全性要求高的网站尽量使用服务器端语言对数据有效

  • 如何基于javascript实现贪吃蛇游戏

    这篇文章主要介绍了如何基于javascript实现贪吃蛇游戏,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友可以参考下 html代码: <div class="content"> <div class="btn startBtn"> <!-- 开始按钮 --> <button type="button"></button> </div>

  • 基于JavaScript实现抽奖系统

    用JavaScript实现一个简单的抽奖系统,有[开始]按钮和[停止]按钮. 功能: - 点开始按钮开始抽奖,随机出现奖品名称: - 点停止按钮即可停止抽奖: - 按下回车键可切换开始抽奖和停止抽奖. 效果 html代码: 创建html结构,最基础的要含有显示的奖品名称和开始.停止按钮. <!doctype html> <html> <head> <title>抽奖系统</title> <meta charset="utf-8&q

  • 基于javascript实现按圆形排列DIV元素(一)

    效果图: 一.分析图: 绿色边框内:外层的DIV元素,相对定位; 白色圆形框:辅助分析的想象形状; 白点:为白色圆形的圆心点,中心点,点o; 圆心角:角NOG; 黄色:需要按圆形排列的,绝对定位的DIV元素; 红色点:为每个黄色DIV的坐标点;即绝对定位时的元素,left值 和 top值 ,设置的点; 二.涉及到的概念定义: 2.1.弧度:弧度是角的度量单位. (红色部分为弧长,角A为弧长对应的圆心角) 弧长等于半径的弧,其所对的圆心角为1弧度.(即两条射线从圆心向圆周射出,形成一个夹角和夹角正

随机推荐