完成図は以下のような感じだ。
アナログ時計をつくる
文字板を実装する
時計の顔といえる文字板を実装する。<section class="clock">
  <div class="dials">
    <div></div>
    <div></div>
    <div></div>
    <div></div>
    <div></div>
    <div></div>
    <div></div>
    <div></div>
    <div></div>
    <div></div>
    <div></div>
    <div></div>
  </div>
</section>
.clock {
  position: relative;
  width: 300px;
  height: 300px;
  border-radius: 50%;
  border: solid 2px silver;
}
/* 中心の軸となる部分 */
.clock::after {
  content: "";
  position: absolute;
  top: calc(50% - 8px);
  left: calc(50% - 8px);
  border-radius: 50%;
  width: 16px;
  height: 16px;
  background-color: silver;
}
/* 文字板(ダイアル) */
.dials {
  position: relative;
  width: 100%;
  height: 100%;
}
/* 目盛り */
.dials > div {
  position: absolute;
  top: 0;
  left: calc(50% - 3px);
  width: 6px;
  height: 50%;
  transform-origin: bottom;
}
.dials > div::after {
  position: absolute;
  top: 0;
  content: "";
  width: 6px;
  height: 15px;
  background-color: silver;
}
.dials div:nth-child(1) { transform: rotate(30deg); }
.dials div:nth-child(2) { transform: rotate(60deg); }
.dials div:nth-child(3) { transform: rotate(90deg); }
.dials div:nth-child(4) { transform: rotate(120deg); }
.dials div:nth-child(5) { transform: rotate(150deg); }
.dials div:nth-child(6) { transform: rotate(180deg); }
.dials div:nth-child(7) { transform: rotate(210deg); }
.dials div:nth-child(8) { transform: rotate(240deg); }
.dials div:nth-child(9) { transform: rotate(270deg); }
.dials div:nth-child(10) { transform: rotate(300deg); }
.dials div:nth-child(11) { transform: rotate(330deg); }
.dials div:nth-child(12) { transform: rotate(360deg); }
.clockが時計の外枠となる。サイズを決めて、border-radius: 50%で円を描く。擬似要素(
.clock::after)で時計の中心軸の円を描く。こちらも同様にborder-radius: 50%で円を描く。あとは中央揃えの定番top: calc(50% - height / 2)、left: calc(50% - width / 2)を使う。次に時計の目盛りを描く。
.dialsの子要素には12個のdiv要素があるので、このdivを30°ずつ回転させながら目盛りを打つ。(360°/ 12時間 → 30°/h)目盛りをスタイリングするときのイメージは以下のような感じだ。
transform-origin: bottomを指定して、各子要素をそれぞれtransform: rotate(30deg)のように回転させる。剣(時針、分針、秒針)を実装する
時刻をつげる剣(時針、分針、秒針)を実装する。ここでは現在の時刻にあわせてそれぞれの剣の角度を調整する。イメージ的には、先程の目盛りを打つのに似ている。<section class="clock">
  <div class="dials">
    <!-- 省略:12個のdiv -->
  </div>
  <div class="hour hand"></div>
  <div class="minute hand"></div>
  <div class="second hand"></div>
</section>
.hand {
  position: absolute;
  bottom: 50%;
  left: 50%;
  background-color: silver;
  transform-origin: 50% 100%;
}
.hour.hand {
  width: 6px;
  height: 90px;
}
.minute.hand {
  width: 4px;
  height: 140px;
}
.second.hand {
  width: 2px;
  height: 130px;
}
HTMLとCSSは上記のような感じ。中央揃えにして、transform-originでは回転させるときの中心を底辺の真ん中に指定している。あとはそれぞれの剣の幅、高さを指定しているだけ。ただこのままでは12時ちょうどを指したまま時計が動かないので、JavaScriptで現在時刻にあわせて剣を回転させる。
const hour = document.querySelector(".hour.hand");
const minute = document.querySelector(".minute.hand");
const second = document.querySelector(".second.hand");
setInterval(() => {
  const date = new Date();
  // 秒針:1周を60で割って、1秒あたりの角度を計算する
  const s = (360 / 60) * date.getSeconds();
  // 分針:1周を60で割った1分あたりの角度+1分あたりの角度を60で割った1秒あたりの角度
  const m = (360 / 60) * date.getMinutes() + (s / 60);
  // 時針:1周を24で割った1時間あたりの角度+1時間あたりの角度を60で割った1分あたりの角度
  const h = (360 / 24) * date.getHours() + (m / 24);
  
  second.style.transform = `rotate(${s}deg)`;
  minute.style.transform = `rotate(${m}deg)`;
  hour.style.transform = `rotate(${h}deg)`;
}, 1000);
rotateの引数には角度を度数法で表した値を渡せば良いので、秒針なら1周360°を60秒で割って1秒あたりの角度を算出する。分針なら360°を60分で割って1分あたりの角度を算出する。時針なら360°を24時間で割って1時間あたりの角度を算出する。このままでも良いのだが、分針・時針の動きがカクカクしすぎてしまう。
たとえば12時30分のときの時針の位置を思い出してほしい。12時ピッタリではなく、12時と1時のあいだにあるはずだ。
そのため分針なら、1分あたりの角度をさらに60秒で割った1秒あたりの角度を足す。時針なら、1時間あたりの角度をさらに60分で割った1分あたりの角度を足すことで、よりアナログ時計のようになる。
完成
<section class="clock">
  <div class="dials">
    <div></div>
    <div></div>
    <div></div>
    <div></div>
    <div></div>
    <div></div>
    <div></div>
    <div></div>
    <div></div>
    <div></div>
    <div></div>
    <div></div>
  </div>
  <div class="hour hand"></div>
  <div class="minute hand"></div>
  <div class="second hand"></div>
</section>
.clock {
  position: relative;
  width: 300px;
  height: 300px;
  border-radius: 50%;
  border: solid 2px silver;
}
.clock::after {
  content: "";
  position: absolute;
  top: calc(50% - 8px);
  left: calc(50% - 8px);
  border-radius: 50%;
  width: 16px;
  height: 16px;
  background-color: silver;
}
.dials {
  position: relative;
  width: 100%;
  height: 100%;
}
.dials > div {
  position: absolute;
  top: 0;
  left: calc(50% - 3px);
  width: 6px;
  height: 50%;
  transform-origin: bottom;
}
.dials > div::after {
  position: absolute;
  top: 0;
  content: "";
  width: 6px;
  height: 15px;
  background-color: silver;
}
.dials div:nth-child(1) { transform: rotate(30deg); }
.dials div:nth-child(2) { transform: rotate(60deg); }
.dials div:nth-child(3) { transform: rotate(90deg); }
.dials div:nth-child(4) { transform: rotate(120deg); }
.dials div:nth-child(5) { transform: rotate(150deg); }
.dials div:nth-child(6) { transform: rotate(180deg); }
.dials div:nth-child(7) { transform: rotate(210deg); }
.dials div:nth-child(8) { transform: rotate(240deg); }
.dials div:nth-child(9) { transform: rotate(270deg); }
.dials div:nth-child(10) { transform: rotate(300deg); }
.dials div:nth-child(11) { transform: rotate(330deg); }
.dials div:nth-child(12) { transform: rotate(360deg); }
.hand {
  position: absolute;
  bottom: 50%;
  left: 50%;
  background-color: silver;
  transform-origin: 50% 100%;
}
.hour.hand {
  width: 6px;
  height: 90px;
}
.minute.hand {
  width: 4px;
  height: 140px;
}
.second.hand {
  width: 2px;
  height: 130px;
}
const hour = document.querySelector(".hour.hand");
const minute = document.querySelector(".minute.hand");
const second = document.querySelector(".second.hand");
setInterval(() => {
  const date = new Date();
  const s = (360 / 60) * date.getSeconds();
  const m = (360 / 60) * date.getMinutes() + (s / 60);
  const h = (360 / 24) * date.getHours() + (m / 24);
  
  second.style.transform = `rotate(${s}deg)`;
  minute.style.transform = `rotate(${m}deg)`;
  hour.style.transform = `rotate(${h}deg)`;
}, 1000);
以上
written by @bc_rikko


参考にさせていただきました。javascriptの時針が誤動作してましたので僭越ながら修正させていただきました。
返信削除const h = Math.abs( date.getHours() - 12 ) / 12 * 360 + (m / 24);
度々すみません。こちらで動きます。
削除const h = Math.abs( date.getHours() % 12 ) / 12 * 360 + (m / 24);
それとwindow.onloadを追加するといいかもしれません。
返信削除function timer(){
const date = new Date();
const s = (360 / 60) * date.getSeconds();
const m = (360 / 60) * date.getMinutes() + (s / 60);
const h = Math.abs( date.getHours() - 12 ) / 12 * 360 + (m / 24);
second.style.transform = `rotate(${s}deg)`;
minute.style.transform = `rotate(${m}deg)`;
hour.style.transform = `rotate(${h}deg)`;
}
window.onload = function(){timer();}
setInterval(timer(), 1000);