ウェブ家の備忘録

ウェブデザイナーの備忘録

JavaScript製デジタル・アナログ時計

下記を使う際はinnerHTMLの改行をなくす。


<div id="hoge"></div>

<script>
const size=300;
document.getElementById("hoge").innerHTML='
<center>
<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>
</center>
<style>
.clock {
  position: relative;
  width: '+size+'px;
  height: '+size+'px;
  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: '+(size/300*6)+'px;
  height: 50%;
  transform-origin: bottom;
}
.dials > div::after {
  position: absolute;
  top: 0;
  content: "";
  width: '+(size/300*6)+'px;
  height: '+(size/300*15)+'px;
  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: '+(size/300*6)+'px;
  height: '+(size/300*90)+'px;
}

.minute.hand {
  width: '+(size/300*4)+'px;
  height: '+(size/300*140)+'px;
}

.second.hand {
  width: '+(size/300*2)+'px;
  height: '+(size/300*130)+'px;
}
</style>
<br>
<center>
  <div id="viewClock">00:00:00</div>
</center>
';

//アナログ時計
const hour = document.querySelector(".hour.hand");
const minute = document.querySelector(".minute.hand");
const second = document.querySelector(".second.hand");

function timer(){
const date = new Date();
const s = (360 / 60) * date.getSeconds();
const m = (360 / 60) * date.getMinutes() + (s / 60);
const h = (360 / 12) * (date.getHours() % 12) + (m / 12);

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);

//デジタル時計
function clock(DOM) {
  const timeText = ("0"+new Date().getHours()  ).slice(-2) + ":"
                 + ("0"+new Date().getMinutes()).slice(-2) + ":"
                 + ("0"+new Date().getSeconds()).slice(-2);
  if(DOM){document.getElementById("viewClock").innerHTML = timeText;}
  else{return timeText;}
}
window.onload = function(){clock("DOM"); console.log(clock());}
setInterval('clock("DOM")',1000);
</script>