Advertisement
Guest User

Untitled

a guest
Jul 19th, 2018
83
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
HTML 5 3.31 KB | None | 0 0
  1. <!DOCTYPE html>
  2. <html>
  3. <head>
  4.     <title></title>
  5. </head>
  6. <body>
  7.     <style>
  8.         html  {
  9.             background: linear-gradient(#8A2387, #E94057, #F27121);
  10.             font-size: 10px;
  11.         }
  12.         .container {
  13.             position: relative;
  14.             display: flex;
  15.             height: 100vh;
  16.             align-content: center;
  17.             align-items: center;
  18.             justify-content: center;
  19.         }
  20.  
  21.         .hand {
  22.             position: absolute;
  23.             right: 50%;
  24.             box-shadow: 0 1px 1px 0px rgba(0,0,0,0.1);
  25.             transform-origin: bottom;
  26.         }
  27.         #clock {
  28.             position: relative;
  29.             height: 400px;
  30.             width: 400px;
  31.             background: #FFF;
  32.             border: 15px solid #333;
  33.             border-radius: 100%;
  34.             box-shadow: inset 0 1px 2px 0px rgba(0,0,0,0.5);
  35.             margin-bottom: 10vh;
  36.         }
  37.         #hour-hand {
  38.             top: 2%;
  39.             height: 48%;
  40.             width: 1rem;
  41.             background: #333;
  42.         }
  43.         #minute-hand {
  44.             top: 4%;
  45.             height: 46%;
  46.             width: .8rem;
  47.             background: #333;
  48.         }
  49.         #second-hand {
  50.             top: 4%;
  51.             height: 46%;
  52.             width: .5rem;
  53.             background: #F92672;
  54.         }
  55.     </style>
  56.  
  57.     <div class="container">
  58.         <div id="clock">
  59.             <div id="second-hand" class="hand" style="transform: rotate(30deg)"></div>
  60.             <div id="minute-hand" class="hand" style="transform: rotate(60deg)"></div>
  61.             <div id="hour-hand" class="hand" style="transform: rotate(90deg)"></div>
  62.         </div>
  63.     </div>
  64.    
  65.     <script>
  66.         // retrieve a static date object
  67.         const transformHours = time => {
  68.             return (time.hours * 360 / 12) + (time.minutes * 360 / (12 * 60)) + (time.seconds * 360 / (12 * 60 * 60))
  69.         }
  70.         const transformMinutes = time => {
  71.             return (time.minutes * 360 / 60) + (time.seconds * 360 / (60 * 60))
  72.         }
  73.         const transformSeconds = time => {
  74.             return time.seconds * 6
  75.         }
  76.         const staticTime = new Date()
  77.         const time = {
  78.             hours: staticTime.getHours() > 12 ? staticTime.getHours() - 12 : staticTime.getHours(),
  79.             minutes: staticTime.getMinutes(),
  80.             seconds: staticTime.getSeconds(),
  81.             increment: function() {
  82.                 this.seconds++
  83.                 if(this.seconds === 60) {
  84.                     this.seconds = 0;
  85.                     this.minutes++;
  86.                 }
  87.                 if(this.minutes === 60) {
  88.                     this.minutes = 0;
  89.                     this.hours++;
  90.                 }
  91.                 if(this.hours === 13) {
  92.                     this.hours = 1;
  93.                 }          
  94.             },
  95.         }
  96.         const [seconds, minutes, hours] = document.querySelectorAll('.hand')
  97.         const clock = document.querySelectorAll('.hand')
  98.         function move(clock) {
  99.             clock.forEach(hand => {
  100.                 switch(hand.getAttribute('id')) {
  101.                     case 'second-hand':
  102.                         hand.setAttribute('style', `transform: rotate(${transformSeconds(time)}deg)`)
  103.                         break
  104.                     case 'minute-hand':        
  105.                         hand.setAttribute('style',
  106.                             `transform: rotate(${transformMinutes(time)}deg)`
  107.                         )
  108.                         break
  109.                     case 'hour-hand':
  110.                         hand.setAttribute('style', `transform: rotate(${transformHours(time)}deg)`)
  111.                         break
  112.                 }
  113.             })
  114.         }
  115.         setInterval(() => {
  116.             time.increment()
  117.             move(clock)
  118.         }, 1000)
  119.                 // transform second-hand
  120.             /**
  121.              * EVERY SECOND: seconds = 360deg/(60s/min) = 6deg
  122.         // transform minute-hand
  123.             /**
  124.              * EVERY MINUTE: minute = 360deg/(60mins/hr) = 6deg
  125.              * EVERY SECOND: minute.secondsPos = 360deg/(60*60) = .1deg
  126.         // transform hour-hand
  127.             /**
  128.              * EVERY HOUR: hour = 360deg/12hr = 30deg
  129.              * EVERY MINUTE: hour.minutePos = 360deg/(12*60)deg = .5deg
  130.              * EVERY SECOND: hour.secondsPos = 360deg/(12*60*60)deg = .00833deg
  131.              */
  132.     </script>
  133. </body>
  134. </html>
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement