jQuery和CSS3 最小清洁模拟时钟

  • 源码大小:384.17KB
  • 所需积分:1积分
  • 源码编号:19JP-3139
  • 浏览次数:776次
  • 最后更新:2023年05月10日
  • 所属栏目:时间和时钟
我要下载
加入收藏
本站默认解压密码:19jp.com 或 19jp_com

简介

一款简单、时尚、响应灵敏的模拟时钟,使用CSS3 2D变换旋转指针。

参见:

  • 10个最好的模拟时钟JavaScript

如何使用它:

1.为模拟时钟创建HTML。

  1. <div id="clockContainer">
  2. <div id="hour"></div>
  3. <div id="minute"></div>
  4. <div id="second"></div>
  5. </div>

2.在CSS中设置指针和钟面的样式。

  1. #clockContainer {
  2. top: 3vw;
  3. position: relative;
  4. width: 40vw;
  5. height: 40vw;
  6. background: url(clock.png) no-repeat;
  7. background-size: 100%;
  8. margin: auto;
  9. opacity: 0.8;
  10. }
  11.  
  12. #hour, #minute, #second {
  13. position: absolute;
  14. background-color: black;
  15. border-radius: 10px;
  16. transform-origin: bottom;
  17. }
  18.  
  19. #hour {
  20. height: 26%;
  21. width: 1.8%;
  22. top: 24%;
  23. left: 48.9%;
  24. opacity: 1;
  25. background-color: rgb(7, 1, 61);
  26. }
  27.  
  28. #minute {
  29. height: 32%;
  30. width: 1.2%;
  31. top: 17%;
  32. left: 49%;
  33. opacity: 0.7;
  34. background-color: rgb(7, 1, 61);
  35. }
  36.  
  37. #second {
  38. background-color: red;
  39. height: 38%;
  40. width: 0.8%;
  41. top: 12%;
  42. left: 49.4%;
  43. opacity: 0.7;
  44. }

3.在文档中加载最新的jQuery库(建议使用瘦版本)。

  1. <script src="/path/to/cdn/jquery.slim.min.js"></script>

4.激活模拟时钟。

  1. setInterval(() => {
  2. date= new Date();
  3. h=date.getHours();
  4. m=date.getMinutes();
  5. s=date.getSeconds();
  6. console.log(h+":"+m+":"+s);
  7.  
  8. hrotation= 30*h + m/2;
  9. mrotation= 6*m;
  10. srotation= 6*s;
  11. console.log(hrotation+":"+mrotation+":"+srotation);
  12.  
  13. $("#hour").css("transform", `rotate(${hrotation}deg)`);
  14. $("#minute").css("transform", `rotate(${mrotation}deg)`);
  15. $("#second").css("transform", `rotate(${srotation}deg)`);
  16.  
  17. }, 1000);

预览截图