[JS30] -2) JS + CSS Clock


transform


transform: rotate

transform: rotate(0.5turn);
transform: rotate3d(1, 2.0, 3.0, 10deg);
transform: rotateX(10deg);
transform: rotateY(10deg);
transform: rotateZ(10deg);

transform-origin

transform-origin: x-axis y-axis;回転すると中軸に戻ります.
transform-origin: 100%;が与えられると、次のように指定された軸の周りに回転します.

JS-時計回りを一定時間移動

const $hourHand = document.querySelector(".hour-hand");
const $minHand = document.querySelector(".min-hand");
const $secondHand = document.querySelector(".second-hand");

setInterval(getSec, 1000);

function getSec(){
  const now = new Date();
  const second = now.getSeconds();
  const min = now.getMinutes();
  const hour = now.getHours();

  $secondHand.style.transform =
    `rotate(${second / 60 * 360 + 90}deg)`;

  $minHand.style.transform =
    `rotate(${min / 60 * 360 + 90}deg)`;

  $hourHand.style.transform =
    `rotate(${((hour / 12) * 360) + 90}deg);`
}

  • getSec関数はsetIntervalで毎秒呼び出されます.

  • getSec関数は1秒単位でnowインスタンスを作成し、Dateオブジェクトから現在の時間を取得します.

  • どの程度回転するかは角度を指定する必要があります.
    時間を角度に変えて伝える.
  • transition


    transition-timing-function


    cubic-bezier


    Elements>stylesで確認できますが、transion-timen-functionを次の2番目の画像と同じ形状に変換すると、時計回り틱,틱の反発効果が発生します.


    Reference


    https://developer.mozilla.org/ko/docs/Web/CSS/transform
    https://mjmjmj98.tistory.com/41 [Live passionate😎]`