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: Fixed</code> is not good for sticky elements on mobile devices
p.headline.headline--description
  | <code>position: fixed</code> is a great css property when making elements stick to the document if they don't change! However, if they change state when customers scroll, elements will jitter and other weirdness is visible. <a href="https://dollarshaveclub.github.io/stickybits/">StickyBits 🔥</a> fixes this by using <code>position: sticky</code> when <code>position: fixed</code> won't work so well. Scroll below for a mobile device example.
  
#fixed-parent.parent.parent--fixed
  #fixed-child.child.child--fixed 
    .child__content.child__content--emoji 😶
    .child__content.child__content--text Sticky State using: <code>position: fixed</code> 
#sticky-parent.parent.parent--sticky
   #sticky-child.child.child--sticky 
     .child__content.child__content--emoji 😃
     .child__content.child__content--text Sticky State using <code>position: sticky</code> 
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%;
  &--sticky {
    background-color: #E8F1D4;
  }
  &--fixed {
    position: relative;
    background-color: #ffccd4;
  }
}
.child {
  background-color: rgba(0, 0, 0, .2);
  height: 8rem;
  width: 100%;
  top: 0;
  z-index: 3;
  &--fixed {
    position: relative;
    &.js-is-fixed {
      position: fixed;
      top: 0;
    }
    &.js-is-fixed-stuck {
      bottom: 0;
      position: absolute;
      top: auto;
    }
  }
  &__content {
    font-size: 1.25rem;
    max-width: 100%;
    padding: 0 calc(100vw - 95%);
    position: absolute;
    &--emoji {
      font-size: 7rem;
      top: .5rem;
    }
    &--text {
      font-size: 1.25rem;
      margin-left: 8rem;
      top: 50%;
      transform: translateY(-50%);
    }
  }
}

              
            
!

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;

})));

var $fixedEl = $('#fixed-child');
var $fixedParent = $('#fixed-parent');
var stickyCoords = {};
function getStickyCoords() {
  stickyCoords.start = $fixedParent.offset().top;
  var parentHeight = $fixedParent.outerHeight();
  var fixedElHeight = $fixedEl.outerHeight();
  var distance = parentHeight - fixedElHeight;
  stickyCoords.stop =  stickyCoords.start + distance;
  return stickyCoords; 
};
$(window).on('scroll', function() {
  var scroll = window.scrollY;
  getStickyCoords();
  if (scroll < stickyCoords.start) {
    $fixedEl.removeClass('js-is-fixed');
    return;
  } else if (
    (scroll > stickyCoords.start) && 
    (scroll < stickyCoords.stop)
  ) {
   $fixedEl.addClass('js-is-fixed');
   $fixedEl.removeClass('js-is-fixed-stuck');
    return;
  } else {
   $fixedEl.addClass('js-is-fixed-stuck');
   $fixedEl.removeClass('js-is-fixed');
  }
});

stickybits('#sticky-child');
              
            
!
999px

Console