ボタン 中級

ローディング→完了

クリックでラベルが消えスピナーが回転し、約1.2秒後に✓へ切替わり色も緑に変化します。フォーム送信や保存操作の結果を分かりやすく伝えます。

#js#button#loading#state

ライブデモ

コード

HTML
<!-- ローディング→完了ボタン: クリックでラベル消失→スピナー回転→約1.2秒後にチェックへ切替+緑化。再クリックで初期化 -->
<div class="stage">
  <button
    class="btn-load"
    type="button"
    data-state="idle"
    aria-live="polite"
    aria-label="注文を確定する"
  >
    <span class="btn-load__spinner" aria-hidden="true"></span>
    <svg class="btn-load__check" viewBox="0 0 24 24" aria-hidden="true">
      <path d="M4 12.5l5 5L20 6"></path>
    </svg>
    <span class="btn-load__label">注文を確定する</span>
  </button>
  <p class="hint">クリックでローディング→完了(✓)へ。もう一度押すと初期化します</p>
</div>
CSS
* { box-sizing: border-box; }
body {
  margin: 0;
  min-height: 100vh;
  display: grid;
  place-items: center;
  overflow: hidden;
  font-family: "Segoe UI", system-ui, sans-serif;
  background: radial-gradient(circle at 50% 25%, #f2f6ff 0%, #e9eefb 55%, #dfe6f6 100%);
}

.stage {
  display: grid;
  place-items: center;
  gap: 22px;
}

/* ボタン本体: グリッドで全ステートの要素を同じセルに重ね、幅/高さを固定してレイアウト崩れを防ぐ */
.btn-load {
  position: relative;
  display: grid;
  place-items: center;
  min-width: 232px;
  border: none;
  border-radius: 16px;
  padding: 20px 24px;
  font-size: 16px;
  font-weight: 700;
  letter-spacing: .02em;
  color: #fff;
  cursor: pointer;
  -webkit-tap-highlight-color: transparent;
  background: linear-gradient(135deg, #3b6bff 0%, #5b8cff 100%);
  box-shadow: 0 12px 26px rgba(59, 107, 255, .32);
  transition: transform .18s ease, box-shadow .35s ease, background .4s ease;
  /* 待機時のアイドル: 影がゆっくり呼吸してクリック可能なことを伝える */
  animation: load-idle-breathe 3s ease-in-out infinite;
}

@keyframes load-idle-breathe {
  0%, 100% { box-shadow: 0 12px 26px rgba(59, 107, 255, .32); }
  50%      { box-shadow: 0 14px 34px rgba(59, 107, 255, .5); }
}

.btn-load__label,
.btn-load__spinner,
.btn-load__check {
  grid-area: 1 / 1;
  transition: opacity .25s ease, transform .25s ease;
}

.btn-load__label {
  opacity: 1;
  transform: scale(1);
  white-space: nowrap;
}

.btn-load__spinner {
  width: 22px;
  height: 22px;
  border-radius: 50%;
  border: 3px solid rgba(255, 255, 255, .35);
  border-top-color: #fff;
  opacity: 0;
  transform: scale(.4);
}

.btn-load__check {
  width: 26px;
  height: 26px;
  opacity: 0;
  transform: scale(.4);
}

.btn-load__check path {
  fill: none;
  stroke: #fff;
  stroke-width: 3;
  stroke-linecap: round;
  stroke-linejoin: round;
  stroke-dasharray: 30;
  stroke-dashoffset: 30;
}

.btn-load:hover,
.btn-load:focus-visible {
  transform: translateY(-2px);
  box-shadow: 0 16px 34px rgba(59, 107, 255, .42);
}

.btn-load:focus-visible {
  outline: 3px solid rgba(59, 107, 255, .5);
  outline-offset: 4px;
}

.btn-load:active {
  transform: translateY(0) scale(.97);
}

/* --- loading state --- */
.btn-load[data-state="loading"] {
  cursor: progress;
  animation: none;
}
.btn-load[data-state="loading"] .btn-load__label {
  opacity: 0;
  transform: scale(.7);
}
.btn-load[data-state="loading"] .btn-load__spinner {
  opacity: 1;
  transform: scale(1);
  animation: btn-load-spin .7s linear infinite;
}

@keyframes btn-load-spin {
  to { transform: scale(1) rotate(360deg); }
}

/* --- success state --- */
.btn-load[data-state="success"] {
  animation: none;
  background: linear-gradient(135deg, #22c55e 0%, #16a34a 100%);
  box-shadow: 0 12px 26px rgba(22, 163, 74, .4);
}
.btn-load[data-state="success"]:hover,
.btn-load[data-state="success"]:focus-visible {
  box-shadow: 0 16px 34px rgba(22, 163, 74, .48);
}
.btn-load[data-state="success"] .btn-load__label {
  opacity: 0;
  transform: scale(.7);
}
.btn-load[data-state="success"] .btn-load__check {
  opacity: 1;
  transform: scale(1);
}
.btn-load[data-state="success"] .btn-load__check path {
  animation: btn-load-check-draw .45s ease forwards .05s;
}

@keyframes btn-load-check-draw {
  to { stroke-dashoffset: 0; }
}

.hint {
  margin: 0;
  font-size: 13px;
  color: #5b6a86;
  text-align: center;
}

@media (prefers-reduced-motion: reduce) {
  .btn-load { animation: none; }
  .btn-load[data-state="loading"] .btn-load__spinner { animation-duration: .01ms; }
  .btn-load[data-state="success"] .btn-load__check path { animation-duration: .01ms; }
}
JavaScript
// ローディング→完了ボタン: クリックで idle -> loading -> success と遷移し、成功状態で再クリックすると idle に戻す
(() => {
  const btn = document.querySelector('.btn-load');
  if (!btn) return; // null安全

  const LOADING_MS = 1200;
  const LABELS = {
    idle: '注文を確定する',
    loading: '読み込み中',
    success: '完了しました',
  };

  let timer = null;

  const setState = (state) => {
    btn.dataset.state = state;
    btn.setAttribute('aria-label', LABELS[state] || LABELS.idle);
    btn.disabled = state === 'loading';
  };

  btn.addEventListener('click', () => {
    const current = btn.dataset.state || 'idle';

    if (current === 'loading') return; // ローディング中はクリックを無視

    if (current === 'success') {
      setState('idle'); // 完了状態からの再クリックで初期化
      return;
    }

    setState('loading');
    if (timer) clearTimeout(timer);
    timer = setTimeout(() => {
      setState('success');
      timer = null;
    }, LOADING_MS);
  });

  setState('idle');
})();

🤖 AIエージェント用プロンプト

このままコピーしてAIに貼り付け「追加する場所」だけ書き換えればOK
あなたは熟練のフロントエンドエンジニアです。私のWebサイトに「ローディング→完了」の効果を追加してください。

# 追加してほしい効果
ローディング→完了(ボタン)
クリックでラベルが消えスピナーが回転し、約1.2秒後に✓へ切替わり色も緑に変化します。フォーム送信や保存操作の結果を分かりやすく伝えます。

# 追加する場所
👉【ここに対象箇所を記入:例「トップのヒーローセクション」「お問い合わせボタン」「記事カードの一覧」など】

# 参考実装(この見た目・挙動を再現してください)
【HTML】
<!-- ローディング→完了ボタン: クリックでラベル消失→スピナー回転→約1.2秒後にチェックへ切替+緑化。再クリックで初期化 -->
<div class="stage">
  <button
    class="btn-load"
    type="button"
    data-state="idle"
    aria-live="polite"
    aria-label="注文を確定する"
  >
    <span class="btn-load__spinner" aria-hidden="true"></span>
    <svg class="btn-load__check" viewBox="0 0 24 24" aria-hidden="true">
      <path d="M4 12.5l5 5L20 6"></path>
    </svg>
    <span class="btn-load__label">注文を確定する</span>
  </button>
  <p class="hint">クリックでローディング→完了(✓)へ。もう一度押すと初期化します</p>
</div>

【CSS】
* { box-sizing: border-box; }
body {
  margin: 0;
  min-height: 100vh;
  display: grid;
  place-items: center;
  overflow: hidden;
  font-family: "Segoe UI", system-ui, sans-serif;
  background: radial-gradient(circle at 50% 25%, #f2f6ff 0%, #e9eefb 55%, #dfe6f6 100%);
}

.stage {
  display: grid;
  place-items: center;
  gap: 22px;
}

/* ボタン本体: グリッドで全ステートの要素を同じセルに重ね、幅/高さを固定してレイアウト崩れを防ぐ */
.btn-load {
  position: relative;
  display: grid;
  place-items: center;
  min-width: 232px;
  border: none;
  border-radius: 16px;
  padding: 20px 24px;
  font-size: 16px;
  font-weight: 700;
  letter-spacing: .02em;
  color: #fff;
  cursor: pointer;
  -webkit-tap-highlight-color: transparent;
  background: linear-gradient(135deg, #3b6bff 0%, #5b8cff 100%);
  box-shadow: 0 12px 26px rgba(59, 107, 255, .32);
  transition: transform .18s ease, box-shadow .35s ease, background .4s ease;
  /* 待機時のアイドル: 影がゆっくり呼吸してクリック可能なことを伝える */
  animation: load-idle-breathe 3s ease-in-out infinite;
}

@keyframes load-idle-breathe {
  0%, 100% { box-shadow: 0 12px 26px rgba(59, 107, 255, .32); }
  50%      { box-shadow: 0 14px 34px rgba(59, 107, 255, .5); }
}

.btn-load__label,
.btn-load__spinner,
.btn-load__check {
  grid-area: 1 / 1;
  transition: opacity .25s ease, transform .25s ease;
}

.btn-load__label {
  opacity: 1;
  transform: scale(1);
  white-space: nowrap;
}

.btn-load__spinner {
  width: 22px;
  height: 22px;
  border-radius: 50%;
  border: 3px solid rgba(255, 255, 255, .35);
  border-top-color: #fff;
  opacity: 0;
  transform: scale(.4);
}

.btn-load__check {
  width: 26px;
  height: 26px;
  opacity: 0;
  transform: scale(.4);
}

.btn-load__check path {
  fill: none;
  stroke: #fff;
  stroke-width: 3;
  stroke-linecap: round;
  stroke-linejoin: round;
  stroke-dasharray: 30;
  stroke-dashoffset: 30;
}

.btn-load:hover,
.btn-load:focus-visible {
  transform: translateY(-2px);
  box-shadow: 0 16px 34px rgba(59, 107, 255, .42);
}

.btn-load:focus-visible {
  outline: 3px solid rgba(59, 107, 255, .5);
  outline-offset: 4px;
}

.btn-load:active {
  transform: translateY(0) scale(.97);
}

/* --- loading state --- */
.btn-load[data-state="loading"] {
  cursor: progress;
  animation: none;
}
.btn-load[data-state="loading"] .btn-load__label {
  opacity: 0;
  transform: scale(.7);
}
.btn-load[data-state="loading"] .btn-load__spinner {
  opacity: 1;
  transform: scale(1);
  animation: btn-load-spin .7s linear infinite;
}

@keyframes btn-load-spin {
  to { transform: scale(1) rotate(360deg); }
}

/* --- success state --- */
.btn-load[data-state="success"] {
  animation: none;
  background: linear-gradient(135deg, #22c55e 0%, #16a34a 100%);
  box-shadow: 0 12px 26px rgba(22, 163, 74, .4);
}
.btn-load[data-state="success"]:hover,
.btn-load[data-state="success"]:focus-visible {
  box-shadow: 0 16px 34px rgba(22, 163, 74, .48);
}
.btn-load[data-state="success"] .btn-load__label {
  opacity: 0;
  transform: scale(.7);
}
.btn-load[data-state="success"] .btn-load__check {
  opacity: 1;
  transform: scale(1);
}
.btn-load[data-state="success"] .btn-load__check path {
  animation: btn-load-check-draw .45s ease forwards .05s;
}

@keyframes btn-load-check-draw {
  to { stroke-dashoffset: 0; }
}

.hint {
  margin: 0;
  font-size: 13px;
  color: #5b6a86;
  text-align: center;
}

@media (prefers-reduced-motion: reduce) {
  .btn-load { animation: none; }
  .btn-load[data-state="loading"] .btn-load__spinner { animation-duration: .01ms; }
  .btn-load[data-state="success"] .btn-load__check path { animation-duration: .01ms; }
}

【JavaScript】
// ローディング→完了ボタン: クリックで idle -> loading -> success と遷移し、成功状態で再クリックすると idle に戻す
(() => {
  const btn = document.querySelector('.btn-load');
  if (!btn) return; // null安全

  const LOADING_MS = 1200;
  const LABELS = {
    idle: '注文を確定する',
    loading: '読み込み中',
    success: '完了しました',
  };

  let timer = null;

  const setState = (state) => {
    btn.dataset.state = state;
    btn.setAttribute('aria-label', LABELS[state] || LABELS.idle);
    btn.disabled = state === 'loading';
  };

  btn.addEventListener('click', () => {
    const current = btn.dataset.state || 'idle';

    if (current === 'loading') return; // ローディング中はクリックを無視

    if (current === 'success') {
      setState('idle'); // 完了状態からの再クリックで初期化
      return;
    }

    setState('loading');
    if (timer) clearTimeout(timer);
    timer = setTimeout(() => {
      setState('success');
      timer = null;
    }, LOADING_MS);
  });

  setState('idle');
})();

# 外部ライブラリ
なし(追加ライブラリ不要)

# 守ってほしいこと
- 既存のHTML構造・レイアウト・デザインを壊さないこと。必要に応じてクラス名・色・サイズを私のサイトに合わせて調整してよい。
- クラス名やidが既存と衝突しないよう、必要なら接頭辞で名前空間を分けること。
- レスポンシブ対応と prefers-reduced-motion への配慮を入れること。
- 私のサイトのフレームワーク(React / Vue / 素のHTML など)に合わせて実装すること。不明な場合は素のHTML/CSS/JSで提示し、組み込み手順も説明すること。
- 変更後の確認手順も簡潔に教えてください。