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

配色

音楽

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

ジャンル

ページ遷移

クリックした場所から円が広がり、次のページが開く

デモ

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

仕様

用途
ページ遷移
動作
DOM
種類
レシピ
動作確認
three 0.186.0・gsap 3.15.0
公開日

しくみと調整

クリックした場所に小さな円がぽんと開き、その円の内側に次のページが見えてきます。裏で読み込んだ次のページを今のページの上に重ね、clip-path の circle() で切り抜いた円の半径を0から画面いっぱいまで広げ、覆いきったところでページを差し替えます。GSAPでは circle() の指定を文字列のままアニメーションさせられます。

円は power2.inOut で1秒かけて開きます。出だしをゆっくりにすることで、どこを起点に円が開き始めたのかを目で追いやすくしました。このとき、元のページは黒い膜を重ねながら0.94倍に縮め、次のページはクリック位置を中心に1.08倍から少し長めの時間をかけて等倍へ戻します。前後のページに明暗と大きさの差が生まれるため、円の輪郭がくっきり引き立ちます。

円の最大半径は、クリックした場所から最も遠い画面の角までの距離を Math.hypot(三平方の定理で2点間の距離を求める関数)で計算しています。固定の半径にしてしまうと、画面の端を押したときに反対側の角まで覆いきれないことがあるためです。Enterキーでリンクを開いた場合はクリック座標がないため、リンク要素の中央を起点にします。

元のページは、途中までスクロールしていても、いま見えている画面の中央を軸にして縮めます。なお、fetch などで読み込んだHTML内のスクリプトは差し替えただけでは動かないため、ページごとの初期化処理は差し替え後に呼び直す必要があります。前後のページの背景色に明るさの差があると、円の縁がいっそう際立ちます。

コード

HTML
<!-- 遷移で差し替えるのは data-page の中だけ。次のページも同じ形で書き、背景色はページのクラスで変える -->
<div class="page page--paper" data-page>
  <header class="site-header">
    <a class="site-header__logo" href="/">Hi! I'm HIGASHIYUKI&reg;</a>
    <nav aria-label="メイン">
      <ul class="site-header__links">
        <li><a href="/shop/">ショップ</a></li>
        <li><a href="/journal/">読みもの</a></li>
        <li><a href="/about/">私たちについて</a></li>
      </ul>
    </nav>
  </header>

  <main class="hero">
    <p class="hero__eyebrow">New season — 2027</p>
    <h1 class="hero__title">ゆっくり過ごす<br>朝のための器</h1>
    <p class="hero__lead">ここに説明文が入ります。</p>
    <a class="button" href="/shop/">コレクションを見る <span aria-hidden="true">&rarr;</span></a>
  </main>
</div>

CSS
:root {
  --depth: #000000; /* 縮んだページの周りに見える奥の背景と、縮んでいくページに重ねて暗くする層の色 */
  --gutter: clamp(24px, 7vw, 96px);
}

/* ページの色はページごとに持つ。明暗を分けておくと、円の縁がはっきり見える */
.page {
  min-height: 100vh;
  background: var(--page-bg);
  color: var(--page-ink);
}

.page--paper { --page-bg: #f2f1ed; --page-ink: #262626; --page-mute: #6b6b6b; --button-bg: #262626; --button-ink: #ffffff; }
.page--night { --page-bg: #111111; --page-ink: #eeece6; --page-mute: #a8a6a0; --button-bg: #eeece6; --button-ink: #111111; }

/* 遷移のあいだは、縮んだページの周りを奥の背景色にする */
.is-revealing { background: var(--depth); }
.is-revealing body { background: none; }

/* 次のページを重ねる層と、今のページに重ねて暗くする層(どちらも JS が作って、終わったら消す) */
.reveal-layer {
  position: fixed;
  inset: 0;
  z-index: 100;
  overflow: hidden;
}

.reveal-shade {
  position: fixed;
  inset: 0;
  z-index: 99;
  background: var(--depth);
  pointer-events: none;
}

.site-header {
  display: flex;
  flex-wrap: wrap; /* 狭い画面では、リンクをロゴの下の行へ折り返す */
  align-items: center;
  gap: 16px 32px;
  padding: 32px var(--gutter);
  font-size: 14px;
  font-weight: 600;
  letter-spacing: 0.04em;
}

.site-header__logo {
  margin-right: auto;
  font-size: 18px;
  font-weight: 700;
  letter-spacing: 0.12em;
  white-space: nowrap;
}

.site-header__links { display: flex; flex-wrap: wrap; gap: 8px 32px; margin: 0; padding: 0; list-style: none; white-space: nowrap; }
.site-header a { color: inherit; text-decoration: none; }

.hero { padding: 96px var(--gutter); }

.hero__eyebrow {
  margin: 0 0 24px;
  font-size: 13px;
  font-weight: 700;
  letter-spacing: 0.14em;
  text-transform: uppercase;
  color: var(--page-mute);
}

.hero__title { margin: 0; font-size: clamp(44px, 7vw, 72px); font-weight: 800; line-height: 1.05; letter-spacing: -0.02em; }
.hero__lead { margin: 24px 0 40px; font-size: 18px; line-height: 1.8; letter-spacing: 0.04em; word-break: auto-phrase; }

.button {
  display: inline-flex;
  align-items: center;
  gap: 8px;
  height: 48px;
  padding: 0 32px;
  border-radius: 999px;
  background: var(--button-bg);
  color: var(--button-ink);
  font-size: 15px;
  font-weight: 600;
  letter-spacing: 0.04em;
  text-decoration: none;
}

.page a:focus-visible { outline: 2px solid currentColor; outline-offset: 4px; }

/* 遷移のあとに見出しへ移したフォーカスは、フォーカスリングを表示しない(スクリーンリーダーの位置を移すためだけのもの) */
.page h1[tabindex="-1"]:focus { outline: none; }

JavaScript
import { gsap } from "gsap";

const REVEAL = 1; // 円が画面を覆いきるまで(秒)
const EASE = "power2.inOut";
const SINK = 0.94; // 前のページが縮む先
const SHADE = 0.5; // 前のページに重ねて暗くする層の濃さ
const reduceMotion = window.matchMedia("(prefers-reduced-motion: reduce)").matches;
let busy = false;
let shownPath = location.pathname; // いま見せているページ

history.scrollRestoration = "manual"; // スクロール位置は、差し替えたあとに自分で戻す

// 次のページの HTML を読み、差し替える部分(data-page)とタイトルを取り出す
async function load(url) {
  const response = await fetch(url);
  if (!response.ok) throw new Error(response.statusText);
  const doc = new DOMParser().parseFromString(await response.text(), "text/html");
  return { next: doc.querySelector("[data-page]"), title: doc.title };
}

// (x, y) から円を広げて次のページを見せ、開ききったら今のページと入れ替える
async function reveal(url, x, y, push) {
  const { next, title } = await load(url);
  if (push) history.pushState({}, "", url);
  shownPath = new URL(url).pathname;
  const current = document.querySelector("[data-page]");
  const layer = Object.assign(document.createElement("div"), { className: "reveal-layer" });
  const shade = Object.assign(document.createElement("div"), { className: "reveal-shade" });
  layer.append(next);
  document.body.append(shade, layer);
  document.documentElement.classList.add("is-revealing");

  // いちばん遠い角までの距離。ここまで広げると、どこを押しても画面を覆いきる
  const radius = Math.hypot(Math.max(x, innerWidth - x), Math.max(y, innerHeight - y));
  // 前のページは、ページ全体の中央ではなく、いま見えている範囲の中央へ縮める
  const middle = innerHeight / 2 - current.getBoundingClientRect().top;
  await gsap
    .timeline()
    .timeScale(reduceMotion ? 20 : 1) // 動きを減らす設定では、ほぼ一瞬で切り替える
    .fromTo(layer, { clipPath: `circle(0px at ${x}px ${y}px)` }, { clipPath: `circle(${radius}px at ${x}px ${y}px)`, duration: REVEAL, ease: EASE })
    .fromTo(next, { scale: 1.08, transformOrigin: `${x}px ${y}px` }, { scale: 1, duration: 1.3, ease: "expo.out" }, 0)
    .fromTo(current, { scale: 1, transformOrigin: `50% ${middle}px` }, { scale: SINK, duration: 0.8, ease: "power2.out" }, 0)
    .fromTo(shade, { opacity: 0 }, { opacity: SHADE, duration: 0.8, ease: "power2.out" }, 0);

  gsap.set(next, { clearProps: "transform,transformOrigin" });
  current.replaceWith(next);
  layer.remove();
  shade.remove();
  document.documentElement.classList.remove("is-revealing");
  document.title = title;
  scrollTo(0, 0);
  // スクリーンリーダーとキーボードの位置を、新しいページの見出しへ移す
  const heading = next.querySelector("h1");
  heading?.setAttribute("tabindex", "-1");
  heading?.focus({ preventScroll: true });
}

async function go(url, x, y, push) {
  if (busy) return;
  busy = true;
  try {
    await reveal(url, x, y, push);
  } catch {
    location.href = url; // 読み込めなかったら、ふつうに移動する
  } finally {
    busy = false;
  }
}

// 同じサイトのほかのページへのリンクだけ横取りする(別タブ・修飾キー付き・同じページ内のリンクは邪魔しない)
document.addEventListener("click", (event) => {
  const link = event.target.closest("a[href]");
  if (!link || event.defaultPrevented || event.button !== 0) return;
  if (event.metaKey || event.ctrlKey || event.shiftKey || event.altKey || link.target || link.hasAttribute("download")) return;
  const url = new URL(link.href);
  if (url.origin !== location.origin || url.pathname === location.pathname) return;
  event.preventDefault();
  // Enter で押したときは座標が無い(0, 0)ので、リンクの中央から開く
  const box = link.getBoundingClientRect();
  const byKey = event.detail === 0;
  const x = byKey ? box.left + box.width / 2 : event.clientX;
  const y = byKey ? box.top + box.height / 2 : event.clientY;
  go(url.href, x, y, true);
});

// ブラウザの戻る・進むでは、画面の中央から開く(同じページの中の移動は除く)
addEventListener("popstate", () => {
  if (location.pathname !== shownPath) go(location.href, innerWidth / 2, innerHeight / 2, false);
});

ページ遷移をすべて見る