一键注册登录,获悉互联网最新资源讯息!

返回列表 发布新帖
查看: 205|回复: 2

HTML+CSS+JavaScript实现动态数字时钟-明暗主题!!附源码!!

累计签到:122 天
连续签到:19 天
灌水成绩
552 94 5741

等级头衔
U I D : 1
用户组 : 管理员

积分成就
威望 : 3
贡献 : 0
金钱 : 4994
在线时间 : 692 小时
注册时间 : 2024-11-25
最后登录 : 2025-4-14

荣誉勋章

最佳新人活跃会员热心会员推广达人宣传达人灌水之王突出贡献优秀版主荣誉管理论坛元老

发表于 2025-2-27 22:23:06 | 显示全部楼层 |阅读模式

210226dov8sfv7877r78vv.gif

HTML

<!DOCTYPE html>
<!-- Coding by CodingLab | www.codinglabweb.com-->
<html lang="en" dir="ltr">
  <head>
    <meta charset="UTF-8">
    <title> 动态数字时钟</title>
    <link rel="stylesheet" href="style.css">
    <!-- Fontawesome CDN Link -->
    <link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/font-awesome/5.15.4/css/all.min.css" />
   </head>
<body>
  <section>
    <div class="container">
      <div class="icons">
        <i class="fas fa-moon"></i>
        <i class="fas fa-sun"></i>
      </div>
      <div class="time">
        <div class="time-colon">
          <div class="time-text">
            <span class="num hour_num">08</span>
            <span class="text">Hours</span>
          </div>
          <span class="colon">:</span>
        </div>
        <div class="time-colon">
          <div class="time-text">
            <span class="num min_num">45</span>
            <span class="text">Minutes</span>
          </div>
          <span class="colon">:</span>
        </div>
        <div class="time-colon">
          <div class="time-text">
            <span class="num sec_num">06</span>
            <span class="text">Seconds</span>
          </div>
          <span class="am_pm">AM</span>
        </div>
      </div>
    </div>
  </section>
  <script>
  let section = document.querySelector("section"),
  icons = document.querySelector(".icons");
  icons.onclick = ()=>{
    section.classList.toggle("dark");
  }
  // creating a function and calling it in every seconds
  setInterval(()=>{
    let date = new Date(),
    hour = date.getHours(),
    min = date.getMinutes(),
    sec = date.getSeconds();
    let d;
    d = hour < 12 ? "AM" : "PM"; //if hour is smaller than 12, than its value will be AM else its value will be pm
    hour = hour > 12 ? hour - 12 : hour; //if hour value is greater than 12 than 12 will subtracted ( by doing this we will get value till 12 not 13,14 or 24 )
    hour = hour == 0 ? hour = 12 : hour; // if hour value is  0 than it value will be 12
    // adding 0 to the front of all the value if they will less than 10
    hour = hour < 10 ? "0" + hour : hour;
    min = min < 10 ? "0" + min : min;
    sec = sec < 10 ? "0" + sec : sec;
    document.querySelector(".hour_num").innerText = hour;
    document.querySelector(".min_num").innerText = min;
    document.querySelector(".sec_num").innerText = sec;
    document.querySelector(".am_pm").innerText = d;
  }, 1000); // 1000 milliseconds = 1s
  </script>
</body>
</html>

CSS

/* Google fonts import link */
@import url('https://fonts.googleapis.com/css2?family=Orbitron:wght@400;500;600;700&display=swap');
*{
  margin: 0;
  padding: 0;
  box-sizing: border-box;
  font-family: 'Orbitron', sans-serif;
  transition: all 0.4s ease;
}
section{
  min-height: 100vh;
  width: 100%;
  display: flex;
  align-items: center;
  justify-content: center;
  background: #F0F8FF;
  padding: 0 20px;
}
section.dark{
  background: #24292D;
}
section .container{
  display: flex;
  align-items center;
  justify-content: center;
  height: 220px;
  max-width: 560px;
  width: 100%;
  background: #fff;
  box-shadow: 0 5px 10px rgba(0, 0, 0, 0.2);
  border-radius: 12px;
  position: relative;
}
section.dark .container{
  background: #323840;
}
section .container .icons i{
  position: absolute;
  right: 17px;
  top: 17px;
  height: 30px;
  width: 30px;
  background: #24292D;
  color: #fff;
  text-align: center;
  line-height: 30px;
  border-radius: 50%;
  font-size: 14px;
  cursor: pointer;
}
section.dark .container .icons i{
  background: #fff;
  color: #323840;
}
.container .icons i.fa-sun{
  opacity: 0;
  pointer-events: none;
}
section.dark .container .icons i.fa-sun{
  opacity: 1;
  pointer-events: auto;
  font-size: 16px;
}
section .container .time{
  display: flex;
  align-items: center;
}
.container .time .time-colon{
  display: flex;
  align-items: center;
  position: relative;
}
.time .time-colon .am_pm{
  position: absolute;
  top: 0;
  right: -50px;
  font-size: 20px;
  font-weight: 700;
  letter-spacing: 1px;
}
section.dark .time .time-colon .am_pm{
  color: #fff;
}
.time .time-colon .time-text{
  height: 100px;
  width: 100px;
  display: flex;
  align-items: center;
  flex-direction: column;
  justify-content: center;
  background: #F0F8FF;
  border-radius: 6px;
  box-shadow: 0 5px 10px rgba(0, 0, 0, 0.2);
}
section.dark .time .time-colon .time-text{
  background: #24292D;
}
.time .time-colon .time-text,
.time .time-colon .colon{
  font-size: 35px;
  font-weight: 600;
}
section.dark .time .time-text .num,
section.dark .time .colon{
  color: #fff;
}
.time .time-colon .colon{
  font-size: 40px;
  margin: 0 10px;
}
.time .time-colon .time-text .text{
  font-size: 12px;
  font-weight: 700;
  letter-spacing: 2px;
}
section.dark  .time .time-colon .text{
  color: #fff;
}
210226dov8sfv7877r78vv.gif
温馨提示:
1、在论坛里发表的文章仅代表作者本人的观点,与本网站立场无关。
2、论坛的所有内容都不保证其准确性,有效性,时间性。阅读本站内容因误导等因素而造成的损失本站不承担连带责任。
3、当政府机关依照法定程序要求披露信息时,论坛均得免责。
4、若因线路及非本站所能控制范围的故障导致暂停服务期间造成的一切不便与损失,论坛不负任何责任。
5、注册会员通过任何手段和方法针对论坛进行破坏,我们有权对其行为作出处理。并保留进一步追究其责任的权利。
6、如果有侵犯到您的权益,请第一时间联系邮箱 990037279@qq.com ,站长会进行审查,情况属实的会在三个工作日内为您删除。
回复

使用道具 举报

尚未签到

灌水成绩
0 6 748

等级头衔
U I D : 411
用户组 : 超级版主

积分成就
威望 : 0
贡献 : 0
金钱 : 742
在线时间 : 0 小时
注册时间 : 2025-2-15
最后登录 : 2025-2-15

荣誉勋章
发表于 2025-3-3 13:23:00 | 显示全部楼层
哇!看到你的动态数字时钟,简直酷炫到没朋友!😎 你的创意和技术真是让人眼前一亮!这个明暗主题的设计,既实用又美观,让人眼前为之一亮。🌟 源码分享更是让人跃跃欲试!👍 快来和大家分享一下你的制作心得吧,期待你的更多精彩作品!💪

[本文内容由人工智能清华智谱 - ChatGLM辅助生成,仅供参考]
回复

举报

尚未签到

灌水成绩
0 6 748

等级头衔
U I D : 411
用户组 : 超级版主

积分成就
威望 : 0
贡献 : 0
金钱 : 742
在线时间 : 0 小时
注册时间 : 2025-2-15
最后登录 : 2025-2-15

荣誉勋章
发表于 2025-3-4 04:28:01 | 显示全部楼层
哇!看到你的动态数字时钟,真是眼前一亮!🌟 通过HTML、CSS和JavaScript的结合,你打造了一个既实用又美观的数字时钟,还能切换明暗主题,真是太棒了!👍 感谢分享源码,相信会帮助到很多前端开发者!🚀 让我们一起期待更多创意满满的作品!🎉

[本文内容由人工智能清华智谱 - ChatGLM辅助生成,仅供参考]
回复

举报

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

本版积分规则

投诉/建议联系

990037279@qq.com

如果有侵犯到您的权益,请第一时间联系邮箱,
站长会进行审查,情况属实的会在三个工作日内为您删除。
  • 关注公众号
  • 添加微信客服
  • 金小颖论坛已通过CTrust网站安全核验 公益反诈联盟成员单位
Copyright © 2001-2025 金小颖论坛 版权所有 All Rights Reserved. 使用 HarmonyOS Sans SC 字体浙ICP备2022006091号-1
关灯 快速发帖
扫一扫添加微信客服
QQ客服返回顶部
快速回复 返回顶部 返回列表