Pen Settings



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


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


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.


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.


                <div id="viewport">
  <div id="content"> 
    <div class="box box-a">a</div>
    <div class="box box-b">b</div>
    <div class="box box-c">c</div> 



                body {


#viewport {
  /*overflow: hidden;*/
  /*position: fixed;*/
  height: 100%;
  width: 100%;
  top: 0;
  left: 0;
  right: 0;
  bottom: 0;

#content {
  overflow: visible;
  width: 100%;
  /* set a height because the contents are position: absolute, thus natively there's no height */
  height: 100vh;
  /* for horizontal */
    display: flex;
  flex-wrap: nowrap;

.box {
  width: 100px;
  height: 100px;
  background-color: #28a92b;
  position: absolute;
  left: calc(50% - 50px);
  z-index: 100;
  line-height: 100px;
  font-size: 50px;
  text-align: center;
} {
  background-color: red;

.box-a {
  left: 2000px;
  background-color: #8d3dae;

.box-b {
  left: 6000px;

.box-c {
  left: 1000px;
  background-color: #e26c16;




// you must wrap your content <div> in another <div> that will serve as the viewport

let snapSections =  gsap.utils.toArray('.box'),
ScrollTrigger.create( {
  trigger: snapSections[0], // first section
  scroller: '#content',
  start: 'top bottom',
  endTrigger: snapSections[snapSections.length - 1], // last section
  end: 'bottom top',
  onRefresh: self => {    
    // translate the offsetTop of each section into a progress value between the ScrollTrigger's start and end for snapping
    let values = => gsap.utils.normalize(self.start, self.end, section.offsetLeft - window.innerWidth / 2 + section.offsetWidth / 2))
    values.push(1) // make sure it can snap to the end of the last section.
    console.log("values", values);
    snapper =  gsap.utils.snap(values) // create a function that'll do the snapping for us. Just pass in a value and it'll return the closest one in the Array.
  snap: value => snapper(value),


// this is the helper function that sets it all up. Pass in the content <div> and then the wrapping viewport <div> (can be the elements or selector text). It also sets the default "scroller" to the content so you don't have to do that on all your ScrollTriggers.
function smoothScroll(content, viewport, smoothness) {
	content = gsap.utils.toArray(content)[0];
	smoothness = smoothness || 1;

	gsap.set(viewport || content.parentNode, {overflow: "hidden", position: "fixed", height: "100%", width: "100%", top: 0, left: 0, right: 0, bottom: 0});
	gsap.set(content, {overflow: "visible", height: "100%"});

	let getProp = gsap.getProperty(content),
		setProp = gsap.quickSetter(content, "x", "px"),
		setScroll = ScrollTrigger.getScrollFunc(window),
		removeScroll = () => = "visible",
		killScrub = trigger => {
			let scrub = trigger.getTween ? trigger.getTween() : gsap.getTweensOf(trigger.animation)[0]; // getTween() was added in 3.6.2
			scrub && scrub.kill();
		width, isProxyScrolling;

	function onResize() {
		width = content.clientWidth; = "visible" = width + "px";
	ScrollTrigger.addEventListener("refreshInit", onResize);
	ScrollTrigger.addEventListener("refresh", () => {
	ScrollTrigger.defaults({scroller: content});
	ScrollTrigger.prototype.update = p => p; // works around an issue in ScrollTrigger 3.6.1 and earlier (fixed in 3.6.2, so this line could be deleted if you're using 3.6.2 or later)

	ScrollTrigger.scrollerProxy(content, {
		scrollTop(value) {
			if (arguments.length) {
				isProxyScrolling = true; // otherwise, if snapping was applied (or anything that attempted to SET the scroll proxy's scroll position), we'd set the scroll here which would then (on the next tick) update the content tween/ScrollTrigger which would try to smoothly animate to that new value, thus the scrub tween would impede the progress. So we use this flag to respond accordingly in the ScrollTrigger's onUpdate and effectively force the scrub to its end immediately.
			return -getProp("x");
		getBoundingClientRect() {
			return {top: 0, left: 0, width: window.innerWidth, height: window.innerHeight};

	return ScrollTrigger.create({
		animation: gsap.fromTo(content, {x:0}, {
			x: () => document.documentElement.clientWidth - width,
			ease: "none",
			onUpdate: ScrollTrigger.update
		scroller: window,
		invalidateOnRefresh: true,
		start: 0,
		end: () => width - document.documentElement.clientWidth,
		scrub: smoothness,
		onUpdate: self => {
			if (isProxyScrolling) {
				isProxyScrolling = false;
		onRefresh: killScrub // when the screen resizes, we just want the animation to immediately go to the appropriate spot rather than animating there, so basically kill the scrub.