Stickybits is a lightweight (<2KB) alternative to position: sticky polyfills that works perfectly for things like sticky headers.


Stickybits is awesome because:

  • it can add a CSS Sticky Class (.js-is-sticky) when position: sticky elements become active and a CSS Stuck Class (.js-is-stuck) when they become stuck. See useStickyClasses
  • it loosely mimics position: sticky to consistently stick elements vertically across multiple platforms
  • does not have the jumpiness that plugins that are built around position: fixed have because it tries to support position: sticky first
  • it is super simple & lightweight (2kb minified)

Install

yarn

yarn add stickybits --dev

npm

npm install stickybits --save-dev

bower

bower install stickybits --save

Setup

Default

Add dist/stickybits.min.js. You have access to the API options below and must invoke scrollDir. See the Default Setup Usage and Options below.

jQuery or Zepto

Add dist/jquery.stickybits.min.js

Default Usage

Basic Setup

stickybits('selector');

By default a selected stickybits element will:

  • Stick elements to the top of the viewport when scrolled to vertically.
  • Stick elements at the bottom of their parent when scrolled past.

useStickyClasses Feature

Stickybits allows costumers to add CSS to elements when they become sticky and when they become stuck at the bottom of their parent element.

By default, if position: sticky is supported, StickyBits will exit allowing the browser to manage stickiness and avoid adding a scroll event listener.

If the useStickyClasses argument is set to true then even if a browser supports position: sticky, StickyBits will still add a scroll event listener to add and remove sticky CSS Classes. This option is available so that CSS styles can use when StickyBits elements become sticky or stuck at the bottom of their parent.

To use useStickyClasses:

stickybits('selector', { useStickyClasses: true });

Then, in css you can do:

.some-sticky-element .js-is-sticky { background-color: red; }
.some-sticky-elemen .js-is-stuck { background-color: green; }

View add css classes for more information on StickyBits CSS Classes

.

Options

Vertical Layout Position

By default, a StickyBits element will stick to the top of the viewport when vertically scrolled to. Stickybits loosely works for bottom positioning as well.

To have a StickyBits element stick to the bottom:

stickybits('selector', { verticalPosition: 'bottom' });

StickyBit Sticky Offset

By default, a StickyBits element will have a 0px sticky layout top offset. This means that the element will stick flush to the top of the viewport.

To have a stickyBit stick with a 20px offset to its vertical layout position:

stickybits('selector', { stickyBitStickyOffset: 20 });

Examples

Notes

3 CSS classes will be added or removed by stickybits unrelated to the basic usage.

  • js-is-sticky if the selected element is sticky.
  • js-is-stuck if the selected element is stopped at the bottom of its parent.
  • js-stickybit-parent so that styles can easily be added to the parent of a Stickbit

Not a Polyfill

We strayed away from calling Stickybits a Shim or Polyfill for position: sticky because full support would require more code. This plugin simply makes elements vertically sticky very similarly to position: sticky. Read more about position sticky here or follow its browser implementation here.

jQuery and Zepto Usage

Basic

$('selector').stickybits();

With useStickyClasses

$('selector').stickybits({useStickyClasses: true});

With customVerticalPosition

$('selector').stickybits({customVerticalPosition: true});

With stickyBitStickyOffset

$('selector').stickybits({stickyBitStickyOffset: 20});

Thanks

This plugin was heavily influenced by Filament Group's awesome Fixed-sticky jQuery plugin. Thanks to them for the ideas.