Huge News!Announcing our $40M Series B led by Abstract Ventures.Learn More
Socket
Sign inDemoInstall
Socket

stickybits

Package Overview
Dependencies
Maintainers
1
Versions
85
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

stickybits

Stickybits is a lightweight (<2KB) alternative to `position: sticky` polyfills

  • 1.0.1
  • Source
  • npm
  • Socket score

Version published
Maintainers
1
Created
Source

StickyBits banner

Make things get sticky …in a good way


Build Status npm version bower version Share on Twitter


StickyBits 🍬

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.
  • in its simpliest usecase, a scroll event listener will not be used if position: sticky is supported.
  • it is super simple & lightweight (2kb minified)

Installation   Setup   Usage   Feature   Options   Examples   Notes


Installing from a package manager

yarn

yarn add stickybits

npm

npm install stickybits --save

bower

bower install stickybits --save

Setup

Add dist/stickybits.min.js.

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 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 for adding or removing CSS Classes. This option is available so that CSS styles can be added when StickyBits become sticky.

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

View add css classes for more information how to style based on StickyBit CSS Classes.

Options

Vertical Layout Position

By default, a StickyBit element will stick to the top of the viewport when vertically scrolled to.

To have a StickyBit not have the inline css property top style:

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

StickyBit Sticky Offset

By default, a StickyBit element will have a 0px sticky layout offset. This means that if the element will sticky to the top of the viewport by default.

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

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

For jQuery and Zepto support, read below.

Examples


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

Notes

CSS Class Usage

3 CSS classes will be added or removed by Stickybits if position: sticky is not supported or if the useStickyClasses: true option is added to the plugin call.

  • 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').sticybits();

With useStickyClasses

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

With customVerticalPosition

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

With stickyBitStickyOffset

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

Thanks

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

Keywords

FAQs

Package last updated on 24 Mar 2017

Did you know?

Socket

Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.

Install

Related posts

SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap
  • Changelog

Packages

npm

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc