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

配色

音楽

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

ジャンル

オーバーレイ

Cookieの同意を選ぶと上から畳まれるバナー

デモ

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

仕様

用途
オーバーレイ
動作
DOM
種類
レシピ
動作確認
three 0.186.0・gsap 3.15.0
公開日

しくみと調整

ページを開くと、画面の下からCookieの同意バナーがせり上がり、アイコン、案内文、ボタンが順番に現れます。最初に大きく動いてすっと止まる expo.out を0.9秒かけて使っているため、ユーザーが案内文を読み始めるちょうどよいタイミングで動きが落ち着きます。

どれかのボタンを選ぶと、まず中の文字やボタンが先に消え、バナー自体が少し下へ下がりながら上の縁から畳まれていきます。閉じるときは最初と最後が滑らかな power3.inOut を使い、選択を終えた区切りが心地よく感じられるようにしています。表示領域を切り抜く clip-path の上端だけを下げる方法をとっているため、畳まれる途中でもバナー全体の幅や角の丸みは崩れません。

ただ、Chromeなどのブラウザでは inset(0% 0% 0% 0%) のような指定を内部で短い形式に省略して返すことがあります。その文字列をGSAPでそのまま補間しようとすると、数値の並びがそろわず角の丸みが崩れる原因になります。そこで、畳む量だけを1つのCSS変数にして切り抜き指定に組み込み、GSAPからはその数値だけを更新するようにして安定した動きを保っています。

ユーザーが選んだ結果はイベントとしてページ側に通知されるため、それを受け取る処理を用意しておけば、「すべて許可」を選んだときだけアクセス解析を読み込む、といった制御が簡単に行えます。また、フッターなどの「Cookieの設定」リンクから開き直したときはバナーへフォーカスを移し、選び終えたら元のリンクへフォーカスを戻すようにして、キーボード操作でも迷わないように配慮しています。

選択した結果はブラウザの localStorage に保存されるため、次回訪れたときにはバナーを表示しません。保存するキー名にはバージョンの数字を含めてあるので、プライバシー方針が変わったときなどはその数字を1つ上げるだけで、再びユーザーに選び直してもらうことができます。

コード

HTML
<!-- 同意バナー。hidden のまま置き、まだ選んでいない人にだけスクリプトが表示する。
     body の最初に置くと、キーボードやスクリーンリーダーで最初に届く -->
<section class="consent" aria-labelledby="consent-title" hidden>
  <svg class="consent__icon" viewBox="0 0 24 24" aria-hidden="true">
    <path d="M12 3a9 9 0 1 0 9 9 3 3 0 0 1-3-3 3 3 0 0 1-3-3 3 3 0 0 1-3-3z" />
    <circle cx="8" cy="10.5" r="0.9" /><circle cx="10.5" cy="16" r="0.9" /><circle cx="15.5" cy="14" r="0.9" />
  </svg>
  <div class="consent__text">
    <h2 class="consent__title" id="consent-title">Cookieの使用について</h2>
    <p class="consent__desc">
      当サイトでは、利便性の向上とアクセス解析のためにCookieを使用しています。詳しくは<a href="/cookie-policy/">Cookieポリシー</a>をご覧ください。
    </p>
  </div>
  <div class="consent__actions">
    <button class="consent__button" type="button" data-consent="necessary">必須のみ許可</button>
    <button class="consent__button consent__button--fill" type="button" data-consent="all">すべて許可</button>
  </div>
</section>

<!-- 選び直すためのボタン。フッターなどに置く(押すと、バナーがもう一度せり上がる) -->
<button class="consent-open" type="button" data-consent-open>Cookieの設定</button>

CSS
.consent,
.consent-open {
  --ink: #eeece6; /* 文字と線 */
  --mute: #a8a6a0; /* 説明の文字 */
  --surface: #262626; /* バナーの背景 */
  --line: rgba(238, 236, 230, 0.14); /* 縁の罫線 */
  --ground: #111111; /* プライマリボタンの文字(背景色と同じ暗い色) */
  --radius: 4px;
}

.consent {
  --fold: 0; /* 畳んだ量(0〜100)。スクリプトが動かす */
  position: fixed;
  right: 24px;
  bottom: 24px;
  left: 24px;
  z-index: 100;
  display: flex;
  align-items: center;
  gap: 24px;
  max-width: 1088px;
  margin: 0 auto;
  padding: 24px 32px;
  border-radius: var(--radius);
  background-color: var(--surface);
  box-shadow: inset 0 0 0 1px var(--line); /* 影は付けず、1px の罫線で縁を取る */
  color: var(--ink);
  /* 上の縁から下の縁へ畳む。数を 1 つの変数にしておくと、GSAP がそのまま補間できる */
  clip-path: inset(calc(var(--fold) * 1%) 0 0 0 round var(--radius));
}

.consent[hidden] { display: none; }

.consent__icon {
  flex: none;
  width: 32px;
  height: 32px;
  fill: none;
  stroke: currentColor;
  stroke-width: 1.5;
  stroke-linecap: round;
  stroke-linejoin: round;
}

.consent__icon circle { fill: currentColor; }
.consent__text { flex: 1; min-width: 0; }
.consent__title { margin: 0; font-size: 16px; letter-spacing: 0.04em; }
.consent__desc { margin: 8px 0 0; color: var(--mute); font-size: 14px; line-height: 1.7; letter-spacing: 0.04em; word-break: auto-phrase; }
.consent__desc a { color: var(--ink); text-underline-offset: 4px; white-space: nowrap; }
.consent__actions { display: flex; flex: none; gap: 16px; }

/* セカンダリボタンは文字の色の線、プライマリボタンは明るい塗りに暗い色の文字 */
.consent__button {
  height: 48px;
  padding: 0 24px;
  border: 0;
  border-radius: 999px;
  box-shadow: inset 0 0 0 1px var(--ink);
  background: none;
  color: var(--ink);
  font: inherit;
  font-size: 14px;
  font-weight: 600;
  letter-spacing: 0.04em;
  white-space: nowrap;
  cursor: pointer;
}

.consent__button--fill { box-shadow: none; background-color: var(--ink); color: var(--ground); }

.consent-open {
  padding: 0;
  border: 0;
  background: none;
  color: inherit;
  font: inherit;
  text-decoration: underline;
  text-underline-offset: 4px;
  cursor: pointer;
}

.consent a:focus-visible,
.consent__button:focus-visible,
.consent-open:focus-visible { outline: 2px solid currentColor; outline-offset: 4px; }

/* 狭い画面では、ボタンを文言の下に横いっぱいに並べる */
@media (max-width: 720px) {
  .consent { right: 16px; bottom: 16px; left: 16px; flex-wrap: wrap; padding: 24px; }
  .consent__icon { display: none; }
  .consent__actions { width: 100%; }
  .consent__button { flex: 1; padding: 0 16px; }
}

JavaScript
import { gsap } from "gsap";

const KEY = "cookie-consent-v1"; // 選んだ結果を保存するキー。ポリシーを変えたら末尾の数字を上げると、全員にもう一度たずねる
const reduceMotion = window.matchMedia("(prefers-reduced-motion: reduce)").matches;
const SPEED = reduceMotion ? 20 : 1; // 動きを減らす設定では、ほぼ一瞬で切り替える

const banner = document.querySelector(".consent");
const items = banner.querySelectorAll(".consent__icon, .consent__text, .consent__button");
let motion = null;
let isOpen = false;
let returnTo = null; // 「Cookieの設定」から開いたときは、閉じたあとフォーカスをそこへ戻す

// ストレージを切っている環境でも止まらないように、読み書きの失敗は無視する
function load() {
  try {
    return localStorage.getItem(KEY);
  } catch {
    return null;
  }
}

function save(value) {
  try {
    localStorage.setItem(KEY, value);
  } catch {}
}

// 選んだ結果をほかのスクリプトへ知らせる。アクセス解析などは "all" のときだけ読み込む
// 例: document.addEventListener("cookieconsent", (event) => event.detail === "all" && loadAnalytics());
const announce = (value) => document.dispatchEvent(new CustomEvent("cookieconsent", { detail: value }));

const run = (timeline) => {
  motion?.kill();
  motion = timeline.timeScale(SPEED);
};

function show(from) {
  if (isOpen) return;
  isOpen = true;
  returnTo = from ?? null;
  banner.hidden = false;
  gsap.set(banner, { "--fold": 0 });
  run(
    gsap
      .timeline()
      // 画面の下の外から、勢いよく現れてゆっくり止まる
      .fromTo(banner, { y: banner.offsetHeight + 24 }, { y: 0, duration: 0.9, ease: "expo.out" }, 0)
      .fromTo(items, { opacity: 0, y: 12 }, { opacity: 1, y: 0, duration: 0.6, ease: "power3.out", stagger: 0.06 }, 0.15)
  );
  if (from) banner.querySelector("button").focus({ preventScroll: true });
}

function choose(value) {
  if (!isOpen) return;
  isOpen = false;
  save(value);
  announce(value);
  const hadFocus = banner.contains(document.activeElement);
  run(
    gsap
      .timeline({
        onComplete: () => {
          banner.hidden = true;
          if (hadFocus) returnTo?.focus();
        },
      })
      // 中身を先に消してから、上の縁から下の縁へ畳みながら少し下へずらす
      .to(items, { opacity: 0, duration: 0.18, ease: "power1.in" }, 0)
      .to(banner, { "--fold": 100, duration: 0.5, ease: "power3.inOut" }, 0.1)
      .to(banner, { y: 16, duration: 0.5, ease: "power2.in" }, 0.1)
  );
}

banner.querySelectorAll("[data-consent]").forEach((button) => {
  button.addEventListener("click", () => choose(button.dataset.consent));
});
document.querySelectorAll("[data-consent-open]").forEach((button) => {
  button.addEventListener("click", () => show(button));
});

// 選んだ結果が保存されていれば、バナーは表示せずに結果だけを知らせる
const saved = load();
if (saved) announce(saved);
else show();

オーバーレイをすべて見る