Stickybits is a lightweight alternative to position: sticky polyfills (original) (raw)


Stickybits is a lightweight alternative to position: sticky polyfills that works perfectly for things like sticky headers. Checkout the Github repository for the most up to date documnetation.


Stickybits is awesome because:


Install

yarn

yarn add stickybits --dev

npm

npm install stickybits --save-dev

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

By default a selected stickybits element will:

Key Note: Stickybits expects and works best when the element that will become sticky is wrapped within a parent element that defines when the element starts being sticky and stops being sticky. See below for visual reference.


                                <main id="some-stickybit-parent"><nav id="some-stickybit-nav"></nav></main></code></pre>
                            

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-element .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.

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});

Browser Compatibility

Stickybits works in all modern browsers including Internet Explorer 9 and above. Please file and issue with browser compatibility quirks.

Thanks

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