【前端】实现时钟网页

打印 上一主题 下一主题

主题 852|帖子 852|积分 2558

【前端】实现时钟网页




  
项目先容

时钟显示在网页中央,而且使网页能够切换白天和夜晚两种模式。搭建根本的html结构,动态得到实时的时,分,秒
通过Date()函数获得。将得到的数字根据逻辑,绑定
给各div结构,实举措态旋转。点击按钮,改变配景颜色
给出最终的HTML代码。
代码

  1. <!DOCTYPE html>
  2. <html lang="zh">
  3. <head>
  4.     <meta charset="UTF-8">
  5.     <meta name="viewport" content="width=device-width, initial-scale=1.0">
  6.     <title>现代风格时钟</title>
  7.     <style>
  8.         body {
  9.             display: flex;
  10.             justify-content: center;
  11.             align-items: center;
  12.             height: 100vh;
  13.             margin: 0;
  14.             font-family: Arial, sans-serif;
  15.             background-color: #f0f0f0;
  16.             transition: background-color 0.5s;
  17.         }
  18.         .clock-container {
  19.             display: flex;
  20.             flex-direction: column;
  21.             align-items: center;
  22.         }
  23.         .clock {
  24.             width: 200px;
  25.             height: 200px;
  26.             border-radius: 50%;
  27.             background-color: white;
  28.             position: relative;
  29.             box-shadow: 0 0 20px rgba(0,0,0,0.1);
  30.         }
  31.         .hand {
  32.             position: absolute;
  33.             bottom: 50%;
  34.             left: 50%;
  35.             transform-origin: 50% 100%;
  36.             border-radius: 5px;
  37.         }
  38.         .hour {
  39.             width: 4px;
  40.             height: 50px;
  41.             background-color: #333;
  42.         }
  43.         .minute {
  44.             width: 3px;
  45.             height: 70px;
  46.             background-color: #666;
  47.         }
  48.         .second {
  49.             width: 2px;
  50.             height: 90px;
  51.             background-color: #e74c3c;
  52.         }
  53.         .center {
  54.             position: absolute;
  55.             top: 50%;
  56.             left: 50%;
  57.             width: 10px;
  58.             height: 10px;
  59.             border-radius: 50%;
  60.             background-color: #333;
  61.             transform: translate(-50%, -50%);
  62.         }
  63.         .digital-time {
  64.             font-size: 48px;
  65.             margin-top: 20px;
  66.             color: #333;
  67.         }
  68.         .mode-switch {
  69.             margin-top: 20px;
  70.             padding: 10px 20px;
  71.             font-size: 16px;
  72.             cursor: pointer;
  73.             background-color: #333;
  74.             color: white;
  75.             border: none;
  76.             border-radius: 5px;
  77.             transition: background-color 0.3s;
  78.         }
  79.         .mode-switch:hover {
  80.             background-color: #555;
  81.         }
  82.         .night-mode {
  83.             background-color: #222;
  84.         }
  85.         .night-mode .clock {
  86.             background-color: #333;
  87.             box-shadow: 0 0 20px rgba(255,255,255,0.1);
  88.         }
  89.         .night-mode .hour, .night-mode .minute {
  90.             background-color: #fff;
  91.         }
  92.         .night-mode .second {
  93.             background-color: #e74c3c;
  94.         }
  95.         .night-mode .center {
  96.             background-color: #fff;
  97.         }
  98.         .night-mode .digital-time {
  99.             color: #fff;
  100.         }
  101.         .night-mode .mode-switch {
  102.             background-color: #f0f0f0;
  103.             color: #333;
  104.         }
  105.         .night-mode .mode-switch:hover {
  106.             background-color: #ddd;
  107.         }
  108.     </style>
  109. </head>
  110. <body>
  111.     <div class="clock-container">
  112.         <div class="clock">
  113.             <div class="hand hour"></div>
  114.             <div class="hand minute"></div>
  115.             <div class="hand second"></div>
  116.             <div class="center"></div>
  117.         </div>
  118.         <div class="digital-time"></div>
  119.         <button class="mode-switch">Dark mode</button>
  120.     </div>
  121.     <script>
  122.         function updateClock() {
  123.             const now = new Date();
  124.             const hours = now.getHours();
  125.             const minutes = now.getMinutes();
  126.             const seconds = now.getSeconds();
  127.             const hourDeg = (hours % 12 + minutes / 60) * 30;
  128.             const minuteDeg = (minutes + seconds / 60) * 6;
  129.             const secondDeg = seconds * 6;
  130.             document.querySelector('.hour').style.transform = `translateX(-50%) rotate(${hourDeg}deg)`;
  131.             document.querySelector('.minute').style.transform = `translateX(-50%) rotate(${minuteDeg}deg)`;
  132.             document.querySelector('.second').style.transform = `translateX(-50%) rotate(${secondDeg}deg)`;
  133.             const digitalTime = `${hours.toString().padStart(2, '0')}:${minutes.toString().padStart(2, '0')} ${hours >= 12 ? 'PM' : 'AM'}`;
  134.             document.querySelector('.digital-time').textContent = digitalTime;
  135.         }
  136.         function toggleMode() {
  137.             document.body.classList.toggle('night-mode');
  138.             const button = document.querySelector('.mode-switch');
  139.             button.textContent = document.body.classList.contains('night-mode') ? 'Light mode' : 'Dark mode';
  140.         }
  141.         setInterval(updateClock, 1000);
  142.         updateClock(); // 立即更新一次,避免延迟
  143.         document.querySelector('.mode-switch').addEventListener('click', toggleMode);
  144.     </script>
  145. </body>
  146. </html>
复制代码
效果图




免责声明:如果侵犯了您的权益,请联系站长,我们会及时删除侵权内容,谢谢合作!更多信息从访问主页:qidao123.com:ToB企服之家,中国第一个企服评测及商务社交产业平台。

本帖子中包含更多资源

您需要 登录 才可以下载或查看,没有账号?立即注册

x
回复

使用道具 举报

0 个回复

倒序浏览

快速回复

您需要登录后才可以回帖 登录 or 立即注册

本版积分规则

拉不拉稀肚拉稀

金牌会员
这个人很懒什么都没写!
快速回复 返回顶部 返回列表