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

              
                <header class="header">
  <a href="" class="header__link">
    <h1 class="header__title">Stickybits</h1>
    <p>Check out Stickbits on Github</p>
  </a>
</header>
<main class="main">
  <div class="parent">
    <div class="child">
      <p>Child 1</p>
    </div>
  </div>
  <div class="parent">
    <div class="child">
      <p>Child 1</p>
    </div>
  </div>
  <div class="parent">
    <div class="child">
      <p>Child 1</p>
    </div>
  </div>
</main>
              
            
!

CSS

              
                body,
html {
  color: white;
  font-family: sans-serif;
  margin: 0;
  padding: 0;
}
header {
  text-align: center;
  h1 {
    margin-bottom: 0;
  }
}
a {
  color: black;
  text-decoration: none;
  &:hover {
   text-decoration: underline;
  }
}
main {
  counter-reset: div;
  display: flex;
  height: 100%;
  justify-content: space-between;
  max-width: 100vw;
  min-height: 2000px;
  position: absolute;
  top: 100px;
  width: 100%;
}
.child {
  padding: 1rem 0;
  text-indent: 1rem;
  width: 100%;
}
.parent {
  height: 100%;
  position: relative;
  width: 100%;
}
.parent:nth-child(odd) {
  background: tan;
}
.parent:nth-child(odd) .child {
  background-color: red;
}
.parent:nth-child(even) {
  background: aqua;
}
.parent:nth-child(even) .child {
  background-color: green;
}
.parent:before {
  counter-increment: div;
  content: 'Parent 'counter(div);
  left: 1rem;
  position: absolute;
  top: 1rem;
  z-index: 2;
}
.child.js-is-sticky {
  top: 0;
}
.child.js-is-stuck {
  bottom: 0;
}
              
            
!

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';

/*
  STICKYBITS 💉
  --------
  a lightweight alternative to `position: sticky` polyfills 🍬
*/
function Stickybit(target, o) {
  if (typeof window === 'undefined') throw Error('stickybits requires `window`');
  if (this.isOff) return this;
  /*
    defaults 🔌
    --------
    - target = el (DOM element)
    - offset = 0 || 'dealer's choice'
    - verticalPosition = top || bottom
    - useStickyClasses =
    - elStyles = CSS Styles
    - positionStickyVal = fixed || sticky
  */
  this.el = target;
  this.offset = o && o.stickyBitStickyOffset || 0;
  this.vp = o && o.verticalPosition || 'top';
  this.useClasses = o && o.useStickyClasses || false;
  this.styles = this.el.style;
  this.positionStickyVal = 'fixed';
  this.setStickyPosition();
  if (this.positionStickyVal === 'fixed' || this.useClasses === true) {
    this.manageStickiness();
  }
  return this;
}

/*
  setStickyPosition ✔️
  --------
  — most basic thing stickybits does
  => checks to see if position sticky is supported
  => stickybits works accordingly
*/
Stickybit.prototype.setStickyPosition = function setStickyPosition() {
  var browserPrefix = ['', '-o-', '-webkit-', '-moz-', '-ms-'];
  var styles = this.styles;
  var vp = this.vp;
  for (var i = 0; i < browserPrefix.length; i += 1) {
    styles.position = browserPrefix[i] + 'sticky';
  }
  if (styles.position !== '') {
    this.positionStickyVal = styles.position;
    if (vp === 'top') {
      styles[vp] = this.offset + 'px';
    }
  }
  return this;
};

/*
  manageStickiness ✔️
  --------
  — manages stickybit state
  => checks to see if the element is sticky || stuck
  => based on window scroll
*/
Stickybit.prototype.manageStickiness = function manageStickiness() {
  var parent = this.el.parentNode;
  var pv = this.positionStickyVal;
  var vp = this.vp;
  var offset = this.offset;
  var styles = this.styles;
  var classes = this.el.classList;
  var win = window;
  parent.classList.add('js-stickybit-parent');
  var stickyBitStart = this.el.getBoundingClientRect().top;
  var stickyBitStop = stickyBitStart + parent.offsetHeight - (this.el.offsetHeight - offset);
  var stickyClass = 'js-is-sticky';
  var stuckClass = 'js-is-stuck';

  // manage stickiness
  function stickiness() {
    var scroll = win.scrollY || win.scrollTop;
    var hasStickyClass = classes.contains(stickyClass);
    var hasStuckClass = classes.contains(stuckClass);
    if (scroll < stickyBitStart) {
      if (hasStickyClass) {
        classes.remove(stickyClass);
        if (pv === 'fixed') styles.position = '';
      }
    } else if (scroll > stickyBitStart && scroll < stickyBitStop) {
      if (!hasStickyClass) classes.add(stickyClass);
      if (hasStuckClass) {
        classes.remove(stuckClass);
        styles.bottom = '';
      }
      styles.position = pv;
      styles[vp] = offset + 'px';
    } else if (scroll > stickyBitStop && !hasStuckClass) {
      classes.remove(stickyClass);
      classes.add(stuckClass);
      if (pv !== 'fixed') return;
      styles.top = '';
      styles.bottom = '0';
      styles.position = 'absolute';
    }
  }

  var invoked = void 0;

  this.checkStickiness = function checkStickiness() {
    if (invoked) return;
    invoked = true;
    win.requestAnimationFrame(stickiness);
    win.setTimeout(function () {
      invoked = false;
    }, 0);
  };

  win.addEventListener('scroll', this.checkStickiness);
  return this;
};

/*
  cleanup 🛁
  --------
  - target = el (DOM element)
  - scrolltarget = window || 'dealer's chose'
  - scroll = removes scroll event listener
*/
Stickybit.prototype.cleanup = function cleanup() {
  var el = this.el;
  var styles = this.styles;
  // cleanup styles
  styles.position = '';
  styles[this.vp] = '';
  // cleanup CSS classes
  el.classList.remove('js-is-sticky', 'js-is-stuck');
  el.parentNode.classList.remove('js-stickybit-parent');
  // remove scroll event listener
  window.removeEventListener('scroll', this.checkStickiness);
  // turn of sticky invocation
  this.checkStickiness = false;
};

function MultiBits(userInstances) {
  var _this = this;

  this.privateInstances = userInstances || [];
  this.cleanup = function () {
    return _this.privateInstances.forEach(function (instance) {
      return instance.cleanup();
    });
  };
}

function stickybits(target, o) {
  var els = typeof target === 'string' ? document.querySelectorAll(target) : target;
  if (!('length' in els)) els = [els];
  var instances = [];
  for (var i = 0; i < els.length; i += 1) {
    var el = els[i];
    instances.push(new Stickybit(el, o));
  }
  return new MultiBits(instances);
}

return stickybits;

})));




stickybits('.child', {verticalPosition: 'bottom'});

// setup bottom sticky
var browserWindowHeight = window.innerHeight;
var children = document.querySelectorAll('.child');
for (var i = 0; i < children.length; i += 1) {
  var child = children[i];
  var childHeight = child.offsetHeight;
  var childPosition = browserWindowHeight - childHeight;
  console.log(browserWindowHeight, child, childHeight, childPosition);
  child.style.top = childPosition + 'px';
}

              
            
!
999px

Console