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

配色

音楽

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

ジャンル

ナビゲーション

ダイヤルを回して項目を選ぶ円形メニュー

デモ

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

仕様

用途
ナビゲーション
動作
DOM
種類
レシピ
動作確認
three 0.186.0・gsap 3.15.0
公開日

しくみと調整

サービスの種類を円周上に並べ、ダイヤルを回して選ぶメニューです。勢いよく回して手を離すとしばらく惰性で回り続け、もっとも近い項目の位置でぴたりと止まります。目印に近づいた項目は色が変わり、止まる直前には中央の円も同じ色に染まって、ラベルと見出しが1行ずつ入れ替わります。

手を離す直前の約0.1秒間で回転の速さを測り、その速さで0.35秒進んだ位置に一番近い項目を「止まり先」として決めています。最初に勢いよく動いてスーッと止まる power3.out の初速は「動く量 × 3 ÷ 動く時間」で計算できるため、離したときの速さを当てはめて止まるまでの時間を逆算しました。これにより、手を離した瞬間に急に速さが変わることなく、そのまま自然な減速に入れます。

もし回転の途中で最寄りの項目を逐一表示してしまうと、中央の文字がめまぐるしく切り替わって読みづらくなります。そこで、止まり先までの残りが項目間隔の35%を切ったタイミングで、一度だけ切り替えるようにしました。なお、項目をクリックしたときや左右の矢印キーで移動したときは最初から行き先が決まっているため、回り始めのタイミングで切り替えています。

CSSの transform を使って項目を回転させて円周上へ配置し、同時に文字側を同じ角度だけ逆向きに回すことで、アイコン内の文字を常に正面に向けたまま移動させています。スクリプトからCSSへ渡すのは、角度を示すCSS変数だけです。また、押した位置から5px動くまではドラッグと判定しないようにしているので、普通にクリックしようとして少し指が動いただけで回転が始まってしまうことはありません。

アクセシビリティ面では、円周の項目をタブ、中央の円をタブパネルとして組んでいます。Tabキーを押したときにフォーカスが入るのは現在選択中の項目だけで、ほかの項目へは左右の矢印キーでスムーズに移動できます。

手を離してから惰性で進む距離を長く設定すると、軽く弾くだけで何項目も回る軽やかなダイヤルになります。逆に短くすると少し進んですぐ止まり、重厚感のある手応えに変わります。項目と中央の円の色は、それぞれの項目に設定したCSS変数 --color で自由に指定できます。

コード

HTML
<section class="dial-nav" aria-labelledby="dial-nav-title">
  <div class="dial-nav__copy">
    <p class="dial-nav__label">SERVICE</p>
    <h2 class="dial-nav__title" id="dial-nav-title">お手伝いできること</h2>
    <p class="dial-nav__lead">ここに説明文が入ります。ここに説明文が入ります。ここに説明文が入ります。</p>
    <p class="dial-nav__hint">
      <svg viewBox="0 0 24 24" aria-hidden="true"><path d="M20 12a8 8 0 1 1-2.34-5.66M20 4v4h-4" /></svg>
      ダイヤルを回して選べます
    </p>
  </div>

  <!-- 円周の項目はタブ、中央の円はタブパネル。--color は選んだときに項目と中央の円を塗る色 -->
  <div class="dial">
    <svg class="dial__ring" viewBox="-288 -288 576 576" aria-hidden="true" focusable="false">
      <circle r="272" />
      <!-- 目盛りは破線の円で描く(pathLength 360 で 1 = 1°)。5° ごとの短い線と、項目の位置の長い線 -->
      <g class="dial__ticks">
        <circle class="dial__tick" r="258" pathLength="360" transform="rotate(-90)" />
        <circle class="dial__tick dial__tick--major" r="255" pathLength="360" transform="rotate(-90)" />
      </g>
      <path class="dial__marker" d="M-9 -286h18l-9 16z" />
    </svg>
    <div class="dial__tabs" role="tablist" aria-label="サービスの種類">
      <button class="dial__item" type="button" role="tab" id="dial-tab-1" aria-controls="dial-panel-1" aria-selected="true" style="--color: #aa95da">
        <svg viewBox="0 0 24 24" aria-hidden="true"><rect x="3" y="4" width="18" height="12" rx="2" /><path d="M8 20h8M12 16v4" /></svg>サイト
      </button>
      <button class="dial__item" type="button" role="tab" id="dial-tab-2" aria-controls="dial-panel-2" aria-selected="false" tabindex="-1" style="--color: #86b3d6">
        <svg viewBox="0 0 24 24" aria-hidden="true"><path d="M12 3l6 7-6 11-6-11zM12 3v6.5" /><circle cx="12" cy="11" r="1.5" /></svg>ロゴ
      </button>
      <button class="dial__item" type="button" role="tab" id="dial-tab-3" aria-controls="dial-panel-3" aria-selected="false" tabindex="-1" style="--color: #dcce6f">
        <svg viewBox="0 0 24 24" aria-hidden="true"><path d="M4 8h3l2-3h6l2 3h3a1 1 0 0 1 1 1v10a1 1 0 0 1-1 1H4a1 1 0 0 1-1-1V9a1 1 0 0 1 1-1z" /><circle cx="12" cy="13.5" r="3.5" /></svg>撮影
      </button>
      <button class="dial__item" type="button" role="tab" id="dial-tab-4" aria-controls="dial-panel-4" aria-selected="false" tabindex="-1" style="--color: #e28d87">
        <svg viewBox="0 0 24 24" aria-hidden="true"><rect x="3" y="5" width="18" height="14" rx="2" /><path d="M10 9.5v5l4.5-2.5z" /></svg>動画
      </button>
      <button class="dial__item" type="button" role="tab" id="dial-tab-5" aria-controls="dial-panel-5" aria-selected="false" tabindex="-1" style="--color: #e2bb89">
        <svg viewBox="0 0 24 24" aria-hidden="true"><path d="M6 3h8l4 4v14H6zM14 3v4h4M9 12h6M9 16h4" /></svg>印刷
      </button>
      <button class="dial__item" type="button" role="tab" id="dial-tab-6" aria-controls="dial-panel-6" aria-selected="false" tabindex="-1" style="--color: #96d192">
        <svg viewBox="0 0 24 24" aria-hidden="true"><path d="M4 20h16M5 15l4-4 4 3 6-7M15 7h4v4" /></svg>運用
      </button>
    </div>

    <!-- 1 行ずつ overflow: clip の行で包み、中の要素を上下へ送って入れ替える -->
    <div class="dial__hub">
      <div class="dial__panel" role="tabpanel" id="dial-panel-1" aria-labelledby="dial-tab-1">
        <p class="dial__line"><span class="dial__en"><span>01</span>WEBSITE</span></p>
        <h3 class="dial__line"><span>Webサイト制作</span></h3>
        <p class="dial__line dial__text"><span>ここに説明文が入ります。</span></p>
        <p class="dial__line"><a class="dial__more" href="/service/website/">詳しく見る →</a></p>
      </div>
      <div class="dial__panel" role="tabpanel" id="dial-panel-2" aria-labelledby="dial-tab-2" hidden>
        <p class="dial__line"><span class="dial__en"><span>02</span>LOGO</span></p>
        <h3 class="dial__line"><span>ロゴデザイン</span></h3>
        <p class="dial__line dial__text"><span>ここに説明文が入ります。</span></p>
        <p class="dial__line"><a class="dial__more" href="/service/logo/">詳しく見る →</a></p>
      </div>
      <div class="dial__panel" role="tabpanel" id="dial-panel-3" aria-labelledby="dial-tab-3" hidden>
        <p class="dial__line"><span class="dial__en"><span>03</span>PHOTO</span></p>
        <h3 class="dial__line"><span>写真撮影</span></h3>
        <p class="dial__line dial__text"><span>ここに説明文が入ります。</span></p>
        <p class="dial__line"><a class="dial__more" href="/service/photo/">詳しく見る →</a></p>
      </div>
      <div class="dial__panel" role="tabpanel" id="dial-panel-4" aria-labelledby="dial-tab-4" hidden>
        <p class="dial__line"><span class="dial__en"><span>04</span>MOVIE</span></p>
        <h3 class="dial__line"><span>動画制作</span></h3>
        <p class="dial__line dial__text"><span>ここに説明文が入ります。</span></p>
        <p class="dial__line"><a class="dial__more" href="/service/movie/">詳しく見る →</a></p>
      </div>
      <div class="dial__panel" role="tabpanel" id="dial-panel-5" aria-labelledby="dial-tab-5" hidden>
        <p class="dial__line"><span class="dial__en"><span>05</span>PRINT</span></p>
        <h3 class="dial__line"><span>印刷物デザイン</span></h3>
        <p class="dial__line dial__text"><span>ここに説明文が入ります。</span></p>
        <p class="dial__line"><a class="dial__more" href="/service/print/">詳しく見る →</a></p>
      </div>
      <div class="dial__panel" role="tabpanel" id="dial-panel-6" aria-labelledby="dial-tab-6" hidden>
        <p class="dial__line"><span class="dial__en"><span>06</span>SUPPORT</span></p>
        <h3 class="dial__line"><span>保守・運用</span></h3>
        <p class="dial__line dial__text"><span>ここに説明文が入ります。</span></p>
        <p class="dial__line"><a class="dial__more" href="/service/support/">詳しく見る →</a></p>
      </div>
    </div>
  </div>
</section>

CSS
.dial-nav {
  --bg: #111111;
  --surface: #262626; /* ダイヤルの外の円 */
  --ink: #eeece6;
  --label: #a8a6a0; /* 選ばれていない項目のアイコンと語 */
  --line: rgba(238, 236, 230, 0.14);
  --tick: rgba(238, 236, 230, 0.24);
  --hub-ink: #111111; /* 中央の円と、選ばれた項目の上の文字 */
  display: grid;
  grid-template-columns: minmax(0, 1fr) minmax(0, 576px);
  align-items: center;
  gap: 4rem;
  padding: 96px clamp(16px, 6vw, 96px);
  background: var(--bg);
  color: var(--ink);
}
.dial-nav__label { margin: 0; font-size: 12px; font-weight: 700; letter-spacing: 0.14em; color: var(--label); }
.dial-nav__title { margin: 20px 0 0; font-size: clamp(32px, 4vw, 44px); font-weight: 800; letter-spacing: -0.02em; line-height: 1.1; }
.dial-nav__lead { margin: 28px 0 0; max-width: 26em; font-size: 16px; line-height: 1.8; letter-spacing: 0.04em; }
.dial-nav__hint { display: flex; align-items: center; gap: 8px; margin: 40px 0 0; font-size: 13px; font-weight: 600; color: var(--label); }
.dial-nav__hint svg { width: 16px; height: 16px; fill: none; stroke: currentColor; stroke-width: 3; stroke-linecap: round; stroke-linejoin: round; }

.dial {
  --hub: #aa95da; /* 中央の円の色。script.js が選んだ項目の --color へ移す */
  position: relative;
  width: 100%;
  aspect-ratio: 1;
  border-radius: 50%;
  container-type: inline-size;
  cursor: grab;
  touch-action: none;
  -webkit-user-select: none;
  user-select: none;
}
.dial.is-dragging { cursor: grabbing; }
.dial__ring { position: absolute; inset: 0; width: 100%; height: 100%; overflow: visible; pointer-events: none; }
.dial__ring > circle { fill: var(--surface); }
.dial__tick { fill: none; stroke: var(--tick); stroke-width: 8; stroke-dasharray: 0.33 4.67; stroke-dashoffset: 0.165; }
.dial__tick--major { /* 60 は項目の間隔(360 ÷ 項目の数) */ stroke: var(--ink); stroke-width: 18; stroke-dasharray: 0.44 59.56; stroke-dashoffset: 0.22; }
.dial__marker { fill: var(--ink); }
.dial__tabs { position: absolute; inset: 0; pointer-events: none; }

/* 項目は円周を移るだけで、向きは変えない。--a(角度)と --on(上に来た度合い)、--dark(文字を暗くする度合い)は script.js が書く */
.dial__item {
  --a: 0deg; --on: 0; --dark: 0;
  position: absolute;
  inset: 50% auto auto 50%;
  display: flex;
  flex-direction: column;
  align-items: center;
  justify-content: center;
  gap: 4px;
  width: max(42px, 12.5cqi); /* 狭い画面では 42px より小さくしない */
  aspect-ratio: 1;
  margin: min(-21px, -6.25cqi) 0 0 min(-21px, -6.25cqi);
  padding: 0;
  border: 0;
  border-radius: 50%;
  background: color-mix(in srgb, var(--color) calc(var(--on) * 100%), transparent);
  box-shadow: inset 0 0 0 1px var(--line);
  color: color-mix(in srgb, var(--hub-ink) calc(var(--dark) * 100%), var(--label));
  font: inherit;
  font-size: max(9px, 2.1cqi);
  font-weight: 600;
  letter-spacing: 0.06em;
  line-height: 1;
  white-space: nowrap;
  transform: rotate(var(--a)) translateY(-35.8cqi) rotate(calc(var(--a) * -1));
  cursor: pointer;
  pointer-events: auto;
}
.dial__item svg { width: max(13px, 3.8cqi); height: max(13px, 3.8cqi); fill: none; stroke: currentColor; stroke-width: 2.2; stroke-linecap: round; stroke-linejoin: round; }
.dial__item:focus-visible { outline: 2px solid var(--ink); outline-offset: 4px; }

.dial__hub {
  position: absolute;
  inset: 23.96%;
  display: grid;
  place-items: center;
  border-radius: 50%;
  background: var(--hub);
  color: var(--hub-ink);
  text-align: center;
}
.dial__panel { grid-area: 1 / 1; display: grid; justify-items: center; gap: max(4px, 2.4cqi); width: 80%; }
.dial__panel[hidden] { display: none; }
.dial__line { margin: 0; overflow: clip; }
.dial__line:has(:focus-visible) { overflow: visible; }
.dial__line > * { display: block; }
.dial__en { display: flex; justify-content: center; gap: 10px; font-size: clamp(9px, 2.1cqi, 12px); font-weight: 700; letter-spacing: 0.14em; color: rgba(17, 17, 17, 0.75); }
.dial__en span { color: var(--hub-ink); font-variant-numeric: tabular-nums; }
h3.dial__line { font-size: clamp(14px, 4.2cqi, 24px); font-weight: 700; letter-spacing: -0.01em; line-height: 1.3; }
.dial__text { font-size: clamp(10px, 2.3cqi, 13px); line-height: 1.7; letter-spacing: 0.04em; }
.dial__more { padding-bottom: 5px; border-bottom: 1px solid rgba(17, 17, 17, 0.3); color: inherit; font-size: clamp(11px, 2.45cqi, 14px); font-weight: 600; letter-spacing: 0.04em; text-decoration: none; }
.dial__more:focus-visible { outline: 2px solid var(--hub-ink); outline-offset: 4px; }

@media (max-width: 860px) {
  .dial-nav { grid-template-columns: minmax(0, 1fr); gap: 2.5rem; padding-block: 64px; }
}

JavaScript
import { gsap } from "gsap";

const THROW = 0.35; // 惰性で進む量。離したときの速さの何秒ぶん先まで回るか
const ZONE = 0.35; // 止まる位置まで、項目の間隔のこの割合より近づいたら中央を入れ替える
const SWAP = { duration: 0.6, stagger: 0.06, ease: "power3.inOut" }; // 中央の行を送って入れ替える動き
const reduceMotion = window.matchMedia("(prefers-reduced-motion: reduce)").matches;
const time = (seconds) => (reduceMotion ? 0 : seconds);
const [clamp, wrap180] = [gsap.utils.clamp(0, 1), gsap.utils.wrap(-180, 180)];
const smooth = (k) => k * k * (3 - 2 * k);
const colorOf = (tab) => getComputedStyle(tab).getPropertyValue("--color").trim();

document.querySelectorAll(".dial").forEach((dial) => {
  const tabs = [...dial.querySelectorAll('[role="tab"]')];
  const panels = tabs.map((tab) => document.getElementById(tab.getAttribute("aria-controls")));
  const ticks = dial.querySelector(".dial__ticks");
  const step = 360 / tabs.length;
  let selected = Math.max(0, tabs.findIndex((tab) => tab.getAttribute("aria-selected") === "true"));
  let [selectedAngle, motion, drag, dragged] = [-selected * step, null, null, false]; // dragged: ドラッグのあとのクリックを無視する
  const state = { angle: selectedAngle };
  // 回転から全部を描く。項目には角度を渡し、上(0°)に近いほどその色で塗る。塗りが半分を越えたら文字を暗くする
  const render = () => {
    ticks.setAttribute("transform", `rotate(${state.angle.toFixed(2)})`);
    tabs.forEach((tab, i) => {
      const theta = i * step + state.angle;
      const on = smooth(clamp(1 - Math.abs(wrap180(theta)) / (step / 2)));
      const vars = { "--a": `${theta.toFixed(2)}deg`, "--on": on.toFixed(3), "--dark": smooth(clamp((on - 0.4) / 0.2)).toFixed(3) };
      Object.entries(vars).forEach(([name, value]) => tab.style.setProperty(name, value));
    });
  };
  // 中央を入れ替える。前の行は外へ送り、次の行は反対側から入れる。中央の円の色は文字より先に移しきる
  const select = (index, dir) => {
    const lines = (panel) => [...panel.querySelectorAll(".dial__line > *")];
    const shift = (sign) => (i, el) => sign * el.parentElement.offsetHeight;
    const [from, to] = [panels[selected], panels[index]];
    tabs.forEach((tab, i) => Object.assign(tab, { tabIndex: i === index ? 0 : -1 }).setAttribute("aria-selected", String(i === index)));
    selected = index;
    gsap.to(dial, { "--hub": colorOf(tabs[index]), duration: time(SWAP.duration), ease: "power2.out", overwrite: true });
    const push = { duration: time(SWAP.duration), stagger: time(SWAP.stagger), ease: SWAP.ease, overwrite: true };
    gsap.to(lines(from), { ...push, y: shift(-dir), onComplete: () => (from.hidden = true) });
    to.hidden = false;
    gsap.fromTo(lines(to), { y: shift(dir) }, { ...push, y: 0 });
  };
  // target(項目の位置)まで回す。now でなければ、残りが ZONE を切ったときに中央を入れ替える
  const rotateTo = (target, duration, ease, now = false) => {
    motion?.kill();
    const index = gsap.utils.wrap(0, tabs.length, Math.round(-target / step));
    const check = () => {
      if (index === selected || (!now && Math.abs(state.angle - target) >= ZONE * step)) return;
      select(index, target < selectedAngle ? 1 : -1);
      selectedAngle = target;
    };
    motion = gsap.to(state, { angle: target, duration: time(duration), ease, onUpdate: () => (render(), check()) });
    check();
  };
  // 項目を上へ。近いほうの向きから回し、遠いほど少し長くかける
  const goTo = (i) => {
    const target = -i * step + 360 * Math.round((state.angle + i * step) / 360);
    rotateTo(target, 0.4 + 0.15 * (Math.abs(target - state.angle) / step), "power3.inOut", true);
  };
  // ポインタの位置を、ダイヤルの中心からの角度(上から時計回り)にする
  const angleOf = (e, r = dial.getBoundingClientRect()) => (Math.atan2(e.clientX - r.left - r.width / 2, r.top + r.height / 2 - e.clientY) * 180) / Math.PI;
  dial.addEventListener("pointerdown", (e) => {
    [dragged, drag] = [false, { id: e.pointerId, last: angleOf(e), x: e.clientX, y: e.clientY, samples: [[e.timeStamp, state.angle]] }];
  });
  dial.addEventListener("pointermove", (e) => {
    if (e.pointerId !== drag?.id || (!dragged && Math.hypot(e.clientX - drag.x, e.clientY - drag.y) < 5)) return; // 5px までは押しただけ
    if (!dragged) motion?.kill(), dial.setPointerCapture(e.pointerId), dial.classList.add("is-dragging"); // 回っていても、つかむと止まる
    dragged = true;
    const a = angleOf(e);
    state.angle += wrap180(a - drag.last); // 指が回った角度だけ回す
    drag.last = a;
    drag.samples = [...drag.samples.filter(([t]) => e.timeStamp - t <= 100), [e.timeStamp, state.angle]];
    render();
  });
  const release = (e) => {
    if (e.pointerId !== drag?.id) return;
    const [[t0, a0], [t1, a1]] = [drag.samples[0], drag.samples.at(-1)];
    drag = null;
    dial.classList.remove("is-dragging");
    if (!dragged) return;
    // 離す前の 0.1 秒ほどの速さ(°/秒)で、その先のいちばん近い項目を止まる位置にする。止めてから離したら 0
    const v = e.timeStamp - t1 > 80 || t1 - t0 < 8 ? 0 : ((a1 - a0) / (t1 - t0)) * 1000;
    const target = Math.round((state.angle + v * THROW) / step) * step;
    const delta = target - state.angle;
    // power3.out の動き始めの速さは「動く量 × 3 ÷ 長さ」。長さをこう決めると、離した速さのまま減速に入る
    rotateTo(target, delta * v > 0 && Math.abs(v) > 30 ? gsap.utils.clamp(0.3, 1.6, (3 * delta) / v) : 0.5, "power3.out");
  };
  ["pointerup", "pointercancel"].forEach((type) => dial.addEventListener(type, release));
  tabs.forEach((tab, i) => tab.addEventListener("click", () => dragged || goTo(i)));
  // ← → で 1 項目ずつ回し、フォーカスも選んだ項目へ移す
  dial.querySelector('[role="tablist"]').addEventListener("keydown", (e) => {
    const move = { ArrowRight: 1, ArrowLeft: -1 }[e.key];
    if (!move) return;
    e.preventDefault();
    goTo(gsap.utils.wrap(0, tabs.length, selected + move)), tabs[selected].focus();
  });
  gsap.set(dial, { "--hub": colorOf(tabs[selected]) });
  render();
});

ナビゲーションをすべて見る