<!DOCTYPEhtml><htmllang="en"><head> <metacharset="UTF-8"> <metaname="viewport"content="width=device-width, initial-scale=1.0"> <linkrel="stylesheet"href="lionel.css"> <title>Document</title></head><body> <divid="smooth-wrapper"> <divid="smooth-content"> <divclass="description"> <div> <h1>Horizontal "<code>containerAnimation</code>"</h1> <p>Scroll this page vertically and you'll see a horizontal fake-scrolling section where a container is animated on the x-axis using a ScrollTrigger animation. With <code>containerAnimation</code> you can trigger animations when certain elements <i>inside</i> that container enter the viewport horizontally! It's like a ScrollTrigger inside of a ScrollTrigger. 🤯 </p> </div> </div> <divclass="container"> <sectionclass="panel blue"> <p>Scroll down to animate horizontally Blue</p> <divclass="box-1 box">box-1</div> </section> <sectionclass="panel orange"> <p>Scroll down to animate horizontally Red</p> <divclass="box-2 box">box-2</div> </section> <sectionclass="panel red"> <p>Scroll down to animate horizontally Blue</p> <divclass="box-3 box">box-3</div> </section> <sectionclass="panel purple"> <p>Scroll down to animate horizontally Red</p> <divclass="box-4 box">box-4</div> </section> <sectionclass="panel green"> <p>Scroll down to animate horizontally Blue</p> <divclass="box-5 box">box-5</div> </section> </div> <divclass="final"> <div> <h1>Wasn't that fun?</h1> <p>Here are a few caveats to keep in mind:</p> <ul> <li>The fake-scrolling animation (just the part that's moving the container horizontally) must have no easing (<code>ease: "none"</code>).</li> <li>Pinning and snapping won't work on ScrollTriggers with a <code>containerAnimation</code>.</li> <li>The mapping of scroll position trigger points are based on the trigger element itself not being animated horizontally (inside the container). If you need to animate the trigger, you can either wrap it in a <div> and use that as the trigger instead or just factor the trigger's movement into your end position. For example, if you animate it left 100px, make the <code>end</code> 100px further to the left. </li> <li>Requires ScrollTrigger 3.8.0 or later</li> </ul> </div> </div> </div> </div> <scriptsrc="https://assets.codepen.io/16327/gsap-latest-beta.min.js?r=v3.11.3"></script> <scriptsrc="https://assets.codepen.io/16327/ScrollTrigger.min.js"></script> <scriptsrc="https://unpkg.com/gsap/dist/Observer.min.js"></script> <scriptsrc="ScrollSmoother.min.js"></script> <scriptsrc="lionel.js"></script></body></html>
gsap.registerPlugin(ScrollTrigger, Observer, ScrollSmoother);ScrollSmoother.create({ smooth:1,// how long (in seconds) it takes to "catch up" to the native scroll position effects:true,// looks for data-speed and data-lag attributes on elements smoothTouch:0.1// much shorter smoothing time on touch devices (default is NO smoothing on touch devices)});let sections =gsap.utils.toArray(".panel");let dragRatio =1;let scrollTo;let scrollTween =gsap.to(sections, { xPercent:-100* (sections.length-1), ease:"none", scrollTrigger: { trigger:".container", pin:true, markers:false, scrub:0.1,onRefresh: self => { dragRatio = (self.end -self.start) / ( (sections.length-1) * sections[0].offsetWidth); }, end:"+=3000" }});Observer.create({ target:".container", type:"wheel,touch,pointer",onPress: self => {self.startScroll =scrollTween.scrollTrigger.scroll(); scrollTo =gsap.quickTo(scrollTween.scrollTrigger,"scroll", { duration:0.5, ease:"power3" }); },onDrag: self => {scrollTo(self.startScroll + (self.startX -self.x) * dragRatio); }});// gsap.set(".box-1, .box-2", { y: 100 });ScrollTrigger.defaults({ markers:false });// red sectiongsap.to(".box-1", { y:-130, duration:2, ease:"none", scrollTrigger: { trigger:".box-1", containerAnimation: scrollTween, start:"left center", toggleActions:"play none none reset", }});// gray sectiongsap.to(".box-2", { y:-120, backgroundColor:"#1e90ff", ease:"none", scrollTrigger: { trigger:".box-2", containerAnimation: scrollTween, start:"center 80%", end:"center 20%", scrub:true }});// purple sectionScrollTrigger.create({ trigger:".box-3", containerAnimation: scrollTween, toggleClass:"active", start:"center 60%"});ScrollTrigger.create({ trigger:".box-4", containerAnimation: scrollTween, toggleClass:"active", start:"center 60%"});// green sectionScrollTrigger.create({ trigger:".green", containerAnimation: scrollTween, start:"center 65%", end:"center 51%",onEnter: () => {console.log("enter"); },onLeave: () => {console.log("leave") },onEnterBack: () => {console.log("enterBack") },onLeaveBack: () => {console.log("leaveBack") },onToggle: self => {console.log("active",self.isActive) },});