Jon
Rohan

I'm a front end web developer living in San Francisco. I have been coding HTML since I was 15. I strive to be the best I can at what I do. I believe in clean and simple design as well as code. This is where I represent myself on the web. I am always looking to form relationships with other amazing people, part time or full time. contact me!

CSS Text Rotation Clock

I recently saw an article on CSS text rotation on the internets and decided to try my own experiement.

What I wanted to make was a clock. The basis of this project is using the -webkit-transform:rotate(0deg); css property. This allows you to rotate any object 360 degrees. So I began to wonder, if I have a line, and I setup a javascript timer can I rotate it every second?

The design is inspired by @eston’s Sarai desktop.

This clock won’t work in IE. There is an IE rotate property, but it only rotates at 0, 90, 180, 270. So that won’t work for this clock. Here’s the IE rotate property anyways

filter: progid:DXImageTransform.Microsoft.BasicImage(rotation=3);

So How did I do it?

I started with an external div for the clock. Giving it a width and height of 100px and position relative (so I can position the clock hands)

#clock {
  margin: 25px;
  float: left;
  width: 100px;
  height: 100px;
  position: relative;
}

After getting the clock, I need a second hand, a minute hand and an hour hand. Originally I just used a side border on the hands, but realized that the rotate property rotates the object at it’s center, and not at it’s end. So to create the hands, I gave them a border top equal to the length of the hand, then a height to offset the border, and some margin to vertically align.

The tick marks are also the lines, with a top and bottom border rotated 180 degrees.

The javascript is a simple timer which updates the degrees of each hand based on the time.

(function(){
  var tick = function() {
    var now = new Date();
    $("#sec").css({"-moz-transform":"rotate(" + (now.getSeconds() * 6) + "deg)",
    "-webkit-transform":"rotate(" + (now.getSeconds() * 6) + "deg)"});
    $("#min").css({"-moz-transform":"rotate(" + (now.getMinutes() * 6) + "deg)",
    "-webkit-transform":"rotate(" + (now.getMinutes() * 6) + "deg)"});
    $("#hour").css({"-moz-transform":"rotate(" + (now.getHours() * 30) + "deg)",
     "-webkit-transform":"rotate(" + (now.getHours() * 30) + "deg)"});
  };
  setInterval(tick,1000);
})();

If you enjoyed reading this post, check out this post Using CSS3’s :target pseudo-class to Make a Slideshow.

Tags: , , , 11 Aug 2009

About the Author, Jon Rohan

Hey! Thanks for stopping by. I love posting new topics here about various development things. If you liked it follow me on twitter and let me know.

Subscribe to Posts

3 comments

Add comment