Skip to content

Instantly share code, notes, and snippets.

@bjornpatje
Created July 19, 2022 13:13
Show Gist options
  • Select an option

  • Save bjornpatje/10980744f09239d83bd34f4db1526e00 to your computer and use it in GitHub Desktop.

Select an option

Save bjornpatje/10980744f09239d83bd34f4db1526e00 to your computer and use it in GitHub Desktop.
Horizontal Scroll with GSAP
<!-- HORIZONTAL SCROLL -->
<script src="https://cdnjs.cloudflare.com/ajax/libs/gsap/3.5.1/gsap.min.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/gsap/3.5.1/ScrollTrigger.min.js"></script>
<script type="text/javascript">
if(jQuery('.page-id-735')){
gsap.registerPlugin(ScrollTrigger);
var allSections = jQuery('.horizontal-section');
allSections.wrapAll('<div class="horizontal-container"/>');
const sections = gsap.utils.toArray(".horizontal-section");
let maxWidth = 0;
const getMaxWidth = () => {
maxWidth = 0;
sections.forEach((section) => {
maxWidth += section.offsetWidth;
});
};
getMaxWidth();
ScrollTrigger.addEventListener("refreshInit", getMaxWidth);
gsap.to(sections, {
x: () => `-${maxWidth - window.innerWidth}`,
ease: "none",
scrollTrigger: {
trigger: ".horizontal-container",
pin: true,
scrub: 1,
end: () => `+=${maxWidth}`,
invalidateOnRefresh: true
}
});
}
</script>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment