jdobry

Untitled

Feb 22nd, 2026
61
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
HTML 5.64 KB | None | 0 0
  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>Analog Clock - Correct Centering</title>
  7.     <style>
  8.         body {
  9.             display: flex;
  10.             justify-content: center;
  11.             align-items: center;
  12.             height: 100vh;
  13.             background-color: #ffffff;
  14.             margin: 0;
  15.         }
  16.         .clock {
  17.             width: 300px;
  18.             height: 300px;
  19.             border: 10px solid #333;
  20.             border-radius: 50%;
  21.             position: relative;
  22.             background-color: white;
  23.             box-shadow: 5px 5px 15px rgba(0,0,0,0.2);
  24.         }
  25.         .number {
  26.             position: absolute;
  27.             font-size: 24px;
  28.             font-weight: bold;
  29.             color: #333;
  30.             text-align: center;
  31.             /* ODEBRÁNO: width: 100%; - to způsobovalo roztahování */
  32.             width: 30px; /* Fixed width matching font size roughly */
  33.             height: 30px;
  34.             line-height: 30px; /* Vertically centers text if only one line */
  35.             /* Margin auto zajišťuje centrování v rámci dané pozice */
  36.             margin-left: -15px; /* Posune o polovinu šířky doleva (30/2) */
  37.             margin-top: -15px;  /* Posune o polovinu výšky nahoru (30/2) */
  38.         }
  39.         .hand {
  40.             position: absolute;
  41.             bottom: 50%;
  42.             left: 50%;
  43.             transform-origin: bottom center;
  44.             transform: translateX(-50%);
  45.             border-top-left-radius: 5px;
  46.             border-top-right-radius: 5px;
  47.             z-index: 10;
  48.         }
  49.         .hour {
  50.             width: 8px;
  51.             height: 70px;
  52.             background-color: #333;
  53.         }
  54.         .minute {
  55.             width: 6px;
  56.             height: 100px;
  57.             background-color: #666;
  58.         }
  59.         .second {
  60.             width: 3px;
  61.             height: 120px;
  62.             background-color: #d32f2f;
  63.             z-index: 11;
  64.         }
  65.         .center {
  66.             position: absolute;
  67.             width: 15px;
  68.             height: 15px;
  69.             background-color: #333;
  70.             border-radius: 50%;
  71.             top: 50%;
  72.             left: 50%;
  73.             transform: translate(-50%, -50%);
  74.             z-index: 12;
  75.         }
  76.         .hour-mark {
  77.             position: absolute;
  78.             width: 2px;
  79.             height: 15px;
  80.             background-color: #333;
  81.             left: 50%;
  82.             top: 0;
  83.             transform-origin: center 150px;
  84.         }
  85.     </style>
  86. </head>
  87. <body>
  88.     <div class="clock" id="clock">
  89.         <div class="center"></div>
  90.         <div class="hand hour" id="hourHand"></div>
  91.         <div class="hand minute" id="minuteHand"></div>
  92.         <div class="hand second" id="secondHand"></div>
  93.     </div>
  94.  
  95.     <script>
  96.         const clock = document.getElementById('clock');
  97.         const hourHand = document.getElementById('hourHand');
  98.         const minuteHand = document.getElementById('minuteHand');
  99.         const secondHand = document.getElementById('secondHand');
  100.  
  101.         // Add clock numbers - using math to position, but NOT rotating the text
  102.         for (let i = 1; i <= 12; i++) {
  103.            const number = document.createElement('div');
  104.            number.className = 'number';
  105.            number.textContent = i;
  106.            const angle = i * 30; // 30 degrees per hour
  107.            
  108.            // Math: x = cx + r * cos(θ), y = cy + r * sin(θ)
  109.            // In CSS, 0° is at 3 o'clock, but clock numbers start at 12 o'clock (which is -90° or 270°).
  110.            // cos(θ - 90°) = sin(θ), sin(θ - 90°) = -cos(θ)
  111.            const radius = 120; // Distance from center to numbers
  112.            const cx = 150; // Center X (half of clock width)
  113.            const cy = 150; // Center Y (half of clock height)
  114.  
  115.            // Vzorec pro polohu čísla:
  116.            // (angle - 90) nám dává správný úhel od 12 hodin
  117.            const x = cx + radius * Math.cos((angle - 90) * Math.PI / 180);
  118.            const y = cy + radius * Math.sin((angle - 90) * Math.PI / 180);
  119.  
  120.            // Přesné umístění s margin-left/margin-top pro vodorovné a svislé centrování
  121.            number.style.left = x + 'px';
  122.            number.style.top = y + 'px';
  123.  
  124.            clock.appendChild(number);
  125.        }
  126.  
  127.        // Add hour marks (tradiční způsob)
  128.        for (let i = 0; i < 12; i++) {
  129.            const mark = document.createElement('div');
  130.            mark.className = 'hour-mark';
  131.            mark.style.left = '50%';
  132.            mark.style.top = '0';
  133.            mark.style.transform = `rotate(${i * 30}deg)`;
  134.            mark.style.transformOrigin = 'center 150px';
  135.            clock.appendChild(mark);
  136.        }
  137.  
  138.        function updateTime() {
  139.            const now = new Date();
  140.            const seconds = now.getSeconds();
  141.            const minutes = now.getMinutes();
  142.            const hours = now.getHours();
  143.  
  144.            const secondDegrees = (seconds / 60) * 360;
  145.            const minuteDegrees = ((minutes + seconds / 60) / 60) * 360;
  146.            const hourDegrees = ((hours % 12 + minutes / 60) / 12) * 360;
  147.  
  148.            // translateX(-50%) je důležité pro centrování šířky ručičky
  149.            secondHand.style.transform = `translateX(-50%) rotate(${secondDegrees}deg)`;
  150.            minuteHand.style.transform = `translateX(-50%) rotate(${minuteDegrees}deg)`;
  151.            hourHand.style.transform = `translateX(-50%) rotate(${hourDegrees}deg)`;
  152.        }
  153.  
  154.        setInterval(updateTime, 1000);
  155.        updateTime();
  156.    </script>
  157. </body>
  158. </html>
Advertisement
Add Comment
Please, Sign In to add comment