Pen Settings

HTML

CSS

CSS Base

Vendor Prefixing

Add External Stylesheets/Pens

Any URLs added here will be added as <link>s in order, and before the CSS in the editor. You can use the CSS from another Pen by using its URL and the proper URL extension.

+ add another resource

JavaScript

Babel includes JSX processing.

Add External Scripts/Pens

Any URL's added here will be added as <script>s in order, and run before the JavaScript in the editor. You can use the URL of any other Pen and it will include the JavaScript from that Pen.

+ add another resource

Packages

Add Packages

Search for and use JavaScript packages from npm here. By selecting a package, an import statement will be added to the top of the JavaScript editor for this package.

Behavior

Auto Save

If active, Pens will autosave every 30 seconds after being saved once.

Auto-Updating Preview

If enabled, the preview panel updates automatically as you code. If disabled, use the "Run" button to update.

Format on Save

If enabled, your code will be formatted when you actively save your Pen. Note: your code becomes un-folded during formatting.

Editor Settings

Code Indentation

Want to change your Syntax Highlighting theme, Fonts and more?

Visit your global Editor Settings.

HTML

              
                <div class="wrapper d-flex flex-nowrap">
  <section id='one' class="section section--large flex-shrink-0 vw-100 vh-100 d-flex justify-content-center align-items-center">
    Part One
  </section>
  <section id='two' class="section section--dark section--small vh-100 flex-shrink-0 d-flex justify-content-center align-items-center">
    Part Two
  </section>
  <section class="section section--small vh-100 flex-shrink-0 d-flex justify-content-center align-items-center">
    Part Three
  </section>
  <section class="section section--large flex-shrink-0 vw-100 vh-100 d-flex justify-content-center align-items-center four">
    Part Four
  </section>
</div>

<nav>
  <div>one</div>
  <div>two</div>
  <div>three</div>
  <div>four</div>
</nav>
              
            
!

CSS

              
                .section {
  &--large {
    width: 100vw;
    background-color: #e4002b;
    color: white;
  }

  &--small {
    width: 46rem;
  }

  &--dark {
    color: white;
    background-color: black;
  }
}

section.four {
    background-color: #0a1b65;
}

nav {
  position: fixed;
  top: 0;
  left: 50%;
  display: flex;
  transform: translateX(-50%);
}
nav div {
  flex-basis: 25%;
  width: 80px;
  text-align: center;
  height: 50px;
  font-size: 21px;
  line-height: 50px;
  cursor: pointer;
  color: white;
  z-index: 10;
  user-select: none;
  background-color: rgba(0,0,0,0.6);
}
              
            
!

JS

              
                gsap.registerPlugin(ScrollTrigger);

let sections = gsap.utils.toArray("section"),
    nav = gsap.utils.toArray("nav div"),
    getMaxWidth = () => sections.reduce((val, section) => val + section.offsetWidth, 0),
    maxWidth = getMaxWidth(),
    scrollSpeed = 4,
    snapProgress,
    lastScrollTween = 0,
    tl = gsap.timeline();

	console.log({ sections, maxWidth });
	console.log(window.innerWidth - maxWidth);
	console.log(maxWidth / scrollSpeed);

tl.to(sections, {
  x: () => window.innerWidth - maxWidth,
  duration: 1,
  ease: "none"
});

ScrollTrigger.create({
  animation: tl,
  trigger: ".wrapper",
  pin: true,
  scrub: 1,
  end: () => "+=" + maxWidth / scrollSpeed,
  invalidateOnRefresh: true
});


function init() {
  gsap.set(sections, {x: 0});
  maxWidth = getMaxWidth();
  let position = 0,
      distance = maxWidth - window.innerWidth;
  // add a label for each section to the timeline (for "labelsDirectional" functionality):
  tl.add("label0", 0);
  sections.forEach((section, i) => {
    let progress = position;
    position += section.offsetWidth / distance;
    tl.add("label" + (i+1), position);
    nav[i].onclick = () => { // link clicks should trigger a scroll tween to the appropriate spot
      snapProgress = progress; // save the current progress so that if we can return it in the directionalSnap() when called right after the scrollTo tween is done (because ScrollTrigger would normally look at the velocity and snap, causing it to overshoot to the next section)
      lastScrollTween = Date.now(); // for checking in the directionalSnap() if there was a recent scrollTo that finished, in which case we'd skip the snapping (well, return the current snapProgress)
      gsap.to(window, {scrollTo: maxWidth / scrollSpeed * progress, duration: 1, overwrite: "auto"});
    };
  });
}

init();
ScrollTrigger.addEventListener("refreshInit", init); // on resize, things must be recalculated








// a helper function for doing "labelsDirectional" snapping, but we can't use that directly since we're doing some special things with scrollTo tweens, and we need a way to skip the snap if a scrollTo recently finished (otherwise it'll overshoot to the next section)
function directionalSnap(timeline) {
		return (value, st) => {
      if (Date.now() - lastScrollTween < 1650) { // recently finished doing a tweened scroll (clicked link), so don't do any snapping.
        return snapProgress;
      }
      let a = [],
        labels = timeline.labels,
        duration = timeline.duration(),
        p, i;
      for (p in labels) {
        a.push(labels[p] / duration);
      }
			a.sort((a, b) => a - b);
			if (st.direction > 0) {
				for (i = 0; i < a.length; i++) {
					if (a[i] >= value) {
						return a[i];
					}
				}
				return a.pop();
			} else {
				i = a.length;
				while (i--) {
					if (a[i] <= value) {
						return a[i];
					}
				}
			}
			return a[0];
		};
	}

gsap.to("#two", {
				backgroundColor: 'green',
				scrollTrigger: {
					trigger: "#one",
					markers: true,
					horizontal: true,
					start: "right 60%",
					scrub: true,
					invalidateOnRefresh: true,
					toggleClass: "active",

					onEnter: () => {
						console.log("Enter");
					},
				},
			});
              
            
!
999px

Console