Thoughts

CSS/Javascript Word Clock

CSS Clock

A little experiment I built: a word clock using CSS3 transforms and a little Javascript to run the actual clock. Useful? Maybe not. Fun? Definitely.

The code behind it is fairly straight-forward. Let's take a peak:

CSS

The first thing we need to do is set a base set of styles for each arm of the clock.

1
li {
2
position: absolute;
3
width: 150px;
4
text-transform: lowercase;
5
-webkit-transform-origin: 0 center;
6
-moz-transform-origin: 0 center;
7
color: rgba(255, 255, 255, 0.1);
8
position: absolute;
9
left: 0;
10
top: 0;
11
}

This is basically saying for each arm, position it at the top of the clock body, and then rotate around left side, so that we have uniform rotations.

Then, for each arm, we set the actual rotation. Here is the HTML for the "hour" arms:

1
<ol id="hours">
2
<li><span>One</span></li>
3
<li><span>Two</span></li>
4
<li><span>Three</span></li>
5
<li><span>Four</span></li>
6
<li><span>Five</span></li>
7
<li><span>Six</span></li>
8
<li><span>Seven</span></li>
9
<li><span>Eight</span></li>
10
<li><span>Nine</span></li>
11
<li><span>Ten</span></li>
12
<li><span>Eleven</span></li>
13
<li><span>Twelve</span></li>
14
</ol>

And, the corresponding CSS:

1
#hours > li {
2
padding: 0 0 0 80px;
3
}
4
#hours > li:nth-child(1) {
5
-webkit-transform: rotate(-60deg);
6
-moz-transform: rotate(-60deg);
7
}
8
#hours > li:nth-child(2) {
9
-webkit-transform: rotate(-30deg);
10
-moz-transform: rotate(-30deg);
11
}
12
#hours > li:nth-child(3) {
13
-webkit-transform: rotate(0deg);
14
-moz-transform: rotate(0deg);
15
}
16
#hours > li:nth-child(4) {
17
-webkit-transform: rotate(30deg);
18
-moz-transform: rotate(30deg);
19
}
20
#hours > li:nth-child(5) {
21
-webkit-transform: rotate(60deg);
22
-moz-transform: rotate(60deg);
23
}
24
#hours > li:nth-child(6) {
25
-webkit-transform: rotate(90deg);
26
-moz-transform: rotate(90deg);
27
}
28
#hours > li:nth-child(7) {
29
-webkit-transform: rotate(120deg);
30
-moz-transform: rotate(120deg);
31
}
32
#hours > li:nth-child(8) {
33
-webkit-transform: rotate(150deg);
34
-moz-transform: rotate(150deg);
35
}
36
#hours > li:nth-child(9) {
37
-webkit-transform: rotate(180deg);
38
-moz-transform: rotate(180deg);
39
}
40
#hours > li:nth-child(10) {
41
-webkit-transform: rotate(210deg);
42
-moz-transform: rotate(210deg);
43
}
44
#hours > li:nth-child(11) {
45
-webkit-transform: rotate(240deg);
46
-moz-transform: rotate(240deg);
47
}
48
#hours > li:nth-child(12) {
49
-webkit-transform: rotate(270deg);
50
-moz-transform: rotate(270deg);
51
}

The first line sets the padding, so that each hour arm begins 80px from the center. The rest set up the individual rotations for each arm, at 30 degrees each. The minute and second hands have more padding, and rotate 6 degrees for each arm. The rest of the CSS in the file is used to style the clock, and to show/hide the extra text.

Javascript

All we need to do for the javascript is create a function that sets class="active" on each arm of the current time. So, at 12:45 and 20 seconds, the 12 hour arm, the 45 minute arm, and the 20 second arm have class="active", and the rest are normal. Then we simply set an interval to run every second.

1
$(function () {
2
var hours = $('#hours'),
3
minutes = $('#minutes'),
4
seconds = $('#seconds')
5
6
//these are default grabs so that the first time it runs, it doesn't throw an error
7
//after that, we use them to cache the current active arm for each group, so that we
8
//don't have to waste time searching
9
var cHour = $('html'),
10
cMinute = $('html'),
11
cSecond = $('html')
12
13
var setCurrentTime = function () {
14
//establish what the time is
15
var currentTime = new Date()
16
var hour = currentTime.getHours() - 1
17
if (hour == -1) {
18
hour = 11
19
}
20
var minute = currentTime.getMinutes() - 1
21
if (minute == -1) {
22
minute = 59
23
}
24
var second = currentTime.getSeconds() - 1
25
if (second == -1) {
26
second = 59
27
}
28
var ampm = 'am'
29
if (hour > 11) {
30
ampm = 'pm'
31
hour = hour - 12
32
}
33
if (hour == 11) {
34
ampm = 'pm'
35
}
36
37
//remove the active class, and add it to the new time
38
cHour.removeClass('active')
39
cHour = hours.children(':eq(' + hour + ')').addClass('active')
40
41
cMinute.removeClass('active')
42
cMinute = minutes.children(':eq(' + minute + ')').addClass('active')
43
44
cSecond.removeClass('active')
45
cSecond = seconds.children(':eq(' + second + ')').addClass('active')
46
47
$('body').removeClass('am').removeClass('pm').addClass(ampm)
48
}
49
//set the interval to run each second
50
setInterval(setCurrentTime, 1000)
51
})

It's a fun little experiment that doesn't have any real value on it's own, but it's a good exercise and as good of an excuse as any to play around with CSS transforms.

The full CSS file can be found here, and the javascript can be found here