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="scroll-wrap">
  <div class="empty">
    <a href="#anchor" class="js-anchor">
      Anchor
    </a>
  </div>
  <div class="empty">
  </div>
  <div class="scroll-elem">
    <div class="indicators">
      <div data-index="1" class="indicator active"></div>
      <div data-index="2" class="indicator"></div>
      <div data-index="3" class="indicator"></div>
    </div>
    <div class="scroll-item">
      <div class="scroll-item-inner">
        <div class="text">1</div>
      </div>
    </div>
    <div class="scroll-item">
      <div class="scroll-item-inner">
        <div class="text">2</div>
      </div>
    </div>
    <div class="scroll-item">
      <div class="scroll-item-inner">
        <div class="text">3</div>
      </div>
    </div>
  </div>
  <div class="scroll-end empty">
  </div>
  <div class="empty">
  </div>
  <div class="empty" id="anchor">
    Anchor section
  </div>
   <div class="empty">
  </div>
   <div class="empty">
  </div>
   <div class="empty">
  </div>
  
</div>
              
            
!

CSS

              
                body {
  font-family: system-ui;
  color: #fff;
  text-align: center;
  margin: 0;
}
.js-anchor {
  font-size: 100px;
}
.empty {
  background: red;
  height: 300px;
  border: 1px solid;
}
.scroll-elem {
  background: #000;
  height: 400px;
  overflow: hidden;
  position: relative;
}
.scroll-item {
  width: 100%;
  height: 100%;
  border: 1px solid;
  background: #000;
}
.scroll-item:first-child {
  z-index: 1;
}
.scroll-item-inner {
  padding-top: 150px;
  height: 100%;
  position: relative;
}
.indicators {
  position: absolute;
  top: 50px;
  left: 50%;
  transform: translateX(-50%);
  display: flex;
  z-index: 10;
}
.indicator {
  cursor: pointer;
  width: 20px;
  height: 20px;
  border-radius: 50%;
  margin: 0px 10px;
  background: yellow;
}
.indicator.active {
  background: green;
}
.text {
  font-size: 150px;
}

              
            
!

JS

              
                console.clear();

let points = gsap.utils.toArray(".scroll-item");

let tl = gsap.timeline({
  scrollTrigger: {
    trigger: ".scroll-elem",
    endTrigger: ".scroll-end",
    start: "bottom bottom",
    markers: true,
    scrub: true,
    pin: ".scroll-wrap",
    snap: {
      snapTo: 1 / points.length
    }
  }
});

points.forEach(function (elem, i) {
  gsap.set(elem, { position: "absolute", bottom: 0 });
  tl.to(elem, { zIndex: i + 1 }, i).from(
    elem.querySelector(".text"),
    { autoAlpha: 0, translateY: -100 },
    i
  );
  if (i != points.length - 1) {
    tl.to(
      elem.querySelector(".text"),
      { autoAlpha: 0, translateY: 100 },
      i + 1
    );
  }
});
gsap.utils.toArray(".indicator").forEach(function (a, i) {
  a.addEventListener("click", function (e) {
    e.preventDefault();
    const start = tl.scrollTrigger.start;
    const end = tl.scrollTrigger.end;
    gsap.to(window, {
      duration: 0.65,
      ease: "none",
      scrollTo: {
        y: end - ((end - start) * Math.abs(i - (points.length - 1))) / points.length
      }
    });
  });
});
let anchors = gsap.utils.toArray(".js-anchor").map((el) => document.querySelector(el.getAttribute("href"))),
    scrollLookup = getScrollLookup(anchors, {start: "top top", pinnedContainer: ".scroll-wrap" });

gsap.utils.toArray(".js-anchor").forEach(function (a, i) {
  a.addEventListener("click", function (e) {
    e.preventDefault();
    gsap.to(window, {
      duration: 0.65,
      ease: "none",
      scrollTo: scrollLookup(anchors[i])
    });
  });
});

ScrollTrigger.sort();




/*
Returns a FUNCTION that you can feed an element to get its scroll position.
- targets: selector text, element, or Array of elements
- config: an object with any of the following optional properties:
  - start: defaults to "top top" but can be anything like "center center", "100px 80%", etc. Same format as "start" and "end" ScrollTrigger values.
  - containerAnimation: the horizontal scrolling tween/timeline. Must have an ease of "none"/"linear".
  - pinnedContainer: if you're pinning a container of the element(s), you must define it so that ScrollTrigger can make the proper accommodations.
*/
function getScrollLookup(
  targets,
  { start, pinnedContainer, containerAnimation }
) {
  let triggers = gsap.utils.toArray(targets).map((el) =>
      ScrollTrigger.create({
        trigger: el,
        start: start || "top top",
        pinnedContainer: pinnedContainer,
        refreshPriority: -10,
        containerAnimation: containerAnimation
      })
    ),
    st = containerAnimation && containerAnimation.scrollTrigger;
  return (target) => {
    let t = gsap.utils.toArray(target)[0],
      i = triggers.length;
    while (i-- && triggers[i].trigger !== t) {}
    if (i < 0) {
      return console.warn("target not found", target);
    }
    return containerAnimation
      ? st.start +
          (triggers[i].start / containerAnimation.duration()) *
            (st.end - st.start)
      : triggers[i].start;
  };
}

              
            
!
999px

Console