JSFiddle - React, Tailwind, and code Playground
by Hugo Vale Pereira
HTML
<script src="https://cdnjs.cloudflare.com/ajax/libs/gsap/3.12.5/gsap.min.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/gsap/3.12.5/ScrollTrigger.min.js"></script>
<div class="wrapper">
<section class="normal"><p>Section 1</p></section>
<section class="normal"><p>Section 2</p></section>
<section class="scene"><p>Section 3</p></section>
<section class="normal"><p>Section 4</p></section>
<section class="normal"><p>Section 5</p></section>
<section class="normal"><p>Section 6</p></section>
</div>
CSS
* {
margin: 0;
padding: 0;
box-sizing: border-box;
}
body {
font-family: system-ui, -apple-system, sans-serif;
}
section {
height: calc( 100vh - 40px );
background-color: teal;
display: flex;
justify-content: center;
align-items: center;
margin-bottom: 20px;
p {
font-size: 22px;
}
&.scene {
background-color: aqua;
}
}
.spacer {
height: 100vh;
display: flex;
align-items: center;
justify-content: center;
font-size: 2rem;
background: linear-gradient(135deg, #667eea 0%, #764ba2 100%);
color: white;
}
.spacer:nth-child(even) {
background: linear-gradient(135deg, #4facfe 0%, #00f2fe 100%);
}
.pin-wrapper {
height: 100vh; /* This creates the scroll space */
}
.fade-section {
height: 100vh;
display: flex;
align-items: center;
justify-content: center;
font-size: 3rem;
font-weight: bold;
background: linear-gradient(135deg, #f093fb 0%, #f5576c 100%);
color: white;
}
JavaScript
gsap.registerPlugin(ScrollTrigger);
const anim = gsap.from('section.scene p', {
scale: 0,
});
const st = ScrollTrigger.create({
trigger: '.scene',
start: 'top top+=20',
end:"+=100",
scrub: true,
markers: true,
pin: ".wrapper",
animation: anim,
onLeave: (self) => {
const start = self.start;
self.scroll(self.start); // <-- sets position to start of ScrollTrigger
self.disable() ; // <-- disables ScrollTrigger to get rid off pin-spacing, which is the white space you got
self.animation.progress(1);
}
});