Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- <!DOCTYPE html>
- <html lang="en">
- <head>
- <meta charset="UTF-8">
- <meta name="viewport" content="width=device-width, initial-scale=1.0">
- <title>Analog Clock - Correct Centering</title>
- <style>
- body {
- display: flex;
- justify-content: center;
- align-items: center;
- height: 100vh;
- background-color: #ffffff;
- margin: 0;
- }
- .clock {
- width: 300px;
- height: 300px;
- border: 10px solid #333;
- border-radius: 50%;
- position: relative;
- background-color: white;
- box-shadow: 5px 5px 15px rgba(0,0,0,0.2);
- }
- .number {
- position: absolute;
- font-size: 24px;
- font-weight: bold;
- color: #333;
- text-align: center;
- /* ODEBRÁNO: width: 100%; - to způsobovalo roztahování */
- width: 30px; /* Fixed width matching font size roughly */
- height: 30px;
- line-height: 30px; /* Vertically centers text if only one line */
- /* Margin auto zajišťuje centrování v rámci dané pozice */
- margin-left: -15px; /* Posune o polovinu šířky doleva (30/2) */
- margin-top: -15px; /* Posune o polovinu výšky nahoru (30/2) */
- }
- .hand {
- position: absolute;
- bottom: 50%;
- left: 50%;
- transform-origin: bottom center;
- transform: translateX(-50%);
- border-top-left-radius: 5px;
- border-top-right-radius: 5px;
- z-index: 10;
- }
- .hour {
- width: 8px;
- height: 70px;
- background-color: #333;
- }
- .minute {
- width: 6px;
- height: 100px;
- background-color: #666;
- }
- .second {
- width: 3px;
- height: 120px;
- background-color: #d32f2f;
- z-index: 11;
- }
- .center {
- position: absolute;
- width: 15px;
- height: 15px;
- background-color: #333;
- border-radius: 50%;
- top: 50%;
- left: 50%;
- transform: translate(-50%, -50%);
- z-index: 12;
- }
- .hour-mark {
- position: absolute;
- width: 2px;
- height: 15px;
- background-color: #333;
- left: 50%;
- top: 0;
- transform-origin: center 150px;
- }
- </style>
- </head>
- <body>
- <div class="clock" id="clock">
- <div class="center"></div>
- <div class="hand hour" id="hourHand"></div>
- <div class="hand minute" id="minuteHand"></div>
- <div class="hand second" id="secondHand"></div>
- </div>
- <script>
- const clock = document.getElementById('clock');
- const hourHand = document.getElementById('hourHand');
- const minuteHand = document.getElementById('minuteHand');
- const secondHand = document.getElementById('secondHand');
- // Add clock numbers - using math to position, but NOT rotating the text
- for (let i = 1; i <= 12; i++) {
- const number = document.createElement('div');
- number.className = 'number';
- number.textContent = i;
- const angle = i * 30; // 30 degrees per hour
- // Math: x = cx + r * cos(θ), y = cy + r * sin(θ)
- // In CSS, 0° is at 3 o'clock, but clock numbers start at 12 o'clock (which is -90° or 270°).
- // cos(θ - 90°) = sin(θ), sin(θ - 90°) = -cos(θ)
- const radius = 120; // Distance from center to numbers
- const cx = 150; // Center X (half of clock width)
- const cy = 150; // Center Y (half of clock height)
- // Vzorec pro polohu čísla:
- // (angle - 90) nám dává správný úhel od 12 hodin
- const x = cx + radius * Math.cos((angle - 90) * Math.PI / 180);
- const y = cy + radius * Math.sin((angle - 90) * Math.PI / 180);
- // Přesné umístění s margin-left/margin-top pro vodorovné a svislé centrování
- number.style.left = x + 'px';
- number.style.top = y + 'px';
- clock.appendChild(number);
- }
- // Add hour marks (tradiční způsob)
- for (let i = 0; i < 12; i++) {
- const mark = document.createElement('div');
- mark.className = 'hour-mark';
- mark.style.left = '50%';
- mark.style.top = '0';
- mark.style.transform = `rotate(${i * 30}deg)`;
- mark.style.transformOrigin = 'center 150px';
- clock.appendChild(mark);
- }
- function updateTime() {
- const now = new Date();
- const seconds = now.getSeconds();
- const minutes = now.getMinutes();
- const hours = now.getHours();
- const secondDegrees = (seconds / 60) * 360;
- const minuteDegrees = ((minutes + seconds / 60) / 60) * 360;
- const hourDegrees = ((hours % 12 + minutes / 60) / 12) * 360;
- // translateX(-50%) je důležité pro centrování šířky ručičky
- secondHand.style.transform = `translateX(-50%) rotate(${secondDegrees}deg)`;
- minuteHand.style.transform = `translateX(-50%) rotate(${minuteDegrees}deg)`;
- hourHand.style.transform = `translateX(-50%) rotate(${hourDegrees}deg)`;
- }
- setInterval(updateTime, 1000);
- updateTime();
- </script>
- </body>
- </html>
Advertisement
Add Comment
Please, Sign In to add comment