½ºÅ©·ÑÆ®¸®°Å¸¦ Ȱ¿ëÇÑ °¡·Î ½ºÅ©·Ñ °¶·¯¸®
Horizontal Scrolling Gallery with ScrollTrigger (GSAP ¿¹Á¦)
GSAP ScrollTrigger¸¦ Ȱ¿ëÇØ ¼¼·Î ½ºÅ©·ÑÀ» °¡·Î ½ºÅ©·Ñ ¾Ö´Ï¸ÞÀ̼ÇÀ¸·Î º¯È¯ÇÑ À̹ÌÁö °¶·¯¸® µ¥¸ð.
ÀÌ ¿¹Á¦´Â GreenSock Animation Platform(GSAP)ÀÇ ScrollTrigger Ç÷¯±×ÀÎÀ» ÀÌ¿ëÇÏ¿©, »ç¿ëÀÚÀÇ ¼¼·Î ½ºÅ©·Ñ µ¿ÀÛÀ» °¡·Î ¹æÇâÀ¸·Î º¯È¯ÇØ ÁÖ´Â °¡·Î ½ºÅ©·Ñ °¶·¯¸®¸¦ ±¸ÇöÇÕ´Ï´Ù.
HTML ±¸Á¶
<div id="smooth-wrapper">
<div id="smooth-content">
<section class="panel plain">
<h3>Scroll down for the Gallery</h3>
</section>
<section id="portfolio">
<div class="container-fluid">
<div class="horiz-gallery-wrapper">
<div class="horiz-gallery-strip">
<div class="project-wrap">
<img src="https://assets.codepen.io/16327/portrait-image-1.jpg" alt="" />
</div>
<div class="project-wrap">
<img src="https://assets.codepen.io/16327/portrait-image-2.jpg" alt="" />
</div>
<div class="project-wrap">
<img src="https://assets.codepen.io/16327/portrait-image-3.jpg" alt="" />
</div>
<div class="project-wrap">
<img src="https://assets.codepen.io/16327/portrait-image-4.jpg" alt="" />
</div>
<div class="project-wrap">
<img src="https://assets.codepen.io/16327/portrait-image-5.jpg" alt="" />
</div>
<div class="project-wrap">
<img src="https://assets.codepen.io/16327/portrait-image-6.jpg" alt="" />
</div>
<div class="project-wrap">
<img src="https://assets.codepen.io/16327/portrait-image-7.jpg" alt="" />
</div>
<div class="project-wrap">
<img src="https://assets.codepen.io/16327/portrait-image-8.jpg" alt="" />
</div>
</div>
</div>
</div>
</section>
<section class="panel plain">
<h3>That's it!</h3>
</section>
</div>
</div>
CSS ¼Ò½º
body {
overflow-x: hidden;
margin: 0;
}
.row,
.section,
section {
position: relative;
overflow: hidden;
}
.section,
section {
text-align: center;
}
.container-fluid {
width: 100%;
padding-right: 0;
padding-left: 0;
margin-right: auto;
margin-left: auto;
}
.horiz-gallery-strip,
.horiz-gallery-wrapper {
display: flex;
flex-wrap: nowrap;
will-change: transform;
position: relative;
}
.project-wrap {
width: 33vw;
padding: 2rem;
box-sizing: content-box;
}
.project-wrap img {
width: 100%;
aspect-ratio: 1/1;
object-fit: cover;
}
JS ¼Ò½º
gsap.registerPlugin(ScrollTrigger, ScrollSmoother);
const smoother = ScrollSmoother.create({
wrapper: "#smooth-wrapper",
content: "#smooth-content",
smooth: 2,
normalizeScroll: true,
ignoreMobileResize: true,
preventDefault: true
});
//Horizontal Scroll Galleries
if (document.getElementById("portfolio")) {
const horizontalSections = gsap.utils.toArray(".horiz-gallery-wrapper");
horizontalSections.forEach(function (sec, i) {
const pinWrap = sec.querySelector(".horiz-gallery-strip");
let pinWrapWidth;
let horizontalScrollLength;
function refresh() {
pinWrapWidth = pinWrap.scrollWidth;
horizontalScrollLength = pinWrapWidth - window.innerWidth;
}
refresh();
// Pinning and horizontal scrolling
gsap.to(pinWrap, {
scrollTrigger: {
scrub: true,
trigger: sec,
pin: sec,
start: "center center",
end: () => `+=${pinWrapWidth}`,
invalidateOnRefresh: true
},
x: () => -horizontalScrollLength,
ease: "none"
});
ScrollTrigger.addEventListener("refreshInit", refresh);
});
}