Параллакс заголовка
Как можно сделать эффект параллакса для заголовка, чтобы при прокрутке страницы вниз заголовок не сразу прокручивался за пределы экрана, а притормаживал спускаясь немного ниже (к нижнему краю своей секции)?
body {
margin: 0;
padding: 0;
color: white;
text-align: center;
}
section {
position: relative;
height: 100vh;
}
section:nth-child(1),
section:nth-child(4) {
background-color: olivedrab;
}
section:nth-child(2) {
background: seagreen;
}
section:nth-child(3) {
background: darkolivegreen;
}
h1,
h2,
h3,
h4 {
position: absolute;
top: 50%;
left: 50%;
transform: translate(-50%, -50%);
font-size: 2rem;
}
<section>
<h1>Заголовок</h1>
</section>
<section>
<h2>Подзаголовок 1</h2>
</section>
<section>
<h3>Подзаголовок 2</h3>
</section>
<section>
<h4>Подзаголовок 3</h4>
</section>
Ответы (1 шт):
Автор решения: Sevastopol'
→ Ссылка
Вариант на чистом CSS:
body {
margin: 0;
padding: 0;
overflow: hidden;
background-color: olivedrab;
color: white;
font-size: 2rem;
text-align: center;
transform: translateZ(0);
}
section {
position: relative;
height: 100vh;
transform-style: preserve-3d;
}
section>div {
position: absolute;
top: 0;
left: 0;
right: 0;
bottom: 0;
transform-origin-x: 100%;
}
section>div>span {
position: absolute;
top: 50%;
left: 50%;
transform: translate(-50%, -50%);
}
.section__list {
background: darkolivegreen;
}
section:nth-child(2) div {
background: seagreen;
}
section:nth-child(3) {
z-index: 3;
}
@supports (perspective: 1px) and (not (-webkit-overflow-scrolling: touch)) {
main {
height: 100vh;
perspective: 1px;
perspective-origin-x: 100%;
scroll-behavior: smooth;
overflow-y: scroll;
-webkit-overflow-scrolling: touch;
overflow-x: hidden;
}
section:nth-child(1) {
margin-bottom: 50vh;
}
.section__list {
transform: translateZ(0);
}
.section__title {
transform: translateZ(-2px) scale(3);
}
}
<main>
<section>
<div class="section__title"><span>Заголовок</span></div>
</section>
<section>
<div class="section__list"><span>Подзаголовок 1</span></div>
</section>
<section>
<div class="section__list"><span>Подзаголовок 2</span></div>
</section>
<section>
<div class="section__title"><span>Подзаголовок 3</span></div>
</section>
</main>