jdobry

Untitled

Feb 20th, 2026
52
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
HTML 4.23 KB | None | 0 0
  1. <!DOCTYPE html>
  2. <html lang="en">
  3. <head>
  4.     <meta charset="UTF-8">
  5.     <title>Analog Clock</title>
  6.     <style>
  7.         body {
  8.             margin: 0;
  9.             height: 100vh;
  10.             display: flex;
  11.             justify-content: center;
  12.             align-items: center;
  13.             background-color: white;
  14.             font-family: Arial, sans-serif;
  15.         }
  16.  
  17.         .clock {
  18.             position: relative;
  19.             width: 300px;
  20.             height: 300px;
  21.             border: 5px solid #333;
  22.             border-radius: 50%;
  23.             background-color: white;
  24.             box-shadow: 0 0 20px rgba(0,0,0,0.1);
  25.         }
  26.  
  27.         .hand {
  28.             position: absolute;
  29.             bottom: 50%;
  30.             left: 50%;
  31.             transform-origin: bottom;
  32.             transform: translateX(-50%) rotate(0deg);
  33.             border-radius: 4px;
  34.             z-index: 10;
  35.         }
  36.  
  37.         .hour-hand {
  38.             width: 8px;
  39.             height: 70px;
  40.             background-color: #333;
  41.         }
  42.  
  43.         .minute-hand {
  44.             width: 6px;
  45.             height: 100px;
  46.             background-color: #666;
  47.         }
  48.  
  49.         .second-hand {
  50.             width: 2px;
  51.             height: 110px;
  52.             background-color: #d00;
  53.             z-index: 15;
  54.         }
  55.  
  56.         .center-dot {
  57.             position: absolute;
  58.             top: 50%;
  59.             left: 50%;
  60.             width: 12px;
  61.             height: 12px;
  62.             background-color: #333;
  63.             border-radius: 50%;
  64.             transform: translate(-50%, -50%);
  65.             z-index: 20;
  66.         }
  67.  
  68.         .number {
  69.             position: absolute;
  70.             width: 20px;
  71.             height: 20px;
  72.             text-align: center;
  73.             line-height: 20px;
  74.             font-size: 20px;
  75.             font-weight: bold;
  76.             color: #333;
  77.         }
  78.     </style>
  79. </head>
  80. <body>
  81.     <div class="clock">
  82.         <div class="hand hour-hand" id="hourHand"></div>
  83.         <div class="hand minute-hand" id="minuteHand"></div>
  84.         <div class="hand second-hand" id="secondHand"></div>
  85.         <div class="center-dot"></div>
  86.     </div>
  87.  
  88.     <script>
  89.         const hourHand = document.getElementById('hourHand');
  90.         const minuteHand = document.getElementById('minuteHand');
  91.         const secondHand = document.getElementById('secondHand');
  92.         const clockFace = document.querySelector('.clock');
  93.  
  94.         // Add numbers 1-12 to the clock face
  95.         for (let i = 1; i <= 12; i++) {
  96.            const number = document.createElement('div');
  97.            number.className = 'number';
  98.            number.textContent = i;
  99.            
  100.            // Position numbers in a circle
  101.            // 12 is at -90 degrees (top)
  102.            const angle = (i * 30 - 90) * (Math.PI / 180);
  103.            const radius = 115; // Distance from center
  104.            const center = 150; // Half of clock size
  105.            
  106.            const x = center + radius * Math.cos(angle) - 10; // -10 to center the 20px div
  107.            const y = center + radius * Math.sin(angle) - 10;
  108.            
  109.            number.style.left = `${x}px`;
  110.            number.style.top = `${y}px`;
  111.            
  112.            clockFace.appendChild(number);
  113.        }
  114.  
  115.        function updateClock() {
  116.            const now = new Date();
  117.            const seconds = now.getSeconds();
  118.            const minutes = now.getMinutes();
  119.            const hours = now.getHours();
  120.  
  121.            // Calculate rotation degrees
  122.            const secondDeg = seconds * 6;
  123.            // Smooth minute movement based on seconds
  124.            const minuteDeg = minutes * 6 + seconds * 0.1;
  125.            // Smooth hour movement based on minutes
  126.            const hourDeg = (hours % 12) * 30 + minutes * 0.5;
  127.  
  128.            // Apply rotation
  129.            secondHand.style.transform = `translateX(-50%) rotate(${secondDeg}deg)`;
  130.            minuteHand.style.transform = `translateX(-50%) rotate(${minuteDeg}deg)`;
  131.            hourHand.style.transform = `translateX(-50%) rotate(${hourDeg}deg)`;
  132.        }
  133.  
  134.        // Update every second
  135.        setInterval(updateClock, 1000);
  136.        // Run immediately to avoid 1-second delay on load
  137.        updateClock();
  138.    </script>
  139. </body>
  140. </html>
Advertisement
Add Comment
Please, Sign In to add comment