Visitar URL original
finished day 2 · wesbos/JavaScript30@4d2f1be · GitHub
Skip to content

Commit 4d2f1be

Browse files
committed
finished day 2
1 parent 3f475bb commit 4d2f1be

1 file changed

Lines changed: 39 additions & 0 deletions

File tree

‎02 - JS + CSS Clock/index-START.html‎

Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -61,12 +61,51 @@
6161
background:black;
6262
position: absolute;
6363
top:50%;
64+
transform-origin: 100%; /* along x-axis, 0% would be left, 100% is at right.
65+
so this will make the origin point the right side,
66+
which in our case is the pivot point in the center
67+
of the clock face. otherwise each hand spins
68+
at the center of the hand, not at one end. */
69+
transform: rotate(90deg); /* rotate all the hands 90º so they start at 12 */
70+
transition: all 0.05s; /* how long the transition takes (the 1-sec "tick" )*/
71+
transition-timing-function: cubic-bezier(0.1, 2.7, 0.58, 1);
72+
/* this defines how the hand moves during transition. this one moves and
73+
bounces back a little, for a fun effect. there's like a little
74+
gui editor in chrome dev tools you can use for this?? */
6475
}
6576

6677
</style>
6778

6879
<script>
80+
const secondHand = document.querySelector('.second-hand');
81+
const minuteHand = document.querySelector('.min-hand');
82+
const hourHand = document.querySelector('.hour-hand');
6983

84+
function setDate() {
85+
const now = new Date();
86+
87+
const seconds = now.getSeconds();
88+
// calculate the seconds
89+
// first, divide seconds by 60, this gives you a percentage
90+
// e.g. 5 / 60 = 8.3%
91+
// multiply that percentage by 360 degrees to get current position
92+
// add 90 to that to offset the fact that we started at 90, not 0.
93+
const secondsDegrees = ((seconds / 60) * 360) + 90;
94+
// set the transform property so we rotate that number of degrees
95+
secondHand.style.transform = `rotate(${secondsDegrees}deg)`;
96+
97+
const minutes = now.getMinutes();
98+
const minutesDegrees = ((minutes / 60) * 360) + 90;
99+
minuteHand.style.transform = `rotate(${minutesDegrees}deg)`;
100+
101+
const hours = now.getHours();
102+
// divide by 12, not 60
103+
const hoursDegrees = ((hours / 12) * 360) + 90;
104+
hourHand.style.transform = `rotate(${hoursDegrees}deg)`;
105+
}
106+
107+
// run this once per 1000ms (1s)
108+
setInterval(setDate, 1000);
70109

71110
</script>
72111
</body>

0 commit comments

Comments
 (0)