JavaScriptのきほん「ブラウザで時計を表示しよう(2)」

スポンサーリンク
JavaScript

前回ご紹介した、『ブラウザで「アナログ時計・デジタル時計・カレンダー」を表示するHTML+Javascript』プログラムコードを改良してみよう。

以下にそのプログラムコードを以下に示す。

✅ 1. index.html(HTMLファイル)

<!DOCTYPE html>
<html lang="ja">
<head>
  <meta charset="UTF-8" />
  <title>アナログ&デジタル時計</title>
  <link rel="stylesheet" href="style.css">
</head>
<body>
  <div id="analog-clock">
    <div id="hour" class="hand"></div>
    <div id="minute" class="hand"></div>
    <div id="second" class="hand"></div>
  </div>

  <div id="digital-container">
    <div id="digital-clock" class="glow-text"></div>
    <div id="calendar" class="fade-in"></div>
  </div>

  <script src="script.js"></script>
</body>
</html>

✅ 2. style.css(CSSファイル、エフェクト付き)

body {
  font-family: 'Segoe UI', sans-serif;
  background: linear-gradient(to right, #f0f0f0, #e0e0e0);
  display: flex;
  flex-direction: column;
  align-items: center;
  justify-content: center;
  height: 100vh;
  margin: 0;
}

#analog-clock {
  position: relative;
  width: 300px;
  height: 300px;
  border: 8px solid #333;
  border-radius: 50%;
  background: white;
  box-shadow: 0 0 15px rgba(0,0,0,0.2);
}

.hand {
  position: absolute;
  bottom: 50%;
  left: 50%;
  transform-origin: bottom center;
  transform: translateX(-50%);
}

#hour {
  width: 6px;
  height: 70px;
  background: #000;
  z-index: 3;
}

#minute {
  width: 4px;
  height: 100px;
  background: #555;
  z-index: 2;
}

#second {
  width: 2px;
  height: 120px;
  background: red;
  z-index: 1;
}

.number {
  position: absolute;
  width: 30px;
  height: 30px;
  line-height: 30px;
  text-align: center;
  font-size: 16px;
  font-weight: bold;
  transform: translate(-50%, -50%);
  color: #000;
}

#digital-container {
  margin-top: 30px;
  text-align: center;
}

#digital-clock {
  font-size: 2rem;
  color: #00bcd4;
  font-weight: bold;
  text-shadow: 0 0 8px #00bcd4;
  animation: pulse 1.5s infinite alternate;
}

#calendar {
  font-size: 1.2rem;
  color: #333;
  margin-top: 8px;
  opacity: 0;
  animation: fadeIn 2s ease forwards;
}

@keyframes pulse {
  from { text-shadow: 0 0 5px #00bcd4; }
  to { text-shadow: 0 0 20px #00bcd4; }
}

@keyframes fadeIn {
  to { opacity: 1; }
}

✅ 3. script.js(JavaScriptファイル)

function pad(num) {
  return num.toString().padStart(2, '0');
}

function getJapaneseWeekday(dayIndex) {
  const weekdays = ['日', '月', '火', '水', '木', '金', '土'];
  return weekdays[dayIndex];
}

function updateClock() {
  const now = new Date();
  const hours = now.getHours();
  const minutes = now.getMinutes();
  const seconds = now.getSeconds();

  const hourDeg = ((hours % 12) + minutes / 60) * 30;
  const minuteDeg = (minutes + seconds / 60) * 6;
  const secondDeg = seconds * 6;

  document.getElementById('hour').style.transform = `translateX(-50%) rotate(${hourDeg}deg)`;
  document.getElementById('minute').style.transform = `translateX(-50%) rotate(${minuteDeg}deg)`;
  document.getElementById('second').style.transform = `translateX(-50%) rotate(${secondDeg}deg)`;

  const digital = `${pad(hours)}:${pad(minutes)}:${pad(seconds)}`;
  document.getElementById('digital-clock').textContent = digital;

  const year = now.getFullYear();
  const month = now.getMonth() + 1;
  const date = now.getDate();
  const weekday = getJapaneseWeekday(now.getDay());
  document.getElementById('calendar').textContent = `${year}年${month}月${date}日(${weekday})`;
}

function createClockNumbers() {
  const clock = document.getElementById('analog-clock');
  const centerX = clock.clientWidth / 2;
  const centerY = clock.clientHeight / 2;
  const radius = centerX - 30;

  for (let i = 1; i <= 12; i++) {
    const angle = (i * 30 - 90) * Math.PI / 180;
    const x = centerX + radius * Math.cos(angle);
    const y = centerY + radius * Math.sin(angle);

    const numDiv = document.createElement('div');
    numDiv.className = 'number';
    numDiv.style.left = `${x}px`;
    numDiv.style.top = `${y}px`;
    numDiv.textContent = i;

    clock.appendChild(numDiv);
  }
}

function startClock() {
  createClockNumbers();
  updateClock();
  setInterval(updateClock, 1000);
}

window.onload = startClock;

✅ 全体構成の修正

  • HTML、CSS、JavaScriptを分離して保守性を向上させた。
  • スタイルを style.css に分離し、見た目を向上させ、アニメーションを追加した。

✅ アナログ時計の改良点

  • (JavaScript)createClockNumbers() 関数を修正し、中心座標・半径・角度計算を正確化した。
  • (CSS) transform-originbottom center に統一し、translateX(-50%) を調整した。

✅ デジタル時計とカレンダーの改良点

  • デジタル時計に「発光アニメーション」、カレンダーに「フェードイン効果」を追加した。
  • テキストに glow(発光風)や shadow を追加して視認性と視覚効果を強化した。
  • デジタル時計は青系、背景はグラデーションで視覚的に洗練した。

実行結果

アナログ・デジタル時計&カレンダー

コメント

タイトルとURLをコピーしました