フォーム
選ぶとしずくのように伸びて次の位置へ移るラジオボタン
デモ
デモを読み込めませんでした。
操作:クリック。「さわってみる」を押すと自動再生が止まり、マウスで動かせます。「さわってみる」を押すと自動再生が止まり、指で触って試せます。
仕様
- 用途
- フォーム
- 動作
- DOM
- 種類
- レシピ
- 動作確認
- three 0.186.0・gsap 3.15.0
- 公開日
しくみと調整
別の選択肢を選ぶと、丸い印の進む側が先に動き出し、後ろ側が少し遅れて追いかけます。移動している間だけしずくのように縦に伸び、次の位置に着くとまたきれいな丸に戻る動きです。移動の途中で別の選択肢を選び直したときも、その時点の位置から新しい行き先へとスムーズに向きを変えます。
この印は、選択肢の列全体に重ねた1枚のSVGに描いています。rect 要素の角の丸み(rx)を円の半径に固定したまま、縦の位置と高さだけを変化させることで、どれだけ伸びても両端の丸さをきれいに保てるようにしました。
前の端は0.5秒前後の power3.inOut で動かし、後ろの端はそれより0.08秒遅らせてスタートさせています。この遅れを0.15秒ほどに増やすとぐっと大きく伸び、0にすれば丸い形のまま移動します。また、離れた選択肢へ移るときほど移動時間を少し長めに取っています。
各選択肢は label で包んだ標準のラジオボタンなので、キーボードの矢印キーで選んでもそのまま change イベントが届きます。フォントの読み込みが終わったときや画面幅が変わったときは、印の行き先の座標を測り直してずれを防ぎます。ちなみに選択肢を横に並べる場合は、上下の代わりに左右の端を動かすだけで同じ表現ができます。
コード
<!-- 本物の radio のまとまり。クリックでも矢印キーでも選べ、フォームでそのまま送れる -->
<fieldset class="choice">
<legend class="choice__legend">お届け方法</legend>
<div class="choice__list">
<label class="choice__option">
<input class="choice__input" type="radio" name="delivery" value="standard" checked>
<span class="choice__ring" aria-hidden="true"></span>
<span class="choice__text">
<span class="choice__name">通常配送</span>
<span class="choice__note">3〜5日でお届けします</span>
</span>
<span class="choice__price">無料</span>
</label>
<label class="choice__option">
<input class="choice__input" type="radio" name="delivery" value="express">
<span class="choice__ring" aria-hidden="true"></span>
<span class="choice__text">
<span class="choice__name">お急ぎ便</span>
<span class="choice__note">明日の午前中にお届けします</span>
</span>
<span class="choice__price">¥500</span>
</label>
<label class="choice__option">
<input class="choice__input" type="radio" name="delivery" value="scheduled">
<span class="choice__ring" aria-hidden="true"></span>
<span class="choice__text">
<span class="choice__name">日時指定便</span>
<span class="choice__note">ご希望の日時にお届けします</span>
</span>
<span class="choice__price">¥300</span>
</label>
<label class="choice__option">
<input class="choice__input" type="radio" name="delivery" value="pickup">
<span class="choice__ring" aria-hidden="true"></span>
<span class="choice__text">
<span class="choice__name">店舗で受け取る</span>
<span class="choice__note">最寄りの店舗に取り置きます</span>
</span>
<span class="choice__price">無料</span>
</label>
<!-- 選択中のドットは 1 つだけ。丸の列に重ねた SVG に描き、選ぶたびに前の丸から滑らせる -->
<svg class="choice__track" aria-hidden="true" focusable="false">
<rect class="choice__dot" />
</svg>
</div>
</fieldset>.choice {
--ink: #262626; /* 文字・選んだカードの枠線とドット */
--mute: #6b6b6b; /* 補足の文字 */
--ring: #898989; /* 選んでいない丸(罫線の色だと、丸が背景に溶ける) */
--line: rgba(38, 38, 38, 0.12); /* 選んでいないカードの枠線 */
--surface: #ffffff; /* 選んだカードの背景色 */
--dot: 12px; /* ドットの直径。JS もここから読む */
min-width: 0;
max-width: 560px;
margin: 0;
padding: 0;
border: 0;
color: var(--ink);
}
.choice__legend {
margin-bottom: 16px;
padding: 0;
font-size: 20px;
font-weight: 700;
}
.choice__list {
position: relative;
display: grid;
gap: 12px;
}
.choice__option {
position: relative;
display: grid;
grid-template-columns: auto 1fr auto;
align-items: center;
gap: 16px;
padding: 16px 24px;
border-radius: 4px;
box-shadow: inset 0 0 0 1px var(--line);
cursor: pointer;
transition: background-color 0.3s, box-shadow 0.3s;
}
/* 選んだカードは、白い背景と 2px の枠線で示す */
.choice__option:has(:checked) {
background-color: var(--surface);
box-shadow: inset 0 0 0 2px var(--ink);
}
.choice__option:has(:focus-visible) {
outline: 2px solid var(--ink);
outline-offset: 3px;
}
/* 本物の radio は見えなくするだけ(display: none にすると、キーボードで選べなくなる) */
.choice__input {
position: absolute;
width: 1px;
height: 1px;
margin: 0;
opacity: 0;
pointer-events: none;
}
.choice__ring {
width: 24px;
height: 24px;
box-sizing: border-box;
border: 2px solid var(--ring);
border-radius: 50%;
transition: border-color 0.3s;
}
.choice__option:has(:checked) .choice__ring { border-color: var(--ink); }
.choice__text { display: grid; gap: 2px; }
.choice__name { font-size: 16px; font-weight: 700; letter-spacing: 0.02em; }
.choice__note { font-size: 13px; letter-spacing: 0.04em; color: var(--mute); }
.choice__price { font-size: 15px; font-weight: 700; font-variant-numeric: tabular-nums; }
/* ドットの SVG はカードより手前に置き、クリックを奪わないようにする */
.choice__track {
position: absolute;
inset: 0;
z-index: 1;
width: 100%;
height: 100%;
overflow: visible;
pointer-events: none;
}
.choice__dot { fill: var(--ink); }
@media (prefers-reduced-motion: reduce) {
.choice__option,
.choice__ring { transition: none; }
}import { gsap } from "gsap";
const reduceMotion = window.matchMedia("(prefers-reduced-motion: reduce)").matches;
const LAG = 0.08; // 後ろの端が遅れて付いてくる時間(秒)。長くすると大きく伸びる
document.querySelectorAll(".choice").forEach((group) => {
const track = group.querySelector(".choice__track");
const dot = group.querySelector(".choice__dot");
const inputs = [...group.querySelectorAll(".choice__input")];
const edge = { top: 0, bottom: 0 }; // ドットの上端と下端(SVG の中の座標)
let current = inputs.find((input) => input.checked) ?? null;
let r = 0;
// 丸の中心。丸の位置は CSS が決めるので、使うたびに測る
const centerOf = (input) => {
const ring = input.parentElement.querySelector(".choice__ring").getBoundingClientRect();
const box = track.getBoundingClientRect();
return { x: ring.left - box.left + ring.width / 2, y: ring.top - box.top + ring.height / 2 };
};
// rx を半径のままにして y と高さだけを書き換えると、どの長さでも端は丸いまま伸びる
const draw = () => {
dot.setAttribute("y", edge.top);
dot.setAttribute("height", Math.max(edge.bottom - edge.top, 0));
};
// 動かさずに、選んでいる丸へ置く(最初と、書体や画面の幅で行の高さが変わったとき)
const place = () => {
gsap.killTweensOf(edge);
dot.style.visibility = current ? "visible" : "hidden";
if (!current) return;
r = parseFloat(getComputedStyle(group).getPropertyValue("--dot")) / 2;
const c = centerOf(current);
edge.top = c.y - r;
edge.bottom = c.y + r;
dot.setAttribute("x", c.x - r);
dot.setAttribute("width", r * 2);
dot.setAttribute("rx", r);
draw();
};
// クリックでも矢印キーでも、選ばれた radio から change が届く
inputs.forEach((input) => {
input.addEventListener("change", () => {
const from = current;
current = input;
if (!from || reduceMotion) return place(); // 初めて選んだとき・動きを減らす設定では、その場に表示する
const to = centerOf(input).y;
const down = to > (edge.top + edge.bottom) / 2; // 移る途中で選び直しても、今のドットの位置から向かう
const duration = 0.4 + 0.1 * Math.abs(inputs.indexOf(input) - inputs.indexOf(from)); // 遠いほど少し長く
gsap.killTweensOf(edge);
// 進む向きの端が先に動き、後ろの端が少し遅れて付いてくる。その間だけドットが伸びる
gsap.to(edge, { [down ? "bottom" : "top"]: to + (down ? r : -r), duration, ease: "power3.inOut", onUpdate: draw });
gsap.to(edge, { [down ? "top" : "bottom"]: to + (down ? -r : r), duration, delay: LAG, ease: "power3.inOut", onUpdate: draw });
});
});
place();
document.fonts.ready.then(place);
window.addEventListener("resize", place);
});










