CSS
/* ---- base ---- */
* {
box-sizing: border-box;
}
html,
body {
height: 100%;
}
body {
margin: 0;
/* スクロール量をしっかり確保(viewport のスクロールを使う) */
min-height: 500vh;
}
.site-header {
position: sticky;
top: 0;
padding: 14px 20px;
color: #fff;
background: color-mix(in oklab, #0b1220 70%, transparent);
backdrop-filter: blur(6px);
border-bottom: 1px solid rgba(255, 255, 255, .1);
z-index: 1000;
}
/* ---- stage ---- */
.wrapper {
position: relative;
height: 450vh;
/* root のスクロールを使うので wrapper は overflow させない */
overflow: visible;
}
.parallax {
position: absolute;
aspect-ratio: 1 / 1;
border-radius: 12px;
box-shadow: 0 8px 24px rgba(0, 0, 0, .25);
will-change: transform;
}
.large {
width: 140px;
background: #A9E34B;
}
/* 近景(速い) */
.medium {
width: 100px;
background: #4DABF7;
}
/* 中景(基準) */
.small {
width: 70px;
background: #FFD43B;
}
/* 遠景(遅い) */
/* ---- keyframes ---- */
@keyframes parallax-fast {
from {
transform: translateY(600px);
}
to {
transform: translateY(0);
}
}
@keyframes parallax-slow {
from {
transform: translateY(-600px);
}
to {
transform: translateY(0);
}
}
/* ---- scroll-driven(ルートを明示)---- */
.large {
animation-name: parallax-fast;
animation-timing-function: linear;
animation-fill-mode: both;
animation-duration: 1ms;
animation-timeline: scroll(root block);
}
.small {
animation-name: parallax-slow;
animation-timing-function: linear;
animation-fill-mode: both;
animation-duration: 1ms;
animation-timeline: scroll(root block);
}
/* .medium は基準速度 → アニメなし */
/* ---- fallback(未対応ブラウザ) ---- */
@supports not (animation-timeline: scroll()) {
.wrapper {
display: none;
}
.fixed-parallax {
min-height: 70vh;
display: grid;
place-items: center;
font-size: clamp(24px, 4vw, 42px);
font-weight: 700;
text-shadow: 0 2px 12px rgba(0, 0, 0, .35);
background-position: center;
background-size: cover;
background-attachment: fixed;
}
.fixed-1 {
background-image: linear-gradient(135deg, #3b82f6, #0ea5e9);
}
.fixed-2 {
background-image: linear-gradient(135deg, #a78bfa, #ec4899);
}
.fixed-3 {
background-image: linear-gradient(135deg, #22c55e, #84cc16);
}
}
/* 対応ブラウザではフォールバック・セクションを消す */
@supports (animation-timeline: scroll()) {
.fixed-parallax {
display: none;
}
}