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

配色

音楽

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

ジャンル

アイコン

「+」が回転して「−」になり、FAQの答えが開くアコーディオン

デモ

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

仕様

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

しくみと調整

FAQの質問を押すと「+」がくるっと半回転し、その間に縦の線が倒れて横線に重なり「−」へと変わります。もう一度押すと、同じ向きに半回転しながら縦の線が起き上がって「+」に戻り、答えが閉じます。

記号全体は0.6秒の back.out(1.4) で回し、縦線だけはそれより短い power2.inOut で先に倒しています。重なった線が一緒に少し行き過ぎてから戻るため、横線がカチャッとはまるような気持ちよさがあります。回転の中心は svgOrigin で記号のど真ん中に設定しました。

記号全体は「押した回数 × 180度」、縦線は「押した回数 × 90度」という計算で行き先の角度を決めています。現在の角度に毎回足していく方式だと、動きの途中で押し直されたときに半端な角度で止まってしまうことがあるためです。

答えの枠の高さは0.45秒かけて広げ、中の文章は少し遅れて上からふわっと出します。開ききったところで高さを auto に戻しておくので、あとから画面幅が変わっても文章が途中で切れることはありません。質問部分は見出しの中のボタンとして実装し、開閉状態を aria-expanded で伝え、閉じている答えには hidden を付けてTabキーの移動先から外しています。

もし閉じるときだけ逆向きに回したい場合は、押した回数ではなく「いま開いているか閉じているか」の状態で角度を決めるように書き換えれば簡単に調整できます。

コード

HTML
<!-- 質問は見出しの中のボタン。開閉の状態は aria-expanded、答えとは aria-controls で結ぶ -->
<div class="faq">
  <div class="faq__item">
    <h3 class="faq__heading">
      <button class="faq__question" id="faq-q1" type="button" aria-expanded="false" aria-controls="faq-a1">
        <span>送料はいくらかかりますか?</span>
        <svg class="faq__icon" viewBox="0 0 24 24" aria-hidden="true" focusable="false">
          <!-- 横と縦の 2 本。アイコンごと回しながら、縦の線だけを 90° 倒して横に重ねる -->
          <g class="faq__mark">
            <line x1="5" y1="12" x2="19" y2="12" />
            <line class="faq__bar" x1="12" y1="5" x2="12" y2="19" />
          </g>
        </svg>
      </button>
    </h3>
    <!-- 閉じている答えは hidden にして、読み上げとタブ移動から外す -->
    <div class="faq__answer" id="faq-a1" role="region" aria-labelledby="faq-q1" hidden>
      <p>全国一律¥550です。¥5,000以上のご注文で、送料は無料になります。</p>
    </div>
  </div>
  <div class="faq__item">
    <h3 class="faq__heading">
      <button class="faq__question" id="faq-q2" type="button" aria-expanded="false" aria-controls="faq-a2">
        <span>注文のあとで、届け先を変えられますか?</span>
        <svg class="faq__icon" viewBox="0 0 24 24" aria-hidden="true" focusable="false">
          <g class="faq__mark">
            <line x1="5" y1="12" x2="19" y2="12" />
            <line class="faq__bar" x1="12" y1="5" x2="12" y2="19" />
          </g>
        </svg>
      </button>
    </h3>
    <div class="faq__answer" id="faq-a2" role="region" aria-labelledby="faq-q2" hidden>
      <p>発送の準備が始まる前なら、注文履歴の画面から変更できます。</p>
    </div>
  </div>
</div>

CSS
.faq {
  --ink: #262626;                  /* 文字とアイコンの色 */
  --line: rgba(38, 38, 38, 0.12);  /* 項目の区切り線 */
  --size: 24px;                    /* アイコンの大きさ */
  max-width: 720px;
  border-top: 1px solid var(--line);
  color: var(--ink);
}

.faq__item {
  border-bottom: 1px solid var(--line);
}

.faq__heading {
  margin: 0;
  font-size: 18px;
  font-weight: 700;
  line-height: 1.5;
  word-break: auto-phrase;
}

.faq__question {
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 24px;
  width: 100%;
  padding: 20px 0;
  border: 0;
  background: none;
  color: inherit;
  font: inherit;
  text-align: left;
  cursor: pointer;
}

.faq__question:focus-visible {
  outline: 2px solid var(--ink);
  outline-offset: 4px;
}

/* 24 グリッドで線幅 2(24px で描けば 2px)。線端は丸 */
.faq__icon {
  flex: none;
  width: var(--size);
  height: var(--size);
  overflow: visible;
  fill: none;
  stroke: currentColor;
  stroke-width: 2;
  stroke-linecap: round;
}

/* 高さは script.js が 0 から中身の高さまで伸ばす */
.faq__answer {
  height: 0;
  overflow: hidden;
}

.faq__answer p {
  margin: 0;
  padding-bottom: 24px;
  font-size: 15px;
  line-height: 1.8;
  letter-spacing: 0.04em;
  word-break: auto-phrase;
}

JavaScript
import { gsap } from "gsap";

const reduceMotion = window.matchMedia("(prefers-reduced-motion: reduce)").matches;

document.querySelectorAll(".faq__item").forEach((item) => {
  const button = item.querySelector(".faq__question");
  const mark = item.querySelector(".faq__mark");
  const bar = item.querySelector(".faq__bar");
  const answer = item.querySelector(".faq__answer");
  const text = answer.firstElementChild;

  // 押した回数。奇数なら開いている。角度はこの回数から決める
  // ("+=180" で足すと、回っている途中に押し直したとき、斜めのまま止まる)
  let turns = button.getAttribute("aria-expanded") === "true" ? 1 : 0;
  gsap.set(mark, { rotation: turns * 180, svgOrigin: "12 12" });
  gsap.set(bar, { rotation: turns * 90, svgOrigin: "12 12" });
  if (turns) {
    answer.hidden = false;
    gsap.set(answer, { height: "auto" });
  }

  let anim = null;
  button.addEventListener("click", () => {
    turns += 1;
    const open = turns % 2 === 1;
    button.setAttribute("aria-expanded", String(open));
    if (open) answer.hidden = false;
    anim?.kill();
    anim = gsap
      .timeline({
        onComplete() {
          // 開いたら高さを auto に戻す(画面の幅が変わって行数が増えても切れない)
          if (open) gsap.set(answer, { height: "auto" });
          else answer.hidden = true;
        },
      })
      // アイコンごと半回転(少し行き過ぎてから止まる)。縦の線は先に倒しきって横に重なる
      .to(mark, { rotation: turns * 180, svgOrigin: "12 12", duration: 0.6, ease: "back.out(1.4)" }, 0)
      .to(bar, { rotation: turns * 90, svgOrigin: "12 12", duration: 0.45, ease: "power2.inOut" }, 0)
      // 答えは控えめに、高さを伸ばして文を浮かせるだけ
      .to(answer, { height: open ? "auto" : 0, duration: open ? 0.45 : 0.4, ease: "power2.inOut" }, 0)
      .fromTo(text, { opacity: open ? 0 : 1, y: open ? -8 : 0 }, { opacity: open ? 1 : 0, y: 0, duration: open ? 0.35 : 0.15 }, open ? 0.12 : 0);
    // 動きを減らす設定では、ほぼ一瞬で切り替える
    if (reduceMotion) anim.progress(1);
  });
});

アイコンをすべて見る