Giter VIP home page Giter VIP logo

stickybits's Introduction

StickyBits banner

Make things get sticky …in a good way


Build Status npm version bower version Greenkeeper codecov Share on Twitter


StickyBits 🍬

Stickybits is a lightweight alternative to position: sticky polyfills. It 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
  • it does not have the jumpiness that plugins that are built around position: fixed have because it tries to support position: sticky first.
  • in its simplest use case, a scroll event listener will not be used if position: sticky is supported.
  • it is super simple & lightweight
  • it provides a wiki that digs deeply into fundementals of position: sticky and position: fixed and it works with them.

Installation   Setup   Usage   Feature   Options   Examples   Debugging   Notes   Contributing   Wiki


Installing from a package manager

yarn

yarn add stickybits 

npm

npm i stickybits

bower

bower i stickybits --save

Setup

Add dist/stickybits.min.js

Or as a module with import stickybits from 'stickybits'

Basic Usage

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 element when scrolled past.

useStickyClasses Feature

Stickybits allows customers 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'});

Custom Scroll Element

By default, if Stickybits uses a scroll event (if position: sticky is not supported or if we use the option useStickyClasses) it uses window.

To have Stickybit use an selector besides window:

const scrollEl = document.getElementById('an-id');
stickybits('selector', {scrollEl: scrollEl});

* Note: This selector is not selected automatically so the specific element need to be passed in.

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 StickyBits element stick with a 20px offset to its vertical layout position:

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

StickyBits Cleanup

To cleanup an instance of Stickybits:

const stickybitsInstancetoBeCleanedup = stickbits('selector');
stickybitsInstancetoBeCleanedup.cleanup();

StickyBits NoStyles

To use StickyBits without inline styles except for position: sticky or position: fixed:

stickbits('selector', {noStyles: true});

StickyBits Custom CSS Classes

To use custom CSS classes for Stickybits, add the appropriate properties and values.

parentClass:

stickbits('selector', {parentClass: 'new-parent-classname'});

stickyClass:

stickbits('selector', {stickyClass: 'new-sticky-classname'});

stuckClass:

stickbits('selector', {stuckClass: 'new-stuck-classname'});

To change all of the CSS classes

stickbits('selector', {
  parentClass: 'new-parent-classname',
  stickyClass: 'new-sticky-classname',
  stuckClass: 'new-stuck-classname'
});

* For jQuery and Zepto support, read the jQuery notes below.

Examples


Examples extended


Have another example or question? Feel free to comment. 🙌

Notes

CSS Class Usage

3 CSS classes will be added and removed by Stickybits if position: sticky is not supported or if the useStickyClasses: true option is added to the plugin call. These Classes can be modified as desired. See the With Custom Classes example above.

  • 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 Stickybits element

Not a Polyfill

Stickybits is not a Shim or Polyfill for position: sticky because full support would require more code. This plugin makes elements vertically sticky very similarly to position: fixed but in a sticky sort of way. Read more about position sticky here or follow its browser implementation here.

Stickybits is a no dependency JavaScript plugin. It provides the smallest API possible in both features and kb size to deliver working sticky elements. This means that opinionated featuring is left out as much as possible and that it works with minimal effort in Frameworks.

CSS when position: sticky is not supported

Sticky Start and Sticky Stop: Because Stickybits is minimal, when position: sticky is not supported Stickybits will use position: fixed which is relative to the browser window. If the StickyBits parent element has a height recognized by the browser, Stickybits will take care of the sticky top and sticky bottom invocation. If the parent's height is not recognized by the browser there will be issues.

Left and Right Positioning: With position: fixed the Stickybit element will work relative to the browser window by default. To work with this issue, there are several options. Some are noted here. More solutions to come!

jQuery and Zepto Usage

Basic

$('selector').stickybits();

With scrollEl

const $scrollEl = $('#scrollEl')[0];
$('selector').stickybits({scrollEl: scrollEl});

* Note: because StickyBits is not jQuery specific, in order to pass in a selector for scrollEl, add a [0] to the the selector.

With useStickyClasses

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

With customVerticalPosition

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

With stickyBitStickyOffset

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

Debugging

Stickybits 2.0 provides the same API but with more debugging feedback.

To view the Stickybits API in it's simpliest form:

var stickybit = stickybits('a selection')
console.log(stickbit)

For more debugging and managing Stickbits, view the wiki.


Utility properties

Stickybits provides both version and userAgent properties which were added to offer insight into the browser and Stickybits.

These utility properties can be accessed as direct child properties of the instantiated Stickybits item.

var stickybit = stickybits('a selection')
stickybit.version // will show the version of stickybits being used
stickybit.userAgent // will show which userAgent stickybits is detecting

Browser Compatibility

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

Contributing

Please contribute to Stickybits by filing an issue, responding to issues, adding to the wiki, or reaching out socially—etc.

Stickybits is a utility. It may often not be needed! With shared understanding of position: sticky and position: fixed along with product awareness, Stickybits can improve as can a shared understanding of the "sticky element issue". Is this paragraph over-reaching? Yes! Help improve it.

Thanks

This plugin was heavily influenced by Filament Group's awesome Fixed-sticky jQuery plugin. Thanks to them for getting my mind going on this a while back. Thanks to Peloton Cycle's Frame Throttle for an insightful solve for optimizing frame throttling.

Contributions: Much browser and documentation debugging has be done by K. Vanberendonck. Architecture discussions and Pull Request help has been provided by Jacob Kelley, Brian Gonzalez, and Matt Young. It is much appreciated!


Created and maintained by Jeff Wainwright with Dollar Shave Club Engineering.

stickybits's People

Contributors

anilanar avatar artjomsimon avatar awcross avatar bkeroackdsc avatar briangonzalez avatar browner12 avatar eddiesigner avatar greenkeeper[bot] avatar jnachtigall avatar nextgenthemes avatar yowainwright avatar

Watchers

 avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.