[项目实践] 用html+css写一个svg时钟

2314 2
guxinlei 2022-12-21 08:04:47 | 显示全部楼层 |阅读模式
用html+css写一个svg时钟
html文件名为index.html
同目录下css文件名为style.css

html代码:
  1. <!DOCTYPE html>
  2. <html lang="en">
  3. <head>
  4.   <meta charset="UTF-8">
  5.   <meta http-equiv="X-UA-Compatible" content="IE=edge">
  6.   <meta name="viewport" content="width=device-width, initial-scale=1.0">
  7.   <title>svg Clock</title>
  8.   <link rel="stylesheet" href="./style.css">
  9. </head>
  10. <body>
  11.   <div id="time">
  12.     <div class="circle" style="--clr:#ff2972;">
  13.       <div class="dots hr_dot"></div>
  14.       <svg>
  15.         <circle cx="70" cy="70" r="70"></circle>
  16.         <circle cx="70" cy="70" r="70" id="hh"></circle>
  17.       </svg>
  18.       <div id="hours" class="common">00</div>
  19.     </div>

  20.     <div class="circle" style="--clr:#fee800;">
  21.       <div class="dots min_dot"></div>
  22.       <svg>
  23.         <circle cx="70" cy="70" r="70"></circle>
  24.         <circle cx="70" cy="70" r="70" id="mm"></circle>
  25.       </svg>
  26.       <div id="minutes" class="common">00</div>
  27.     </div>

  28.     <div class="circle" style="--clr:#04fc43;">
  29.       <div class="dots sec_dot"></div>
  30.       <svg>
  31.         <circle cx="70" cy="70" r="70"></circle>
  32.         <circle cx="70" cy="70" r="70" id="ss"></circle>
  33.       </svg>
  34.       <div id="seconds" class="common">00</div>
  35.     </div>

  36.     <div class="ap">
  37.       <div id="ampm">AM</div>
  38.     </div>
  39.   </div>

  40.   <script>
  41.     setInterval(() => {
  42.       let hours = document.getElementById('hours')
  43.       let minutes = document.getElementById('minutes')
  44.       let seconds = document.getElementById('seconds')
  45.       let ampm = document.getElementById('ampm')

  46.       let hh = document.getElementById('hh')
  47.       let mm = document.getElementById('mm')
  48.       let ss = document.getElementById('ss')

  49.       let hr_dot = document.querySelector('.hr_dot')
  50.       let min_dot = document.querySelector('.min_dot')
  51.       let sec_dot = document.querySelector('.sec_dot')

  52.       let h = new Date().getHours()
  53.       let m = new Date().getMinutes()
  54.       let s = new Date().getSeconds()
  55.       let am = h > 12 ? 'PM' : 'AM'

  56.       // convert 24hr clock to 12hr clock
  57.       if(h > 12) {
  58.         h = h - 12
  59.       }

  60.       // add zero before single digit number
  61.       h = (h < 10) ? '0' + h : h
  62.       m = (m < 10) ? '0' + m : m
  63.       s = (s < 10) ? '0' + s : s

  64.       // hours.innerHTML = h
  65.       // minutes.innerHTML = m
  66.       // seconds.innerHTML = s

  67.       hours.innerHTML = h + "<br><span>Hours</span></br>"
  68.       minutes.innerHTML = m + "<br><span>Minutes</span></br>"
  69.       seconds.innerHTML = s + "<br><span>Seconds</span></br>"
  70.       ampm.innerHTML = am

  71.       hh.style.strokeDashoffset = 440 - (440 * h) / 12
  72.       // 12 hrs clock

  73.       mm.style.strokeDashoffset = 440 - (440 * m) / 60
  74.       // 60minutes
  75.       ss.style.strokeDashoffset = 440 - (440 * s) / 60
  76.       // 60seconds

  77.       hr_dot.style.transform = `rotate(${h * 30}deg)`
  78.       // 360 / 12 = 30
  79.       min_dot.style.transform =  `rotate(${m * 6}deg)`
  80.       // 360 / 60 = 6
  81.       sec_dot.style.transform =   `rotate(${s * 6}deg)`
  82.       // 360 / 60 = 6
  83.     })
  84.    
  85.   </script>
  86. </body>
  87. </html>
复制代码


css部分:
  1. import url('https://fonts.googleapis.com/css?family=Poppins:200,300,400,500,600,700,800,900&display=swap');

  2. * {
  3.   margin: 0;
  4.   padding: 0;
  5.   box-sizing: border-box;
  6.   font-family: 'Poppins', sans-serif;
  7. }

  8. body {
  9.   display: flex;
  10.   justify-content: center;
  11.   align-items: center;
  12.   min-height: 100vh;
  13.   background: #2f363e;
  14. }

  15. #time {
  16.   display: flex;
  17.   gap: 40px;
  18.   color: #fff;
  19. }

  20. #time .circle {
  21.   position: relative;
  22.   width: 150px;
  23.   height: 150px;
  24.   display: flex;
  25.   justify-content: center;
  26.   align-items: center;
  27. }

  28. /* 修饰svg start */
  29. #time .circle svg {
  30.   position: relative;
  31.   width: 150px;
  32.   height: 150px;
  33.   transform: rotate(270deg);
  34. }
  35. #time .circle svg circle {
  36.   width: 100%;
  37.   height: 100%;
  38.   fill: transparent;
  39.   stroke: #191919;
  40.   stroke-width: 4;
  41.   transform: translate(5px, 5px);
  42. }

  43. #time .circle svg circle:nth-child(2) {
  44.   stroke: var(--clr);
  45.   stroke-dasharray: 440;
  46. }
  47. /* svg end */


  48. #time  div{
  49.   position: absolute;
  50.   text-align: center;
  51.   font-weight: 500;
  52.   font-size: 1.5em;
  53. }

  54. #time .common {
  55.   top: 40px;
  56. }

  57. #time div span {
  58.   position: absolute;
  59.   transform: translateX(-50%) translateY(-10px);
  60.   font-size: 0.35em;
  61.   font-weight: 300;
  62.   letter-spacing: 0.1em;
  63.   text-transform: uppercase;
  64. }

  65. #time .ap {
  66.   position: relative;
  67.   font: 1em;
  68.   transform: translateX(-20px);
  69. }

  70. /* svg圆点 */
  71. .dots {
  72.   position: absolute;
  73.   width: 100%;
  74.   height: 100%;
  75.   z-index: 10;
  76.   display: flex;
  77.   justify-content: center;
  78. }

  79. .dots::before {
  80.   content: '';
  81.   position: absolute;
  82.   top: -3px;
  83.   width: 15px;
  84.   height: 15px;
  85.   background: var(--clr);
  86.   border-radius: 50%;
  87.   box-shadow: 0 0 20px var(--clr),
  88.               0 0 60px var(--clr);
  89. }
复制代码
xeabc1213 2023-5-21 19:05:42 | 显示全部楼层
html+css
index.html
style.css
您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

guxinlei

荣誉红客

关注
  • 24
    主题
  • 0
    粉丝
  • 1
    关注
青,取之于蓝而胜于蓝;冰,水为之而寒于水。

中国红客联盟公众号

联系站长QQ:5520533

admin@chnhonker.com
Copyright © 2001-2025 Discuz Team. Powered by Discuz! X3.5 ( 粤ICP备13060014号 )|天天打卡 本站已运行