Advertisement
Guest User

Untitled

a guest
Jan 16th, 2017
63
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.89 KB | None | 0 0
  1. CSS + JS Clock Notes:
  2.  
  3. In order to get two lines to pivot off of the end of each other's x-axis (i.e. the second/minute/hour hands of a clock),
  4. give each instance of the .hand class the attribute "transform-origin: 100%;"
  5.  
  6. Problem: div's go left-to-right, so the clock won't start at 90 degrees.
  7. Solution: give .hand an attribute of "transform: rotate(90deg);"
  8.  
  9. Question: How to mimic a mechanical clock's tick-and-stop motion?
  10. Answer: give .hand an attribute of "transition-timing-function: cubic-bezier(0.1, 2.7, 0.58, 1);
  11. The parameters for the cubic-bezier are selected by clicking the icon and pulling the bottom purple button all the way up.
  12.  
  13. Lastly: to make the "ticks" near-instantaneous, give .hand an attribute of "transition: all 0.05s;"
  14.  
  15. Lines 3-13 provide the logic for _how_ the hands move. From here on, the code regulates _when_ they move.
  16.  
  17. Within <script> tags, first select the secondHand element, then pass it into a function which determines its placement:
  18.  
  19. 1) const secondHand = document.querySelector('.second-hand');
  20.  
  21. 2) function setDate() {
  22. const now = new Date(); //creates new Date object
  23. const seconds = now.getSeconds(); //gets current second from Date object
  24. const secondsDegrees = ((seconds / 60) * 360) + 90; //calculates the angle the second hand should hold (and corrects for line 6)
  25. secondHand.style.transform = `rotate(${secondsDegrees}deg)` //rotates the second hand.
  26. }
  27.  
  28. Repeat for minutes and hours. Write both of these above setDate()
  29.  
  30. const minsHand = document.querySelector('.min-hand');
  31. const hourHand = document.querySelector('.hour-hand');
  32.  
  33. Now within setDate() function, add the following blocks:
  34.  
  35. const minutes = now.getMinutes();
  36. minuteDegrees = ((minutes/60) * 360) + 90;
  37. minsHand.style.transform = `rotate(${minuteDegrees}deg)`
  38.  
  39. const hours = now.getHour();
  40. hourDegrees = ((hours/12) * 360) + 90;
  41. hourHand.style.transform = `rotate(${hourDegrees}deg)`
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement