Hi! I'm HIGASHIYUKI® Art Director | Photo & Videographer | Web Engineer

配色

音楽

Lo-Fi #001by ST®AND.(音源のサイト。新しいタブで開きます)

ジャンル

ボタン

右端まで引くと確定し、途中で離すと弾んで戻るスライドボタン

デモ

操作:ドラッグ。「さわってみる」を押すと自動再生が止まり、マウスで動かせます。

仕様

用途
ボタン
動作
DOM
種類
レシピ
動作確認
three 0.186.0・gsap 3.15.0
公開日

しくみと調整

スライドボタンを途中で離すと、ハンドルが左端へ勢いよく戻り、端に当たって小さく2度ほど弾みます。右端まで届かなかったことが、動きそのものから直感的に伝わります。最後まで引ききったときだけボタン全体が成功の緑に塗られ、ハンドルの矢印がチェックマークに変形して、「予約を確定しました」の文字が下から浮かび上がります。

左端へ戻る動きには0.8秒の bounce.out を使いました。引いている間は、ハンドルの後ろに背景より少し明るい塗りがついてきて、案内文を覆いながら薄くしていきます。setPointerCapture でポインタを捕まえているため、指がボタンの外枠からはみ出しても追従が途切れず、touch-action: none でドラッグ中にページ全体が一緒にスクロールしてしまうのも防いでいます。確定の判定は、指を離すのを待たずに右端へ到達した瞬間に行うようにしました。

ハンドルの矢印の先端とチェックマークは、どちらも「3つの点を結んだ折れ線」で作っています。頂点の数がぴったりそろっているため、SVGの path の d 属性の数値をそのまま補間するだけで形が変わり、変形用の特別なプラグインを使わずに済みます。

引く操作だけでは、キーボードや支援技術を使う人が確定できません。そこでハンドルは本物の button 要素にしておき、Enterキーかスペースキーで押されたとき(クリックイベントの detail が0のとき)は、ハンドルを自動で右端まで送ってから確定するようにしました。逆に、マウスや指でポンと押しただけのときは確定せず、少し右へ動かして戻すことで「引いて使うボタン」だと知らせます。確定の結果は role="status" の領域に書き込み、スクリーンリーダーにもしっかり届けます。

長押しで確定するボタンと比べると、「あとどれくらいで確定するか」が目に見えて分かり、やめたいときは指を離すだけでいつでも引き返せるのが強みです。予約の確定や送金、アカウントの削除のように取り消しのきかない操作で、確定の一歩手前で思い直せる安心感を作れます。

コード

HTML
<div class="slide-confirm">
  <div class="slide-confirm__track">
    <span class="slide-confirm__hint" aria-hidden="true">
      スライドして予約を確定
      <svg viewBox="0 0 24 24" width="18" height="18"><path d="M6 6l6 6-6 6M13 6l6 6-6 6" /></svg>
    </span>
    <span class="slide-confirm__fill" aria-hidden="true"></span>
    <span class="slide-confirm__done" aria-hidden="true">予約を確定しました</span>
    <!-- ハンドルは本物のボタン。ドラッグできない人は Enter / Space(と支援技術の操作)で確定できる -->
    <button class="slide-confirm__knob" type="button" aria-label="予約を確定する" aria-describedby="slide-confirm-note">
      <svg viewBox="0 0 24 24" width="24" height="24" aria-hidden="true" focusable="false">
        <path class="slide-confirm__shaft" d="M5 12 L19 12" />
        <!-- 矢じり。確定のチェックと同じ 3 点の折れ線 -->
        <path class="slide-confirm__head" d="M13 18 L19 12 L13 6" />
      </svg>
    </button>
  </div>
  <p class="slide-confirm__note" id="slide-confirm-note" role="status">つまみを右端まで引くか、Enterキーで確定します。</p>
</div>

CSS
.slide-confirm {
  --ink: #eeece6;                   /* ハンドルと文字の色 */
  --ground: #111111;                /* トラックの背景色。矢印の色にもなる */
  --surface: #262626;               /* まわりのパネルの背景 */
  --mute: #a8a6a0;                  /* 案内の文字 */
  --line: rgba(238, 236, 230, 0.14);
  --trail: #31373c;                 /* ハンドルの後ろの塗り(背景色にブルーグレーを混ぜた色) */
  --success: #4f8a64;               /* 確定の塗りとチェック */
  --success-text: #6ba37f;          /* 確定のメッセージ(小さな文字でも読める濃さ) */
  --height: 64px;
  --pad: 8px;                       /* ハンドルとトラックのすきま */
  max-width: 400px;
  padding: 24px;
  border-radius: 4px;
  background-color: var(--surface);
  color: var(--ink);
}

.slide-confirm__track {
  position: relative;
  height: var(--height);
  border-radius: 999px;
  background-color: var(--ground);
  box-shadow: inset 0 0 0 1px var(--line);
  user-select: none;
  -webkit-user-select: none;
}

.slide-confirm__hint,
.slide-confirm__done {
  position: absolute;
  top: 0;
  bottom: 0;
  display: flex;
  align-items: center;
  justify-content: center;
  gap: 6px;
  letter-spacing: 0.04em;
  white-space: nowrap;
}

/* 案内の文字は、ハンドルの右からトラックの右端までの中央に置く */
.slide-confirm__hint {
  left: var(--height);
  right: 16px;
  color: var(--mute);
  font-size: 15px;
  font-weight: 600;
}

.slide-confirm__hint svg { fill: none; stroke: currentColor; stroke-width: 2.4; stroke-linecap: round; stroke-linejoin: round; }

/* ハンドルの後ろの塗り。幅は JS がハンドルの位置から決める */
.slide-confirm__fill { position: absolute; left: 0; top: 0; bottom: 0; width: var(--height); border-radius: inherit; background-color: var(--trail); }

/* 確定の文字は、右端のハンドルを除いた幅の中央に置く */
.slide-confirm__done { left: 0; right: var(--height); color: #ffffff; font-size: 16px; font-weight: 700; opacity: 0; }

.slide-confirm__knob {
  position: absolute;
  left: var(--pad);
  top: var(--pad);
  display: grid;
  place-items: center;
  width: calc(var(--height) - var(--pad) * 2);
  height: calc(var(--height) - var(--pad) * 2);
  padding: 0;
  border: 0;
  border-radius: 50%;
  background-color: var(--ink);
  color: var(--ground);
  cursor: grab;
  touch-action: none; /* 指でドラッグするときに、ページが一緒にスクロールしないように */
  transition: box-shadow 0.15s;
}

.slide-confirm__knob svg { overflow: visible; fill: none; stroke: currentColor; stroke-width: 2; stroke-linecap: round; stroke-linejoin: round; }
.slide-confirm__knob:focus-visible { outline: 2px solid var(--ink); outline-offset: 3px; }
.slide-confirm.is-dragging .slide-confirm__knob { cursor: grabbing; box-shadow: 0 0 0 6px rgba(238, 236, 230, 0.18); }
.slide-confirm__knob[aria-disabled="true"] { cursor: default; }

.slide-confirm__note { margin: 12px 0 0; color: var(--mute); font-size: 13px; line-height: 1.6; letter-spacing: 0.04em; }
.slide-confirm.is-done .slide-confirm__note { color: var(--success-text); font-weight: 600; }

@media (prefers-reduced-motion: reduce) {
  .slide-confirm__knob { transition: none; }
}

JavaScript
import { gsap } from "gsap";

const reduceMotion = window.matchMedia("(prefers-reduced-motion: reduce)").matches;
const SPEED = reduceMotion ? 20 : 1; // 動きを減らす設定では、ほぼ一瞬で済ませる
const CHECK = "M5.5 12.5 L10 17 L18.5 7.5"; // 矢じりと同じ 3 点の折れ線なので、d の数字をそのまま補間できる

document.querySelectorAll(".slide-confirm").forEach((root) => {
  const [track, hint, fill, done, knob, note] = ["track", "hint", "fill", "done", "knob", "note"].map((part) => root.querySelector(`.slide-confirm__${part}`));
  const [shaft, head] = knob.querySelectorAll("path");
  const success = getComputedStyle(root).getPropertyValue("--success").trim();
  let x = 0; // ハンドルの位置(左の端からの px)
  let max = 0; // ハンドルが動ける幅
  let from = 0, start = 0, moved = false; // つかんだ位置と、ドラッグしたかどうか
  let confirmed = false, back = null;

  // ハンドル・後ろの塗り・案内の文字を、ハンドルの位置から描く
  const draw = (value) => {
    x = value;
    gsap.set(knob, { x });
    fill.style.width = `${x + knob.offsetLeft * 2 + knob.offsetWidth}px`;
    hint.style.opacity = String(gsap.utils.clamp(0, 1, 1 - x / (max * 0.55)));
  };
  const measure = () => {
    max = track.clientWidth - knob.offsetWidth - knob.offsetLeft * 2;
    draw(confirmed ? max : Math.min(x, max));
  };
  // ハンドルを value まで動かす(途中で離したときの戻り・キーボードで確定するときの移動)
  const slide = (value, vars) => {
    back?.kill();
    const state = { x };
    back = gsap.to(state, { x: value, ...vars, duration: vars.duration / SPEED, onUpdate: () => draw(state.x) });
  };

  const confirm = () => {
    confirmed = true;
    back?.kill();
    draw(max);
    root.classList.add("is-done");
    knob.setAttribute("aria-disabled", "true");
    knob.setAttribute("aria-label", "予約を確定しました");
    note.textContent = "予約が確定しました。確認のメールを送りました。";
    // ボタン全体を緑に塗り、矢じりをチェックに変え、確定の文字を下から表示する
    gsap
      .timeline()
      .to(fill, { backgroundColor: success, duration: 0.3, ease: "none" }, 0)
      .to(shaft, { opacity: 0, duration: 0.12, ease: "none" }, 0)
      .to(knob, { color: success, duration: 0.25, ease: "none" }, 0)
      .to(head, { attr: { d: CHECK }, duration: 0.45, ease: "back.out(2)" }, 0.05)
      .fromTo(done, { opacity: 0, y: 10 }, { opacity: 1, y: 0, duration: 0.35, ease: "power2.out" }, 0.15)
      .timeScale(SPEED);
    root.dispatchEvent(new CustomEvent("slideconfirm", { bubbles: true })); // ここで予約の処理を呼ぶ
  };

  knob.addEventListener("pointerdown", (event) => {
    if (confirmed) return;
    knob.setPointerCapture(event.pointerId); // トラックの外へはみ出しても追える
    back?.kill();
    from = event.clientX - x;
    start = event.clientX;
    moved = false;
    root.classList.add("is-dragging");
  });
  knob.addEventListener("pointermove", (event) => {
    if (confirmed || !knob.hasPointerCapture(event.pointerId)) return;
    moved ||= Math.abs(event.clientX - start) > 4;
    draw(gsap.utils.clamp(0, max, event.clientX - from));
    if (x < max) return;
    knob.releasePointerCapture(event.pointerId); // 右端に届いたら、離すのを待たずに確定する
    root.classList.remove("is-dragging");
    confirm();
  });
  // 途中で離したら、左の端へ戻って端で弾む
  const release = () => {
    root.classList.remove("is-dragging");
    if (!confirmed && x > 0) slide(0, { duration: 0.8, ease: "bounce.out" });
  };
  knob.addEventListener("pointerup", release);
  knob.addEventListener("pointercancel", release);

  knob.addEventListener("click", (event) => {
    if (confirmed) return;
    // Enter / Space と支援技術の操作は detail が 0。右端まで動かしてから確定する
    if (event.detail === 0) {
      slide(max, { duration: 0.45, ease: "power2.in", onComplete: confirm });
    } else if (!moved && x === 0) {
      // マウスや指で押しただけでは確定しない。少し動かして戻し、ドラッグする操作だと伝える
      back?.kill();
      const state = { x: 0 };
      back = gsap.timeline({ onUpdate: () => draw(state.x) })
        .to(state, { x: 24, duration: 0.15, ease: "power2.out" })
        .to(state, { x: 0, duration: 0.6, ease: "bounce.out" })
        .timeScale(SPEED);
    }
  });

  new ResizeObserver(measure).observe(track);
  measure();
});

ボタンをすべて見る