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

配色

音楽

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

ジャンル

カード

単色のパネルが通り過ぎると写真が現れる

デモ

仕様

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

しくみと調整

スクロールなどで写真が画面に入ると、単色のパネルが左から伸びてきて枠いっぱいを覆います。続けてパネルが右へ縮みながら抜けていくと、その後ろから写真が現れる仕組みです。写真は少し拡大された状態で登場し、そこからゆっくりと本来の大きさに落ち着きます。

パネルが枠を覆いきるまでに0.55秒、右へ抜けていくのに0.6秒かかります。一方、写真が元の大きさに戻る動きには1.4秒かけているため、パネルが通り過ぎたあとも写真だけが少しの間ゆったり動き続け、心地よい余韻が残ります。

写真そのものを表示に切り替えるのは、パネルが枠全体を覆いきったまさにその瞬間です。これより少しでも早く表示してしまうと、まだパネルが通り過ぎていない反対側から写真がちらっと見えてしまい、仕掛けが丸見えになってしまいます。

パネルの色は画像ごとにCSS変数で変えられるので、写真の雰囲気に合わせて色を選べます。アニメーションは初めて画面に入ったときの1回だけ再生されますが、もし上下方向に通り抜けさせたい場合は、パネルが伸び縮みする向きと基準点を縦方向に変えるだけで対応できます。

コード

HTML
<div class="wipe-gallery">
  <figure class="wipe" style="--bar: #d29b97">
    <img class="wipe__image" src="/images/reveal-seaside.jpg" alt="">
    <span class="wipe__bar" aria-hidden="true"></span>
    <figcaption class="wipe__caption">01 海辺の夕暮れ</figcaption>
  </figure>
  <figure class="wipe" style="--bar: #d2b999">
    <img class="wipe__image" src="/images/reveal-meadow.jpg" alt="">
    <span class="wipe__bar" aria-hidden="true"></span>
    <figcaption class="wipe__caption">02 草原の朝</figcaption>
  </figure>
  <figure class="wipe" style="--bar: #98b1c4">
    <img class="wipe__image" src="/images/reveal-moonlit-cove.jpg" alt="">
    <span class="wipe__bar" aria-hidden="true"></span>
    <figcaption class="wipe__caption">03 月夜の入り江</figcaption>
  </figure>
</div>

CSS
.wipe-gallery {
  --bg: #111111;
  --ink: #eeece6;
  display: grid;
  grid-template-columns: repeat(auto-fit, minmax(240px, 1fr));
  gap: 24px;
  padding: 64px clamp(16px, 6vw, 96px);
  background: var(--bg);
}

.wipe {
  position: relative;
  overflow: hidden;
  margin: 0;
  aspect-ratio: 4 / 5;
  border-radius: 4px;
}

.wipe__image {
  position: absolute;
  inset: 0;
  width: 100%;
  height: 100%;
  object-fit: cover;
  visibility: hidden;
}

.wipe__bar {
  position: absolute;
  inset: 0;
  background: var(--bar);
  transform: scaleX(0);
  transform-origin: 0% 50%;
}

.wipe__caption {
  position: absolute;
  left: 20px;
  bottom: 20px;
  color: var(--ink);
  font-size: 18px;
  font-weight: 700;
  letter-spacing: -0.01em;
  text-shadow: 0 2px 10px rgba(0, 0, 0, 0.5);
}

JavaScript
import { gsap } from "gsap";

const COVER = 0.55; // カバーが覆う時間(秒)
const UNCOVER = 0.6; // カバーが抜ける時間(秒)
const ZOOM = 1.3; // 表示した瞬間の画像の拡大率。ここから 1 まで縮める
const reduceMotion = matchMedia("(prefers-reduced-motion: reduce)").matches;

document.querySelectorAll(".wipe").forEach((frame) => {
  const bar = frame.querySelector(".wipe__bar");
  const img = frame.querySelector(".wipe__image");
  gsap.set(img, { autoAlpha: 0 });

  // カバーは左を原点に伸びて覆い、覆いきった瞬間に画像を表示して、右を原点に縮んで抜ける
  const reveal = gsap
    .timeline({ paused: true })
    .fromTo(bar, { scaleX: 0, transformOrigin: "0% 50%" }, { scaleX: 1, duration: COVER, ease: "power4.inOut" })
    .set(img, { autoAlpha: 1 })
    .set(bar, { transformOrigin: "100% 50%" })
    .to(bar, { scaleX: 0, duration: UNCOVER, ease: "power4.inOut" })
    .fromTo(img, { scale: ZOOM }, { scale: 1, duration: 1.4, ease: "expo.out" }, "<");

  // 動きを減らす設定では、現れきった状態で見せる
  if (reduceMotion) {
    reveal.progress(1);
    return;
  }

  // 画面に入ったら 1 回だけ走らせる
  const io = new IntersectionObserver(
    ([entry], self) => {
      if (!entry.isIntersecting) return;
      self.unobserve(entry.target);
      reveal.play();
    },
    { rootMargin: "0px 0px -15%" }
  );
  io.observe(frame);
});

カードをすべて見る