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. If you link to another Pen, it will include the CSS from that Pen. If the preprocessor matches, it will attempt to combine them before processing.

+ 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

              
                h1.headline.headline--title CSS property <code>Position: Sticky</code> doesn't support changes based on invocation 
p.headline.headline--description
  | <code>position: sticky</code> is a great css property—without any JavaScript, elements can become sticky relative to their parent! However, there is no way to make CSS changes to selected elements base on when they become sticky. <a href="https://dollarshaveclub.github.io/stickybits/">StickyBits 🔥</a> with the option <code>useStickyClasses</code> allows selected elements to be maniplated with CSS when they're sticky. Scroll below for an example (NOTE: This demo assumes <code>position: sticky</code> is supported by the browser).
.parent.parent--happy
  #happy-note.child.note.note--happy 
    .note__content.note__content--emoji 😊
    .note__content.note__content--text This emoji is happy because because it becames sticky with 2 css rules.
    
.parent.parent--sad
  #sad-note.child.note.note--sad 
    .note__content.note__content--emoji 😔
    .note__content.note__content--text This emoji is sad because it does not change when it becomes sticky or stuck.
    
.parent.parent--pumped
  #pumped-note.child.note.note--pumped
    .note__content.note__content--emoji
      span.neutral 😐
      span.pumped 😃
    .note__content.note__content--text
      spon.neutral This emoji will be happier with Stickybits so it can know when it is sticky.
      span.pumped This emoji is happy because it is knows when it is sticky!
    
p.headline.headline--description Use <a href="https://dollarshaveclub.github.io/stickybits/">StickyBits 🔥</a> with the option <code>useStickyClasses</code> to make changes to your sticky element when it becomes sticky like in the example above.

  

              
            
!

CSS

              
                body {
  min-height: 160rem;
  background-color: #fefbf7;
}
code {
  background-color: #eee;
}
.headline {
  margin: 1rem auto 0;
  max-width: calc(100vw - 10%);
  &--title {
    text-align: center;
  }
  &--description {
    font-size: 1.25rem;
    line-height: 1.8;
  }
}
.parent {
  height: 30rem;
  margin-top: 1rem;
  width: 100%;
  &--sad {
    background-color: #E8F1D4;
  }
  &--happy {
    background-color: #ffccd4;
  }
  &--pumped {
    background-color: #eaf2f5;
  }
}
.child {
  padding: 0 calc(100vw - 95%);
  position: sticky;
  top: 0;
  z-index: 3;
}
.note {
  background-color: rgba(0, 0, 0, .2);
  height: 8rem;
  max-width: 100%;
  z-index: 4;
  &__content {
    position: absolute;
    &--emoji {
      font-size: 7rem;
      top: .5rem;
    }
    &--text {
      font-size: 1.25rem;
      margin-left: 8rem;
      top: 50%;
      transform: translateY(-50%);
    }
  }
}
.neutral {
  display: inherit;
  .js-is-sticky & {
    display: none;
  }
}
.pumped {
  display: none;
  .js-is-sticky & {
    display: inherit;
  }
}
              
            
!

JS

              
                (function (global, factory) {
  typeof exports === 'object' && typeof module !== 'undefined' ? module.exports = factory() :
  typeof define === 'function' && define.amd ? define(factory) :
  (global.stickybits = factory());
}(this, (function () { 'use strict';

var browserPrefix = ['', '-o-', '-webkit-', '-moz-', '-ms-'];
var stickyBitClass = 'js-is-sticky';
var stickyBitIsStuckClass = 'js-is-stuck';

function Stickybit(target, o) {
  this.el = target;
  this.scrollTarget = o && o.scrollTarget || window;
  this.stickyBitStickyOffset = o && o.stickyBitStickyOffset || 0;
  this.verticalPosition = o && o.verticalPosition || 'top';
  this.useStickyClasses = o && o.useStickyClasses || false;
  this.elStyle = this.el.style;
  this.positionStickyVal = 'fixed';
}

Stickybit.prototype.setStickyPosition = function setStickyPosition() {
  var elStyle = this.elStyle;
  var verticalPosition = this.verticalPosition;
  for (var i = 0; i < browserPrefix.length; i += 1) {
    elStyle.position = browserPrefix[i] + 'sticky';
  }
  if (elStyle.position !== '') {
    this.positionStickyVal = elStyle.position;
    if (verticalPosition === 'top') {
      elStyle[verticalPosition] = this.stickyBitStickyOffset + 'px';
    }
  }
};

Stickybit.prototype.manageStickiness = function manageStickiness() {
  var el = this.el;
  var scrollTarget = this.scrollTarget;
  var positionStickyVal = this.positionStickyVal;
  var verticalPosition = this.verticalPosition;
  var stickyBitStickyOffset = this.stickyBitStickyOffset;
  var elStyle = this.elStyle;
  var elClasses = el.classList;
  var elParent = el.parentNode;
  var stickyBitStart = el.getBoundingClientRect().top;
  var stickyBitStop = stickyBitStart + elParent.offsetHeight - (el.offsetHeight - stickyBitStickyOffset);
  elParent.classList.add('js-stickybit-parent');
  function stickiness() {
    var scroll = scrollTarget.scrollY;
    if (scroll < stickyBitStart) {
      if (elClasses.contains(stickyBitClass)) {
        elClasses.remove(stickyBitClass);
        if (positionStickyVal === 'fixed') elStyle.position = '';
      }
    } else if (scroll > stickyBitStart && scroll < stickyBitStop) {
      if (!elClasses.contains(stickyBitClass)) elClasses.add(stickyBitClass);
      if (elClasses.contains(stickyBitIsStuckClass)) {
        elClasses.remove(stickyBitIsStuckClass);
        elStyle.bottom = '';
      }
      elStyle.position = positionStickyVal;
      elStyle[verticalPosition] = stickyBitStickyOffset + 'px';
    } else if (scroll > stickyBitStop && !elClasses.contains(stickyBitIsStuckClass)) {
      elClasses.remove(stickyBitClass);
      elClasses.add(stickyBitIsStuckClass);
      if (positionStickyVal !== 'fixed') return;
      elStyle.top = '';
      elStyle.bottom = '0';
      elStyle.position = 'absolute';
    }
  }
  var invoked = void 0;
  function checkStickiness() {
    if (invoked) return;
    invoked = true;
    stickiness();
    window.setTimeout(function () {
      invoked = false;
    }, 0);
  }
  scrollTarget.addEventListener('scroll', function () {
    return scrollTarget.requestAnimationFrame(checkStickiness);
  });
};

function stickybits(target, o) {
  var els = typeof target === 'string' ? document.querySelectorAll(target) : target;
  if (!('length' in els)) els = [els];
  var stickyBit = void 0;
  for (var i = 0; i < els.length; i += 1) {
    var el = els[i];
    stickyBit = new Stickybit(el, o);
    stickyBit.setStickyPosition();
    if (stickyBit.positionStickyVal === 'fixed' || stickyBit.useStickyClasses === true) {
      stickyBit.manageStickiness();
    }
  }
}

return stickybits;

})));


stickybits('#pumped-note', {useStickyClasses: true});
              
            
!
999px

Console