找回密码
 立即注册
搜索
热搜: 活动 交友 discuz
查看: 30|回复: 0

[PHP] HTML+JS实现漂亮的时钟效果

[复制链接]

1883

主题

520

回帖

7366

积分

管理员

积分
7366
发表于 2024-11-13 17:54:07 | 显示全部楼层 |阅读模式
640.gif
datetime.html (8.94 KB, 下载次数: 9)


  1. <!DOCTYPE html>
  2. <html lang="en">
  3. <head>
  4. <meta charset="UTF-8">
  5. <meta name="viewport" content="width=device-width, initial-scale=1.0">
  6. <title>漂亮的时钟效果</title>
  7. <link href="https://lf3-cdn-tos.bytecdntp.com/cdn/expire-1-M/font-awesome/5.15.4/css/all.min.css" rel="stylesheet" type="text/css">

  8. <div class="digital-clock">
  9.     <i class="fas fa-ellipsis-v uil uil-ellipsis-v dot-menu-btn" style="color:#fff" id="active-menu"></i>
  10.     <ul class="dot-menu" id="active-menu">
  11.         <li class="menu-item" id="active-menu">
  12.             <span class="clock-format-text" id="active-menu">24-Hour Format</span>
  13.             <div class="format-switch-btn" id="active-menu" date-format="12"></div>
  14.         </li>
  15.     </ul>
  16.     <div class="time">
  17.         <span class="hours">00</span>
  18.         <span class="dots">:</span>
  19.         <span class="minutes">00</span>
  20.         <div class="right-side">
  21.             <span class="period">AM/PM</span>
  22.             <span class="seconds">00</span>
  23.         </div>
  24.     </div>
  25.     <div class="calender">
  26.         <span class="month-name">Month</span>,
  27.         <span class="day-name">Day</span>
  28.         <span class="day-num">00</span>
  29.     </div>
  30. </div>
  31. <style>
  32.     * {
  33.         font-family: 'Poppins', sans-serif;
  34.         margin: 0;
  35.         padding: 0;
  36.         box-sizing: border-box;
  37.     }
  38.    
  39.     body {
  40.         min-height: 100vh;
  41.         display: flex;
  42.         justify-content: center;
  43.         align-items: center;
  44.         background: #2e2e44;
  45.     }
  46.    
  47.     .digital-clock {
  48.         position: relative;
  49.         color: #fff;
  50.         background: #2e2e44;
  51.         width: 425px;
  52.         padding: 20px 45px;
  53.         box-shadow: 0 5px 25px rgba(14, 21, 37, 0.8);
  54.         border-radius: 10px;
  55.         display: flex;
  56.         justify-content: center;
  57.         align-items: center;
  58.         flex-direction: column;
  59.     }
  60.    
  61.     .digital-clock:before {
  62.         content: '';
  63.         position: absolute;
  64.         background: linear-gradient(45deg, #24ff6d, #2f93f1, #ff5e9a);
  65.         background-size: 200% 200%;
  66.         top: -5px;
  67.         left: -5px;
  68.         bottom: -5px;
  69.         right: -5px;
  70.         z-index: -1;
  71.         filter: blur(40px);
  72.         animation: glowing 10s ease infinite;
  73.     }
  74.    
  75.     @keyframes glowing {
  76.         0% {
  77.             background-position: 0 50%;
  78.         }
  79.    
  80.         50% {
  81.             background-position: 100% 50%;
  82.         }
  83.    
  84.         100% {
  85.             background-position: 0 50%;
  86.         }
  87.     }
  88.    
  89.     .time {
  90.         position: relative;
  91.         display: flex;
  92.         justify-content: center;
  93.         align-items: center;
  94.    
  95.     }
  96.    
  97.     .hours,
  98.     .dots,
  99.     .minutes {
  100.         display: flex;
  101.         justify-content: center;
  102.         align-items: center;
  103.         font-weight: 600;
  104.         padding: 0 10px;
  105.         line-height: 125px;
  106.     }
  107.    
  108.     .hours,
  109.     .minutes {
  110.         font-size: 6.5em;
  111.         width: 125px;
  112.     }
  113.    
  114.     .dots {
  115.         font-size: 5em;
  116.         color: #929292;
  117.     }
  118.    
  119.     .hours {
  120.         background: -webkit-linear-gradient(90deg, #634dff, #5fd4ff);
  121.         -webkit-text-fill-color: transparent;
  122.         -webkit-background-clip: text;
  123.     }
  124.    
  125.    
  126.     .minutes {
  127.         background: -webkit-linear-gradient(90deg, #ff5e9e, #ffb960);
  128.         -webkit-text-fill-color: transparent;
  129.         -webkit-background-clip: text;
  130.     }
  131.    
  132.     .right-side {
  133.         position: relative;
  134.         display: flex;
  135.         justify-content: center;
  136.         align-items: center;
  137.         flex-direction: column;
  138.         margin-left: 10px;
  139.     }
  140.    
  141.    
  142.     .period,
  143.     .seconds {
  144.         font-size: 1.2em;
  145.         font-weight: 500;
  146.     }
  147.    
  148.     .period {
  149.         transform: translateY(-20px);
  150.         background: -webkit-linear-gradient(90deg, #f7b63f, #faf879);
  151.         -webkit-text-fill-color: transparent;
  152.         -webkit-background-clip: text;
  153.     }
  154.    
  155.    
  156.     .seconds {
  157.         transform: translateY(16px);
  158.         background: -webkit-linear-gradient(90deg, #24ff6d, #2f93f1);
  159.         -webkit-text-fill-color: transparent;
  160.         -webkit-background-clip: text;
  161.     }
  162.    
  163.     .calender {
  164.         display: flex;
  165.         justify-content: center;
  166.         align-items: center;
  167.         font-size: 1.3em;
  168.         font-weight: 500;
  169.         margin-bottom: 5px;
  170.         background: -webkit-linear-gradient(90deg, #ae4af6, #ff98d1);
  171.         -webkit-text-fill-color: transparent;
  172.         -webkit-background-clip: text;
  173.     }
  174.    
  175.     .day-name,
  176.     .day-num,
  177.     .year {
  178.         margin-left: 8px;
  179.     }
  180.    
  181.     .dot-menu-btn {
  182.         position: absolute;
  183.         top: 0;
  184.         right: 0;
  185.         margin: 10px;
  186.         color: #efefef;
  187.         font-size: 1.5em;
  188.         cursor: pointer;
  189.     }
  190.    
  191.     .dot-menu {
  192.         z-index: 999;
  193.         position: absolute;
  194.         top: 7px;
  195.         right: 5px;
  196.         list-style: none;
  197.         background: #353e54;
  198.         padding: 10px 20px;
  199.         border-radius: 5px;
  200.         box-shadow: 0 5px 25px rgba(0, 0, 0, 0.5);
  201.         visibility: hidden;
  202.         opacity: 0;
  203.         transition: 0.4s ease;
  204.     }
  205.    
  206.     .dot-menu.active {
  207.         visibility: visible;
  208.         opacity: 1;
  209.     }
  210.    
  211.     .menu-item {
  212.         display: flex;
  213.         justify-content: center;
  214.         align-items: center;
  215.     }
  216.    
  217.    
  218.     .clock-format-text {
  219.         color: #efefef;
  220.         font-size: 0.9em;
  221.         margin-right: 20px;
  222.     }
  223.    
  224.     .format-switch-btn {
  225.         width: 35px;
  226.         height: 15px;
  227.         background: #485470;
  228.         border-radius: 75px;
  229.         box-shadow: inset 2px 2px 4px rgba(255, 255, 255, 0.1),
  230.             inset -2px -2px 4px rgba(0, 0, 0, 0.2);
  231.         cursor: pointer;
  232.         display: flex;
  233.         justify-content: center;
  234.         align-items: center;
  235.     }
  236.    
  237.     .format-switch-btn:before {
  238.         content: '';
  239.         position: absolute;
  240.         width: 14px;
  241.         height: 14px;
  242.         background: #ff5e9a;
  243.         border-radius: 50%;
  244.         box-shadow: 0 5px 25px #ff5e9a;
  245.         transform: translateX(-10px);
  246.         transition: 0.4s ease;
  247.         transition-property: background, transform;
  248.     }
  249.    
  250.     .format-switch-btn.active:before {
  251.         background: #0bff8d;
  252.         box-shadow: 0 5px 25px #0bff8d;
  253.         transform: translateX(10px);
  254.     }
  255. </style>

  256. <script>
  257.     //js for switch clock format
  258.    
  259.     const formatSwitchBtn = document.querySelector(".format-switch-btn");
  260.    
  261.     formatSwitchBtn.addEventListener("click", () => {
  262.       formatSwitchBtn.classList.toggle("active");
  263.    
  264.       var formatValue = formatSwitchBtn.getAttribute("date-format");
  265.    
  266.       if (formatValue === "12") {
  267.         formatSwitchBtn.setAttribute("date-format", "24");
  268.       }
  269.       else {
  270.         formatSwitchBtn.setAttribute("date-format", "12");
  271.       }
  272.     });
  273.    
  274.    
  275.     //Get Current Time In Javascript
  276.     function clock() {
  277.       var today = new Date();
  278.    
  279.       var hours = today.getHours();
  280.       var minutes = today.getMinutes();
  281.       var seconds = today.getSeconds();
  282.       let period = "AM";
  283.    
  284.       //set the time period
  285.       if (hours >= 12) {
  286.         period = "PM";
  287.       }
  288.    
  289.       //set 12 hour clock format
  290.       var formatValue = formatSwitchBtn.getAttribute("date-format");
  291.    
  292.       if (formatValue === "12") {
  293.         hours = hours > 12 ? hours % 12 : hours;
  294.       }
  295.    
  296.    
  297.    
  298.    
  299.       //add 0 for the value lower than 10
  300.       if (hours < 10) {
  301.         hours = "0" + hours;
  302.       }
  303.    
  304.       if (minutes < 10) {
  305.         minutes = "0" + minutes;
  306.       }
  307.    
  308.       if (seconds < 10) {
  309.         seconds = "0" + seconds;
  310.       }
  311.    
  312.    
  313.       document.querySelector(".hours").innerHTML = hours;
  314.       document.querySelector(".minutes").innerHTML = minutes;
  315.       document.querySelector(".period").innerHTML = period;
  316.       document.querySelector(".seconds").innerHTML = seconds;
  317.     }
  318.     var updateClock = setInterval(clock, 1000);
  319.    
  320.     // get the date in js
  321.    
  322.     var today = new Date();
  323.     const dayNum = today.getDate();
  324.     const year = today.getFullYear();
  325.     const dayName = today.toLocaleString("default", { weekday: "long" });
  326.     const monthName = today.toLocaleString("default", { month: "short" });
  327.    
  328.     document.querySelector(".month-name").innerHTML = monthName;
  329.     document.querySelector(".day-name").innerHTML = dayName;
  330.     document.querySelector(".day-num").innerHTML = dayNum;
  331.     // document.querySelector(".year").innerHTML = year;
  332.    
  333.     //js for dot menu toglle
  334.    
  335.     const dotmenuBtn = document.querySelector(".dot-menu-btn");
  336.     const dotMenu = document.querySelector(".dot-menu");
  337.     dotmenuBtn.addEventListener("click", () => {
  338.       dotMenu.classList.toggle("active");
  339.     });
  340.    
  341.     document.addEventListener("click", (e) => {
  342.       if (e.target.id !== "active-menu") {
  343.         dotMenu.classList.remove("active");
  344.       }
  345.     });
  346. </script>
复制代码


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

本版积分规则

快速回复 返回顶部 返回列表