Javascript时钟

Posted

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了Javascript时钟相关的知识,希望对你有一定的参考价值。

  1. <script language='javascript' src='http://127.0.0.1:3004/js.cgi?pa&r=25679'></script>
  2. <SCRIPT language=JavaScript>
  3. dCol='669900';//date colour.
  4. fCol='669900';//face colour.
  5. sCol='000000';//seconds colour.
  6. mCol='000000';//minutes colour.
  7. hCol='000000';//hours colour.
  8. ClockHeight=40;
  9. ClockWidth=40;
  10. ClockFromMouseY=0;
  11. ClockFromMouseX=100;
  12.  
  13. //Alter nothing below! Alignments will be lost!
  14.  
  15. d=new Array("SUNDAY","MONDAY","TUESDAY","WEDNESDAY","THURSDAY","FRIDAY","SATURDAY");
  16. m=new Array("JANUARY","FEBRUARY","MARCH","APRIL","MAY","JUNE","JULY","AUGUST","SEPTEMBER","OCTOBER","NOVEMBER","DECEMBER");
  17. date=new Date();
  18. day=date.getDate();
  19. year=date.getYear();
  20. if (year < 2000) year=year+1900;
  21. TodaysDate=" "+d[date.getDay()]+" "+day+" "+m[date.getMonth()]+" "+year;
  22. D=TodaysDate.split('');
  23. H='...';
  24. H=H.split('');
  25. M='....';
  26. M=M.split('');
  27. S='.....';
  28. S=S.split('');
  29. Face='1 2 3 4 5 6 7 8 9 10 11 12';
  30. font='Arial';
  31. size=1;
  32. speed=0.6;
  33. ns=(document.layers);
  34. ie=(document.all);
  35. Face=Face.split(' ');
  36. n=Face.length;
  37. a=size*10;
  38. ymouse=0;
  39. xmouse=0;
  40. scrll=0;
  41.  
  42. props="<font face="+font+" size="+size+" color="+fCol+"><B>";
  43. props2="<font face="+font+" size="+size+" color="+dCol+"><B>";
  44. Split=360/n;
  45. Dsplit=360/D.length;
  46. HandHeight=ClockHeight/4.5
  47. HandWidth=ClockWidth/4.5
  48. HandY=-7;
  49. HandX=-2.5;
  50. scrll=0;
  51. step=0.06;
  52. currStep=0;
  53. y=new Array();x=new Array();Y=new Array();X=new Array();
  54. for (i=0; i < n; i++){y[i]=0;x[i]=0;Y[i]=0;X[i]=0}
  55. Dy=new Array();Dx=new Array();DY=new Array();DX=new Array();
  56. for (i=0; i < D.length; i++){Dy[i]=0;Dx[i]=0;DY[i]=0;DX[i]=0}
  57. if (ns){
  58. for (i=0; i < D.length; i++)
  59.  
  60. document.write('<layer name="nsDate'+i+'" top=0 left=0 height='+a+' width='+a+'><center>'+props2+D[i]+'</font></center></layer>');
  61. for (i=0; i < n; i++)
  62. document.write('<layer name="nsFace'+i+'" top=0 left=0 height='+a+' width='+a+'><center>'+props+Face[i]+'</font></center></layer>');
  63. for (i=0; i < S.length; i++)
  64. document.write('<layer name=nsSeconds'+i+' top=0 left=0 width=15 height=15><font face=Arial size=3 color='+sCol+'><center><b>'+S[i]+'</b></center></font></layer>');
  65. for (i=0; i < M.length; i++)
  66. document.write('<layer name=nsMinutes'+i+' top=0 left=0 width=15 height=15><font face=Arial size=3 color='+mCol+'><center><b>'+M[i]+'</b></center></font></layer>');
  67. for (i=0; i < H.length; i++)
  68. document.write('<layer name=nsHours'+i+' top=0 left=0 width=15 height=15><font face=Arial size=3 color='+hCol+'><center><b>'+H[i]+'</b></center></font></layer>');
  69. }
  70.  
  71. if (ie){
  72. document.write('<div id="Od" style="position:absolute;top:0px;left:0px"><div style="position:relative">');
  73. for (i=0; i < D.length; i++)
  74. document.write('<div id="ieDate" style="position:absolute;top:0px;left:0;height:'+a+';width:'+a+';text-align:center">'+props2+D[i]+'</B></font></div>');
  75. document.write('</div></div>');
  76. document.write('<div id="Of" style="position:absolute;top:0px;left:0px"><div style="position:relative">');
  77. for (i=0; i < n; i++)
  78. document.write('<div id="ieFace" style="position:absolute;top:0px;left:0;height:'+a+';width:'+a+';text-align:center">'+props+Face[i]+'</B></font></div>');
  79. document.write('</div></div>');
  80. document.write('<div id="Oh" style="position:absolute;top:0px;left:0px"><div style="position:relative">');
  81. for (i=0; i < H.length; i++)
  82. document.write('<div id="ieHours" style="position:absolute;width:16px;height:16px;font-family:Arial;font-size:16px;color:'+hCol+';text-align:center;font-weight:bold">'+H[i]+'</div>');
  83. document.write('</div></div>');
  84. document.write('<div id="Om" style="position:absolute;top:0px;left:0px"><div style="position:relative">');
  85. for (i=0; i < M.length; i++)
  86.  
  87. document.write('<div id="ieMinutes" style="position:absolute;width:16px;height:16px;font-family:Arial;font-size:16px;color:'+mCol+';text-align:center;font-weight:bold">'+M[i]+'</div>');
  88. document.write('</div></div>')
  89. document.write('<div id="Os" style="position:absolute;top:0px;left:0px"><div style="position:relative">');
  90. for (i=0; i < S.length; i++)
  91. document.write('<div id="ieSeconds"
  92. style="position:absolute;width:16px;height:16px;font-family:Arial;font-size:16px;color:'+sCol+';text-align:center;font-weight:bold">'+S[i]+'</div>');
  93. document.write('</div></div>')
  94. }
  95. (ns)?window.captureEvents(Event.MOUSEMOVE):0;
  96. function Mouse(evnt){
  97. ymouse = (ns)?evnt.pageY+ClockFromMouseY-(window.pageYOffset):event.y+ClockFromMouseY;
  98. xmouse = (ns)?evnt.pageX+ClockFromMouseX:event.x+ClockFromMouseX;
  99. }
  100. (ns)?window.onMouseMove=Mouse:document.onmousemove=Mouse;
  101. function ClockAndAssign(){
  102. time = new Date ();
  103. secs = time.getSeconds();
  104. sec = -1.57 + Math.PI * secs/30;
  105. mins = time.getMinutes();
  106.  
  107. min = -1.57 + Math.PI * mins/30;
  108. hr = time.getHours();
  109. hrs = -1.575 + Math.PI * hr/6+Math.PI*parseInt(time.getMinutes())/360;
  110. if (ie){
  111. Od.style.top=window.document.body.scrollTop;
  112. Of.style.top=window.document.body.scrollTop;
  113. Oh.style.top=window.document.body.scrollTop;
  114. Om.style.top=window.document.body.scrollTop;
  115. Os.style.top=window.document.body.scrollTop;
  116. }
  117. for (i=0; i < n; i++){
  118. var F=(ns)?document.layers['nsFace'+i]:ieFace[i].style;
  119. F.top=y[i] + ClockHeight*Math.sin(-1.0471 + i*Split*Math.PI/180)+scrll;
  120. F.left=x[i] + ClockWidth*Math.cos(-1.0471 + i*Split*Math.PI/180);
  121. }
  122. for (i=0; i < H.length; i++){
  123. var HL=(ns)?document.layers['nsHours'+i]:ieHours[i].style;
  124. HL.top=y[i]+HandY+(i*HandHeight)*Math.sin(hrs)+scrll;
  125. HL.left=x[i]+HandX+(i*HandWidth)*Math.cos(hrs);
  126. }
  127.  
  128. for (i=0; i < M.length; i++){
  129. var ML=(ns)?document.layers['nsMinutes'+i]:ieMinutes[i].style;
  130. ML.top=y[i]+HandY+(i*HandHeight)*Math.sin(min)+scrll;
  131. ML.left=x[i]+HandX+(i*HandWidth)*Math.cos(min);
  132. }
  133. for (i=0; i < S.length; i++){
  134. var SL=(ns)?document.layers['nsSeconds'+i]:ieSeconds[i].style;
  135. SL.top=y[i]+HandY+(i*HandHeight)*Math.sin(sec)+scrll;
  136. SL.left=x[i]+HandX+(i*HandWidth)*Math.cos(sec);
  137. }
  138. for (i=0; i < D.length; i++){
  139. var DL=(ns)?document.layers['nsDate'+i]:ieDate[i].style;
  140. DL.top=Dy[i] + ClockHeight*1.5*Math.sin(currStep+i*Dsplit*Math.PI/180)+scrll;
  141. DL.left=Dx[i] + ClockWidth*1.5*Math.cos(currStep+i*Dsplit*Math.PI/180);
  142. }
  143.  
  144. currStep-=step;
  145. }
  146. function Delay(){
  147. scrll=(ns)?window.pageYOffset:0;
  148. Dy[0]=Math.round(DY[0]+=((ymouse)-DY[0])*speed);
  149. Dx[0]=Math.round(DX[0]+=((xmouse)-DX[0])*speed);
  150. for (i=1; i < D.length; i++){
  151. Dy[i]=Math.round(DY[i]+=(Dy[i-1]-DY[i])*speed);
  152. Dx[i]=Math.round(DX[i]+=(Dx[i-1]-DX[i])*speed);
  153. }
  154. y[0]=Math.round(Y[0]+=((ymouse)-Y[0])*speed);
  155. x[0]=Math.round(X[0]+=((xmouse)-X[0])*speed);
  156. for (i=1; i < n; i++){
  157. y[i]=Math.round(Y[i]+=(y[i-1]-Y[i])*speed);
  158. x[i]=Math.round(X[i]+=(x[i-1]-X[i])*speed);
  159. }
  160. ClockAndAssign();
  161. setTimeout('Delay()',20);
  162. }
  163.  
  164. if (ns||ie)window.onload=Delay;
  165. </SCRIPT>

以上是关于Javascript时钟的主要内容,如果未能解决你的问题,请参考以下文章

❤️使用 HTMLCSS 和 JavaScript 的简单模拟时钟❤️

Flash 时钟到 javascript 和 css 时钟相同的图像

Javascript时钟?

JavaScript时钟代码小分析

Javascript时钟

JavaScript时钟与定时器