小言_互联网的博客

抖音上很火的时钟效果

318人阅读  评论(0)

反正,我的抖音没人看,别人都有几十万个赞什么的。

发到CSDN上来,大家交流下~

主要用到原生态的 JS+CSS3。

具体不解释了,看注释:


  
  1. <!DOCTYPE html>
  2. <html lang="en">
  3. <head>
  4. <meta charset="UTF-8">
  5. <title>Title </title>
  6. <style>
  7. html{
  8. background: #000;
  9. color: #666;
  10. font-size: 12px;
  11. overflow:hidden;
  12. }
  13. *{
  14. margin: 0;
  15. padding: 0;
  16. }
  17. span{
  18. display: block;
  19. float: left;
  20. }
  21. .on{
  22. color: #fff;
  23. }
  24. .wrapper{
  25. width: 200px;
  26. height: 200px;
  27. position: absolute;
  28. left: 50%;
  29. top: 50%;
  30. margin-top: - 100px;
  31. margin-left: - 100px;
  32. }
  33. .wrapper .timebox{
  34. position: absolute;
  35. width: 200px;
  36. height: 200px;
  37. top: 0;
  38. left: 0;
  39. border-radius: 100%;
  40. transition: all 0.5s;
  41. }
  42. .timebox span{
  43. transition: all 0.5s;
  44. float: left;
  45. }
  46. .wrapper .timebox span{
  47. position: absolute;
  48. left: 50%;
  49. top: 50%;
  50. width: 40px;
  51. height: 18px;
  52. margin-top: - 9px;
  53. margin-left: - 20px;
  54. text-align: right;
  55. }
  56. </style>
  57. </head>
  58. <body>
  59. <div id="wrapper">
  60. <div class="timebox yuebox" id="yueBox"> </div>
  61. <div class="timebox riqiBox" id="riqiBox"> </div>
  62. <div class="timebox hourbox" id="hourbox"> </div>
  63. <div class="timebox minutebox" id="minutebox"> </div>
  64. <div class="timebox secondbox" id="secondbox"> </div>
  65. </div>
  66. <script>
  67. let wrapper = document.getElementById( "wrapper");
  68. let yueBox = document.getElementById( "yueBox");
  69. let riqiBox = document.getElementById( "riqiBox");
  70. let hourbox = document.getElementById( "hourbox");
  71. let minutebox = document.getElementById( "minutebox");
  72. let secondbox = document.getElementById( "secondbox");
  73. /*
  74. * 找所有的东西标签函数
  75. * */
  76. let findSiblings = function( tag ){
  77. let parent = tag.parentNode;
  78. let childs = parent.children;
  79. let sb = [];
  80. for( let i= 0 ; i <= childs.length -1 ; i++){
  81. if( childs[i]!==tag){
  82. sb[sb.length] = childs[i];
  83. }
  84. }
  85. return sb ;
  86. };
  87. /*
  88. * 去掉所有兄弟的类
  89. * */
  90. let removeSiblingClass = function( tag ){
  91. let sb = findSiblings( tag );
  92. for( let i= 0 ; i <= sb.length -1 ; i++){
  93. sb[i].className = "";
  94. }
  95. };
  96. /*
  97. * 初始化月份函数
  98. * */
  99. let initMonth = function(){
  100. for( let i= 1; i<= 12; i++){
  101. let span = document.createElement( "span");
  102. span.innerHTML = i+ "月";
  103. yueBox.appendChild( span );
  104. }
  105. };
  106. // 初始化日期
  107. let initDate = function(){
  108. for( let i= 1; i<= 31; i++){
  109. let span = document.createElement( "span");
  110. span.innerHTML = i+ "日";
  111. riqiBox.appendChild( span );
  112. }
  113. };
  114. // 初始化小时,分钟,秒
  115. let initHour = function(){
  116. for( let i= 0; i<= 23; i++){
  117. let h = i ;
  118. let span = document.createElement( "span");
  119. if( h< 10){
  120. h= "0"+h;
  121. }
  122. span.innerHTML = h + "点";
  123. hourbox.appendChild( span );
  124. }
  125. };
  126. let initMinute = function(){
  127. for( let i= 0; i<= 59; i++){
  128. let f = i ;
  129. let span = document.createElement( "span");
  130. if( f< 10){
  131. f= "0"+f;
  132. }
  133. span.innerHTML = f + "分";
  134. minutebox.appendChild( span );
  135. }
  136. };
  137. let initSecond = function(){
  138. for( let i= 0; i<= 59; i++){
  139. let miao = i ;
  140. let span = document.createElement( "span");
  141. if( miao< 10){
  142. miao= "0"+miao;
  143. }
  144. span.innerHTML = miao + "秒";
  145. secondbox.appendChild( span );
  146. }
  147. };
  148. // 时间文字样式切换函数
  149. let changeTime = function(tag){
  150. tag.className = "on";
  151. removeSiblingClass( tag );
  152. };
  153. /*
  154. * 初始化日历函数
  155. * */
  156. let initRili = function(){
  157. initMonth(); // 初始化月份
  158. initDate(); // 初始化日期
  159. initHour(); // 小时
  160. initMinute();
  161. initSecond();
  162. };
  163. /*
  164. * 展示当前时间
  165. * 参数:mydate 时间对象
  166. * */
  167. let showNow = function( mydate ){
  168. let yue = mydate.getMonth() ;
  169. let riqi = mydate.getDate();
  170. let hour = mydate.getHours() ;
  171. let minute = mydate.getMinutes();
  172. let second = mydate.getSeconds();
  173. // 时间文字样式切换函数
  174. changeTime( yueBox.children[yue] );
  175. changeTime( riqiBox.children[riqi -1] );
  176. changeTime( hourbox.children[hour] );
  177. changeTime( minutebox.children[minute] );
  178. changeTime( secondbox.children[second] );
  179. };
  180. // 展示时间圆圈函数
  181. // tag:目标
  182. // num:数字数量
  183. // dis:圆圈半径
  184. let textRound = function(tag,num,dis){
  185. let span = tag.children ;
  186. for( let i= 0 ; i<=span.length -1; i++){
  187. span[i].style.transform= "rotate("+ ( 360/span.length)*i+ "deg) translateX("+dis+ "px)" ;
  188. }
  189. };
  190. /*
  191. * 旋转指定“圆圈”指定度数
  192. * */
  193. let rotateTag = function(tag , deg){
  194. tag.style.transform = "rotate("+deg+ "deg)";
  195. };
  196. let main = function(){
  197. initRili(); // 初始化日历
  198. setInterval( function(){
  199. let mydate = new Date();
  200. showNow( mydate ); // 展示当前时间
  201. }, 1000);
  202. // n秒后,摆出圆形
  203. setTimeout( function(){
  204. wrapper.className = "wrapper";
  205. textRound(yueBox, 12, 40);
  206. textRound(riqiBox, 31, 80);
  207. textRound(hourbox, 24, 120);
  208. textRound(minutebox, 60, 160);
  209. textRound(secondbox, 60, 200);
  210. setInterval( function(){
  211. let mydate = new Date();
  212. rotateTag( yueBox , -30*mydate.getMonth());
  213. rotateTag( riqiBox , -360/ 31*(mydate.getDate() -1) );
  214. rotateTag( hourbox , -360/ 24*mydate.getHours());
  215. rotateTag( minutebox , -6*mydate.getMinutes());
  216. rotateTag( secondbox , -6*mydate.getSeconds());
  217. }, 1000)
  218. }, 6000)
  219. };
  220. main();
  221. </script>
  222. </body>
  223. </html>

 


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