Pen Settings

HTML

CSS

CSS Base

Vendor Prefixing

Add External Stylesheets/Pens

Any URL's 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 it's URL and the proper URL extention.

+ 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

Save Automatically?

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 id="one" class="description panel blue">
    <div><h1>Navigation links with smooth scrolling</h1>
      <p>ScrollTrigger works great with navigation links within the page! Try clicking one of the links above and see how ScrollTrigger stays perfectly synced.</p>
      <div class="scroll-down">Scroll down<div class="arrow"></div></div>
    </div>
  </div>


  <section id="two" class="panel red">
    <p><span class="line line-1"></span>This line's animation will begin when it enters the viewport and finish when its top edge hits the top of the viewport, staying perfectly in sync with the scrollbar because it has <code>scrub:&nbsp;true</code></p>
  </section>


  <section id="three" class="panel orange">
    <p><span class="line line-2"></span>This orange panel gets pinned when its top edge hits the top of the viewport, then the line's animation is linked with the scroll position until it has traveled 100% of the viewport's height (<code>end: "+=100%"</code>), then the orange panel is unpinned and normal scrolling resumes. Padding is added automatically to push the rest of the content down so that it catches up with the scroll when it unpins. You can set <code>pinSpacing: false</code> to prevent that if you prefer.</p>
  </section>


  <section id="four" class="panel purple">
    <p><span class="line line-3"></span>This panel gets pinned in a similar way, and has a more involved animation that's wrapped in a timeline, fading the background color and animating the transforms of the paragraph in addition to the line, all synced with the scroll position perfectly.</p>
  </section>



<section id="five" class="panel gray">
  DONE!
</section>





<nav>
  <div><a href="#one">Section one</a></div>
  <div><a href="#two">Section two</a></div>
  <div><a href="#three">Section three</a></div>
  <div><a href="#four">Section four</a></div>
  <div><a href="#five">Section five</a></div>
</nav>

<header>
   <a href="https://greensock.com/scrolltrigger">
     <img class="greensock-icon" src="https://s3-us-west-2.amazonaws.com/s.cdpn.io/16327/scroll-trigger-logo-light.svg" width="200" height="64" />
  </a> 
</header>
              
            
!

CSS

              
                .line {
  width: 100%;
  max-width: 800px;
  height: 8px;
  margin: 0 0 10px 0;
  position: relative;
  display: inline-block;
  background-color: rgba(255,255,255,1);
}


/* Not great browser support :(
html {
  scroll-behavior: smooth;
} 
*/
nav {
  position: fixed;
  top: 10px;
  right: 10px;
  background: black;
  padding: 0px 10px;
}
nav a {
  color: white;
  font-size: 1.3em;
  line-height: 1.7;
  text-decoration: none;
}
nav a:hover {
  text-decoration: underline;
}

              
            
!

JS

              
                gsap.registerPlugin(ScrollTrigger, ScrollToPlugin);

// --- RED PANEL ---
gsap.from(".line-1", {
  scrollTrigger: {
    trigger: ".line-1",
    scrub: true,
    start: "top bottom",
    end: "top top"
  },
  scaleX: 0,
  transformOrigin: "left center", 
  ease: "none"
});


// --- ORANGE PANEL ---
gsap.from(".line-2", {
  scrollTrigger: {
    trigger: ".orange",
    scrub: true,
    pin: true,
    start: "top top",
    end: "+=100%"
  },
  scaleX: 0, 
  transformOrigin: "left center", 
  ease: "none"
});


// --- PURPLE/GREEN PANEL ---
var tl = gsap.timeline({
    scrollTrigger: {
      trigger: ".purple",
      scrub: true,
      pin: true,
      start: "top top",
      end: "+=100%"
    }
  });

tl.from(".purple p", {scale: 0.3, rotation:45, autoAlpha: 0, ease: "power2"})
  .from(".line-3", {scaleX: 0, transformOrigin: "left center", ease: "none"}, 0)
  .to(".purple", {backgroundColor: "#28a92b"}, 0);

gsap.utils.toArray("nav a").forEach(function(a) {
  a.addEventListener("click", function(e) {
    e.preventDefault();
    gsap.to(window, {duration: 1, scrollTo: e.target.getAttribute("href")});
  });
});






// possible solution for old iOS bugs that don't display things inside an iframe correctly. Create a --full-height CSS variable and use it instead of height: 100%
// function readHeight() {
// 		if (ScrollTrigger.isScrolling()) {
// 			console.log("wait until end...");
// 			ScrollTrigger.addEventListener("scrollEnd", readHeight);
// 		} else {
// 			ScrollTrigger.removeEventListener("scrollEnd", readHeight);
// 			window.removeEventListener("resize", readHeight);
// 			let scrollFunc = ScrollTrigger.getScrollFunc(window),
// 				scrollProgress = scrollFunc() / ScrollTrigger.maxScroll(window),
// 				docStyle = document.documentElement.style,
// 				bodyStyle = document.body.style;
// 			bodyStyle.overflow = "auto";
// 			docStyle.setProperty("--full-height", "100%");
// 			docStyle.setProperty("--full-height", window.innerHeight + "px");
// 			bodyStyle.overflow = "unset";
// 			setTimeout(function() {
// 				window.addEventListener("resize", readHeight);
// 			}, 500);
// 			ScrollTrigger.refresh(true);
// 			scrollFunc(scrollProgress * ScrollTrigger.maxScroll(window));
// 		}
// 	}
              
            
!
999px

Console