【GSAP】scrollTo で作る高機能スムーススクロール!固定ヘッダー対策(offsetY)やイージング制御デモ
Webサイト内のアンカーリンク(目次やナビゲーション)をクリックした際、目的のセクションまで滑らかに移動する「スムーススクロール」は定番のUI演出です。
CSSの scroll-behavior: smooth; でも簡易的な実装は可能ですが、「移動スピードやイージング(加減速)を細かくこだわりたい」「固定ヘッダーがセクションの見出しに被ってしまうのを防ぎたい」「自動スクロール中にユーザーが画面を触ったら即座に動きを止めたい」といった要望を素のJavaScriptだけで実装しようとすると、非常に複雑な計算が必要になります。
そんな悩みをたった数行のコードで解決してくれるのが、GSAPの公式プラグイン ScrollToPlugin(scrollTo) です! 今回は、ScrollToPlugin の導入方法から便利なオプション、そして挙動の違いを実際に比較できるデモをご紹介します!
デモ動作
See the Pen Untitled by mikanbako (@mikanbako) on CodePen.
demo: 【GSAP】ScrollToPlugin(scrollTo)の使い方!
実装コード紹介
【HTML】
<div class="l-wrap">
<!-- 画面上部の固定ヘッダー(高さ約130px) -->
<header class="p-fixed-header js-header">
<div class="p-header-inner">
<!-- 設定切り替え行 -->
<div class="c-config-row">
<label class="c-toggle-label">
<input type="checkbox" class="js-offset-check" checked>
<span>offsetY で固定ヘッダー分(+少し余白)ずらす</span>
</label>
<div class="c-select-wrap">
<select class="c-select js-ease-select">
<option value="normal">⚡ 標準 (1.0秒 / power3.inOut)</option>
<option value="bounce">🏀 バウンド風 (1.2秒 / back.out)</option>
<option value="slow">🐢 autoKill実験用 (3.5秒かけて移動)</option>
</select>
</div>
</div>
<!-- ナビゲーションボタン行 -->
<nav class="c-nav-row">
<button class="c-btn js-scroll-btn" data-target="#sec1" type="button">Section 1</button>
<button class="c-btn js-scroll-btn" data-target="#sec2" type="button">Section 2</button>
<button class="c-btn js-scroll-btn" data-target="#sec3" type="button">Section 3</button>
<button class="c-btn c-btn--sub js-scroll-btn" data-target="0" type="button">⬆ 最上部 (0)</button>
<button class="c-btn c-btn--sub js-scroll-btn" data-target="max" type="button">⬇ 最下部 (max)</button>
</nav>
<!-- ステータス&autoKill検知ログ -->
<div class="c-status-bar js-status">
💡 ボタンをクリックしてスクロール動作を確認してみてね!
</div>
</div>
</header>
<!-- メインコンテンツ(各セクション) -->
<main class="l-main">
<section id="sec1" class="c-section c-section--1">
<div class="c-box">
<span class="c-tag">#sec1</span>
<h2>Section 1 の見出しです</h2>
<p>上のチェックボックスで「offsetY」をOFFにしてからボタンを押すと、この見出しが固定ヘッダーの下に潜り込んで隠れてしまうのが確認できます!</p>
</div>
</section>
<section id="sec2" class="c-section c-section--2">
<div class="c-box">
<span class="c-tag">#sec2</span>
<h2>Section 2 の見出しです</h2>
<p>セレクトボックスを「🐢 autoKill実験用 (3.5秒)」に変えてボタンを押し、移動している最中にマウスホイール(または画面スワイプ)を動かしてみてね!</p>
</div>
</section>
<section id="sec3" class="c-section c-section--3">
<div class="c-box">
<span class="c-tag">#sec3</span>
<h2>Section 3 の見出しです</h2>
<p>セレクタ(#sec3)だけでなく、数値の「0(最上部)」やキーワードの「"max"(ページ最下部)」へも簡単にスクロールできます✨</p>
</div>
</section>
<footer class="c-footer">
<p>🎉 ここがページの最下部(scrollTo: "max" の到着地点)です!</p>
</footer>
</main>
</div>
【CSS】
/* ベース装飾 */
.l-wrap {
background: #f7f7f7;
color: #3e3e3e;
}
/* 固定ヘッダー */
.p-fixed-header {
position: fixed;
top: 0;
left: 0;
z-index: 100;
width: 100%;
padding: 12px 15px;
background: rgb(255 255 255 / 90%);
box-shadow: 0 4px 20px rgb(0 0 0 / 10%);
backdrop-filter: blur(4px);
}
.p-header-inner {
display: flex;
flex-direction: column;
gap: 8px;
max-width: 650px;
margin: 0 auto;
}
/* 設定行 */
.c-config-row {
display: flex;
flex-wrap: wrap;
gap: 8px;
align-items: center;
justify-content: space-between;
font-size: 14px;
font-weight: bold;
}
.c-toggle-label {
display: flex;
gap: 6px;
align-items: center;
padding: 5px 10px;
color: #00838f;
cursor: pointer;
background: #e0f7fa;
border-radius: 6px;
cursor: pointer;
}
/* チェックボックス */
input[type=checkbox] {
position: relative;
display: inline-block;
width: 16px;
aspect-ratio: 1;
margin-right: 5px;
font-family: inherit;
font-size: 14px;
border: 1px solid #333;
border-radius: 3px;
background-color: white;
cursor: pointer;
}
input[type=checkbox]::before {
position: absolute;
top: 50%;
left: 50%;
width: 80%;
height: 40%;
content: "";
border-bottom: 2px solid #333;
border-left: 2px solid #333;
opacity: 0;
transform: translate(-50%, calc(-50% - 2px)) rotate(-45deg);
transition: opacity 0.3s ease-out;
}
input[type=checkbox]:checked::before {
opacity: 1;
}
/* セレクトボックス */
.c-select {
padding: 6px 24px 6px 8px;
font-family: inherit;
font-size: 14px;
font-weight: bold;
color: #333;
cursor: pointer;
outline: none;
border: 2px solid #cbd5e1;
border-radius: 6px;
}
.c-select-wrap {
position: relative;
cursor: pointer;
}
.c-select-wrap::after {
position: absolute;
top: 50%;
right: 12px;
width: 12px;
height: 10px;
content: "";
clip-path: polygon(0 0, 100% 0, 50% 100%);
background-color: #888;
transform: translateY(-50%);
pointer-events: none;
}
/* ナビボタン行 */
.c-nav-row {
display: grid;
grid-template-columns: repeat(5, 1fr);
gap: 6px;
}
.c-btn {
padding: 8px 6px;
font-family: inherit;
font-size: 14px;
font-weight: bold;
color: #fff;
cursor: pointer;
background: #1bb4d4;
border: none;
border-radius: 6px;
transition: 0.2s;
}
.c-btn:hover {
opacity: 0.85;
transform: translateY(-1px);
}
.c-btn--sub {
background: #495057;
}
/* ステータスバー */
.c-status-bar {
padding: 6px 12px;
font-size: 14px;
font-weight: bold;
color: #555;
text-align: center;
background: #f1f3f5;
border-radius: 4px;
transition: 0.2s;
}
.c-status-bar.is-moving {
color: #b8860b;
background: #fff3cd;
}
.c-status-bar.is-killed {
color: #e83e8c;
background: #ffe3e3;
}
.c-status-bar.is-done {
color: #28a745;
background: #d4edda;
}
/* メイン領域と各セクション */
.l-main {
/* 最初の要素が隠れないよう余白確保 */
padding-top: 300px;
}
.c-section {
display: flex;
flex-direction: column;
justify-content: flex-start;
min-height: 100vh;
padding: 0 20px 40px;
}
.c-section--1 {
background: #e0f7fa;
}
.c-section--2 {
background: #fff9db;
}
.c-section--3 {
background: #fff0f6;
}
/* セクション内のボックス(上端に配置してoffsetYの効果をわかりやすくする) */
.c-box {
width: 100%;
max-width: 600px;
padding: 30px;
margin: 0 auto;
background: #fff;
border-top: 6px solid #1bb4d4;
border-radius: 12px;
box-shadow: 0 8px 20px rgb(0 0 0 / 6%);
}
.c-section--2 .c-box {
border-top-color: #fab005;
}
.c-section--3 .c-box {
border-top-color: #e83e8c;
}
.c-tag {
display: inline-block;
padding: 4px 10px;
margin-bottom: 10px;
font-family: monospace;
font-size: 14px;
color: #fff;
background: #333;
border-radius: 4px;
}
.c-box h2 {
margin-bottom: 10px;
font-size: 22px;
color: #1bb4d4;
}
.c-box p {
font-size: 15px;
font-weight: bold;
line-height: 1.7;
color: #555;
}
.c-footer {
padding: 60px 20px;
font-size: 15px;
font-weight: bold;
color: #fff;
text-align: center;
background: #333;
}
【JavaScript】
// 💡 ScrollToPlugin の登録(必須!)
gsap.registerPlugin(ScrollToPlugin);
const header = document.querySelector('.js-header');
const offsetCheck = document.querySelector('.js-offset-check');
const easeSelect = document.querySelector('.js-ease-select');
const statusEl = document.querySelector('.js-status');
const scrollBtns = document.querySelectorAll('.js-scroll-btn');
scrollBtns.forEach(btn => {
btn.addEventListener('click', () => {
const rawTarget = btn.dataset.target;
// "0" の時は数値の 0 に変換、それ以外は文字列("#sec1" や "max")のまま扱う
const targetVal = rawTarget === '0' ? 0 : rawTarget;
// 💡 固定ヘッダーの高さ+余白15pxを offsetY として計算(OFFなら 0)
const offsetVal = offsetCheck.checked ? header.offsetHeight + 15 : 0;
// 選択されたモードに応じて duration と ease を設定
const mode = easeSelect.value;
let durationVal = 1.0;
let easeVal = 'power3.inOut';
if (mode === 'bounce') {
durationVal = 1.2;
easeVal = 'back.out(1.4)';
} else if (mode === 'slow') {
durationVal = 3.5;
easeVal = 'power1.inOut';
}
// ステータス表示の更新
statusEl.textContent = '🚀 スクロール移動中...(手動でホイールを回すと autoKill で止まります!)';
statusEl.className = 'c-status-bar is-moving';
// 💡 gsap.to(window, { scrollTo: ... }) でスクロール実行!
gsap.to(window, {
duration: durationVal,
ease: easeVal,
scrollTo: {
y: targetVal, // 移動先(セレクタ "#sec1"、数値 0、"max" など)
offsetY: offsetVal, // 固定ヘッダー分のズレ補正(px)
autoKill: true, // ユーザーが手動スクロールしたら即座に中断する!
// 手動操作で中断された時に呼ばれる専用コールバック
onAutoKill: () => {
statusEl.textContent = '⚡ autoKill発動!手動スクロールを検知したため自動移動をストップしました!';
statusEl.className = 'c-status-bar is-killed';
}
},
onComplete: () => {
statusEl.textContent = `✨ 移動完了!(ターゲット: ${rawTarget} / offsetY: ${offsetVal}px)`;
statusEl.className = 'c-status-bar is-done';
}
});
});
});
ScrollToPluginの導入と便利なオプション紹介
導入方法と一番シンプルな基本構文
ScrollToPlugin はGSAPの拡張プラグインのため、GSAP本体と合わせて読み込み、gsap.registerPlugin(ScrollToPlugin) で登録してから使用します。
【CDNで読み込む場合】(記事作成 2026/11 現在)
<script src="https://cdn.jsdelivr.net/npm/gsap@3.15/dist/gsap.min.js"></script>
<script src="https://cdn.jsdelivr.net/npm/gsap@3.15/dist/ScrollToPlugin.min.js"></script>
【npm / モジュール環境で読み込む場合】
import gsap from 'gsap';
import { ScrollToPlugin } from 'gsap/ScrollToPlugin';
gsap.registerPlugin(ScrollToPlugin);
画面全体をスクロールさせる時は、アニメーションの対象(第1引数)に window を指定し、scrollTo プロパティに行き先を渡すだけです!
// #about セクションまで、1秒かけて滑らかにスクロール!
gsap.to(window, {
duration: 1,
ease: 'power2.inOut',
scrollTo: '#about'
});
通常のGSAPアニメーションとまったく同じように duration(秒数) や ease(イージング) が使えるため、「最初はゆっくり動き出し、最後はピタッと減速して止まる」といった心地よいスクロール演出が自在に作れます。
行き先(y)の指定が超柔軟!セレクタ・数値・"max" まで対応
scrollTo に渡す行き先は、用途に合わせて3つの形式でに指定できます。
- CSSセレクタ(IDやクラス名)で指定:
scrollTo: "#section2"
対象の要素が画面の上端に来る位置をGSAPが自動で計算してスクロールしてくれます。 - 数値(ピクセル)で指定:
scrollTo: 0(または500など)
「トップへ戻るボタン」を作る際は、scrollTo: 0と書くだけでページ最上部(0px地点)へ戻せます。 "max"キーワードで指定:scrollTo: "max"
ページの高さがどれくらいあるかを計算しなくても、"max"と書くだけで「ページの最下部」まで自動でスクロールしてくれます!
固定ヘッダーの被りを防ぐオプション:offsetY
最も重宝するのが、オブジェクト形式で指定できる offsetY です! 上部に固定ヘッダー(position: fixed)があるサイトでそのまま #section へスクロールさせると、セクションの見出しが画面の一番上まで移動してしまい、固定ヘッダーの裏側に隠れて見えなくなるという問題が起きます。
そんな時は offsetY にヘッダーの高さ(px)を指定するだけで、その分だけ手前の位置で止めてくれます!
const headerHeight = document.querySelector('.header').offsetHeight;
gsap.to(window, {
duration: 1,
ease: 'power2.inOut',
scrollTo: {
y: '#section',
offsetY: headerHeight // 💡 ヘッダーの高さ分だけ下にずらして止める!(例: 80)
}
});
header.offsetHeight のように動的に高さを取得して渡せば、PCとスマホでヘッダーの高さが変わる場合でも柔軟に対応できます。
ユーザーの操作を邪魔しない:autoKill: true
もうひとつの強力な機能が autoKill: true です。 自動スクロールしている最中に、ユーザーが「やっぱり自分でスクロールして別の場所を見たい!」と思ってマウスホイールを回したりスマホ画面をスワイプした時、自動スクロールがそのまま動き続けてしまうと画面がガタガタと引っ張り合いになり、非常にストレスを与えてしまいます。
autoKill: true を設定しておけば、「スクロール中にユーザーの手動スクロール操作を検知した瞬間、自動的にGSAPのスクロールアニメーションを中断(Kill)してユーザーに操作権を譲る」という気配り設計がたった1行で実現できます!
gsap.to(window, {
duration: 1.5,
scrollTo: {
y: '#contact',
offsetY: 80,
autoKill: true, // 💡 ユーザーが途中でスクロールしたら自動移動を即ストップ!
onAutoKill: () => {
console.log('手動操作を検知したためスクロールを中断しました');
}
}
});
※onAutoKill手動操作で中断された時に呼ばれる専用コールバックを使うことで、スクロール中断時に独自の処理を実装可能です!
補足:公式ドキュメント
まとめ
今回はGSAPの ScrollToPlugin(scrollTo)について解説しました。
gsap.to(window, { scrollTo: ... })で、durationやeaseを使った滑らかなスクロールが作れる- 行き先は
"#id"(セレクタ)、0(最上部)、"max"(最下部)など指定できる offsetYを使えば、固定ヘッダーが見出しに被ってしまう問題を簡単に回避できるautoKill: trueを入れておけば、移動中にユーザーが手動スクロールした際に自動で動きを止めてくれる
素のJavaScriptで一から書くと大変なスクロール制御も、ScrollToPlugin を使えば驚くほどシンプルかつ高品質に実装できます。サイト内リンクや「トップへ戻るボタン」を作る際は、ぜひ活用してみてくださいね!