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

              
                <header>
		<div class="container">
			<h1>Site Title</h1>
		</div>
	</header>

	<div class="container clearfix">
		<div id="sidebar">
			<div class="sidebar__inner">
				<p>This is sticky element that is smaller than the viewport.</p>
			</div>
		</div>
		<div id="content">
			<p>Lorem ipsum dolor sit amet, consectetur adipiscing elit. Cras tempus id leo et aliquam. Proin consectetur ligula vel neque cursus laoreet. Nullam dignissim, augue at consectetur pellentesque, metus ipsum interdum sapien, quis ornare quam enim vel ipsum.</p>

			<p>In congue nunc vitae magna tempor ultrices. Cras ultricies posuere elit. Nullam ultrices purus ante, at mattis leo placerat ac. Nunc faucibus ligula nec lorem sodales venenatis. Curabitur nec est condimentum, blandit tellus nec, semper arcu. Nullam in porta ipsum, non consectetur mi. Sed pharetra sapien nisl. Aliquam ac lectus sed elit vehicula scelerisque ut vel sem. Ut ut semper nisl.</p>

			<p>Curabitur rhoncus, arcu at placerat volutpat, felis elit sollicitudin ante, sed tempus justo nibh sed massa. Integer vestibulum non ante ornare eleifend. In vel mollis dolor.</p>
		</div>		
	</div>

	<footer>
		<p>Very Tall Footer</p>
	</footer>
              
            
!

CSS

              
                *,
		*:before,
		*:after{
			box-sizing: border-box;
		}
		
		header{
			color: #888;
		}

		header h1{
			font-size: 30px;
		}

		.container{
			max-width: 700px;
			margin: 0 auto;
		}

		#content{
			border: 2px dotted green;
			padding: 18px;
			margin-left: 215px;
			min-height: 300vh;
			color: darkgrey;
		}

		#sidebar{
			float: left;
			width: 200px;
			color: #ffbdbd;
			will-change: min-height;
		}

		#sidebar .sidebar__inner{
			border: 2px dotted red;
			padding: 10px;
      height: 150vh;

			position: relative;
			transform: translate(0, 0);
			transform: translate3d(0, 0, 0);
			will-change: position, transform;
		}

		footer{
			height: 300px;
			height: 85vh;
			border: 2px dotted black;
			margin-top: 20px;
			text-align: center;
			color: #8e8d8d;
			background: rgba(0, 0, 0, 0.02);
			line-height: 300px;
			line-height: 115vh;
		}

		footer p{
			margin: 0;
		}

		.clearfix:after{
			display: block;
			content: "";
			clear: both;
		}


body {
  background-color:#1d1d1d;
  font-family: "Signika Negative", Arial, sans-serif;
}
.pin-spacer {
/*   float: left; */
}



#sidebar{ top: 100vh; }
              
            
!

JS

              
                function stickyBothDirections(element, vars) {
  vars = vars || {};
  element = gsap.utils.toArray(element)[0];
  let keywords = {top: "0", center: "50%", bottom: "100%"},
      scrollFunc = ScrollTrigger.getScrollFunc(window),
      overlap, topOffset,
      updateOverlap = () => {
        topOffset = ((typeof(vars.start) === "function" ? vars.start() : vars.start || "0 0") + "").split(" ")[1] || "0";
        topOffset = keywords[topOffset] || topOffset;
        topOffset = ~topOffset.indexOf("%") ? parseFloat(topOffset) / 100 * window.innerHeight : parseFloat(topOffset) || 0;
        overlap = Math.max(0, element.offsetHeight - window.innerHeight + topOffset);
      },
      {onUpdate, onRefresh} = vars,
      offset = 0,
      lastY = 0,
      pinned,
      pin = (value, bottom) => {
        pinned = value;
        if (pinned) {
          let bounds = element.getBoundingClientRect();
          gsap.set(element, {
            position: "fixed",
            left: bounds.left,
            width: bounds.width,
            boxSizing: "border-box",
            y: 0,
            top: bottom ? topOffset - overlap : topOffset
          });
        } else {
          gsap.set(element, {
            position: "relative",
            clearProps: "left,top,width,boxSizing",
            y: offset
          });
        }
      },
      self;
  updateOverlap();
  vars.trigger = element;
  vars.start = "start" in vars ? vars.start : "top top";
  vars.onRefresh = self => {
    updateOverlap();
    self.vars.onUpdate(self);
    onRefresh && onRefresh(self);
  };
  vars.onRefreshInit = self => {
    self.progress = 0;
    self.isActive = false;
    vars.onUpdate(self);
  };
  vars.onUpdate = self => {
    let {progress, start, end, isActive} = self,
        y = progress * (end - start),
        delta = y - lastY,
        exceedsBottom = y + Math.max(0, delta) >= overlap + offset;
    if ((exceedsBottom || y + Math.min(0, delta) < offset) && isActive) {
      offset += exceedsBottom ? y - overlap - offset : y - offset;
      pinned || pin(true, exceedsBottom);
    // uncomment if you want to prioritize showing the TOP of the sidebar.
    // } else if (!exceedsBottom && y + offset < overlap && isActive) {
    //   offset += y - offset;
    //   pinned || pin(true, false);
    } else if (pinned || !isActive) {
      isActive || (offset = y ? self.end - self.start - overlap : 0);
      pin(false);
    }
    lastY = y;
    onUpdate && onUpdate(self);
  };
  self = ScrollTrigger.create(vars);
  return self;
}

stickyBothDirections("#sidebar", {
  start: "top top",
  endTrigger: "#content",
  end: self => "bottom bottom" + (window.innerHeight > self.trigger.offsetHeight ? "-=" + (window.innerHeight - self.trigger.offsetHeight) : ""),
  markers: true,
});

// NOTE: if you'd like to resize the sidebar, see this CodePen: https://codepen.io/GreenSock/pen/YzraKgQ?editors=0010


//stickyBothDirections("#sidebar", {
//  start: "top top",
//  endTrigger: "#content",
//  end: () => "bottom bottom-=" + ( window.innerHeight - document.querySelector('#sidebar').offsetHeight ),
//  markers: true
//});








// OLD - before adding an onRefreshInit() that handles resizing of the trigger: 
// function stickyBothDirections(element, vars) {
//   vars = vars || {};
//   element = gsap.utils.toArray(element)[0];
//   let keywords = {top: "0", center: "50%", bottom: "100%"},
//       overlap, topOffset,
//       updateOverlap = () => {
//         topOffset = ((typeof(vars.start) === "function" ? vars.start() : vars.start || "0 0") + "").split(" ")[1] || "0";
//         topOffset = keywords[topOffset] || topOffset;
//         topOffset = ~topOffset.indexOf("%") ? parseFloat(topOffset) / 100 * window.innerHeight : parseFloat(topOffset) || 0;
//         overlap = Math.max(0, element.offsetHeight - window.innerHeight + topOffset);
//       },
//       {onUpdate, onRefresh} = vars,
//       offset = 0,
//       lastY = 0,
//       pinned,
//       pin = (value, bottom) => {
//         pinned = value;
//         if (pinned) {
//           let bounds = element.getBoundingClientRect();
//           gsap.set(element, {
//             position: "fixed",
//             left: bounds.left,
//             width: bounds.width,
//             boxSizing: "border-box",
//             y: 0,
//             top: bottom ? topOffset - overlap : topOffset
//           });
//         } else {
//           gsap.set(element, {
//             position: "relative",
//             clearProps: "left,top,width,boxSizing",
//             y: offset
//           });
//         }
//       },
//       self;
//   updateOverlap();
//   vars.trigger = element;
//   vars.start = "start" in vars ? vars.start : "top top";
//   vars.onRefresh = self => {
//     updateOverlap();
//     self.vars.onUpdate(self);
//     onRefresh && onRefresh(self);
//   };
//   vars.onUpdate = self => {
//     let {progress, start, end, isActive} = self,
//         y = progress * (end - start),
//         delta = y - lastY,
//         exceedsBottom = y + Math.max(0, delta) >= overlap + offset;
//     if ((exceedsBottom || y + Math.min(0, delta) < offset) && isActive) {
//       offset += exceedsBottom ? y - overlap - offset : y - offset;
//       pinned || pin(true, exceedsBottom);
//     // uncomment if you want to prioritize showing the TOP of the sidebar.
//     // } else if (!exceedsBottom && y + offset < overlap && isActive) {
//     //   offset += y - offset;
//     //   pinned || pin(true, false);
//     } else if (pinned || !isActive) {
//       isActive || (offset = y ? self.end - self.start - overlap : 0);
//       pin(false);
//     }
//     lastY = y;
//     onUpdate && onUpdate(self);
//   };
//   self = ScrollTrigger.create(vars);
//   return self;
// }
              
            
!
999px

Console