Convert Figma logo to code with AI

wilddeer logostickyfill

Polyfill for CSS `position: sticky`

2,286
251
2,286
27

Top Related Projects

DEPRECATED: A position: sticky polyfill that works with filamentgroup/fixed-fixed for a safer position:fixed fallback.

A responsive image polyfill for <picture>, srcset, sizes, and more

3,304

jQuery Plugin for Sticky Objects

22,354

A light-weight, no-dependency, vanilla JavaScript engine to drive user's focus across the page

Quick Overview

Stickyfill is a polyfill for CSS position: sticky. It aims to provide a cross-browser solution for sticky positioning, allowing elements to switch between relative and fixed positioning as the user scrolls. This library is particularly useful for creating sticky headers, sidebars, or other elements that need to remain visible while scrolling.

Pros

  • Provides a fallback for browsers that don't support native position: sticky
  • Lightweight and easy to implement
  • Works with both top and bottom positioning
  • Supports both vertical and horizontal scrolling

Cons

  • May have performance issues with a large number of sticky elements
  • Doesn't support all features of native position: sticky (e.g., z-index stacking)
  • Requires JavaScript, which means it won't work if JavaScript is disabled
  • May not perfectly replicate native behavior in all scenarios

Code Examples

  1. Basic usage:
// Select all elements with the .sticky class
const elements = document.querySelectorAll('.sticky');

// Apply Stickyfill to the selected elements
Stickyfill.add(elements);
  1. Applying to a single element:
const stickyHeader = document.querySelector('#sticky-header');
Stickyfill.add(stickyHeader);
  1. Removing Stickyfill from elements:
// Remove Stickyfill from specific elements
Stickyfill.remove(elements);

// Or remove from all elements
Stickyfill.removeAll();

Getting Started

  1. Include the Stickyfill script in your HTML:
<script src="https://cdnjs.cloudflare.com/ajax/libs/stickyfill/2.1.0/stickyfill.min.js"></script>
  1. Apply the position: sticky style to your elements:
.sticky {
  position: -webkit-sticky;
  position: sticky;
  top: 0;
}
  1. Initialize Stickyfill in your JavaScript:
const stickyElements = document.querySelectorAll('.sticky');
Stickyfill.add(stickyElements);

That's it! Your elements should now have sticky behavior across different browsers.

Competitor Comparisons

DEPRECATED: A position: sticky polyfill that works with filamentgroup/fixed-fixed for a safer position:fixed fallback.

Pros of fixed-sticky

  • Lightweight and simple implementation
  • Supports both top and bottom positioning
  • Works with dynamically added content

Cons of fixed-sticky

  • Limited browser support (IE8+)
  • Requires additional CSS for proper styling
  • May have performance issues with complex layouts

Code Comparison

fixed-sticky:

$(function() {
  $(".sticky").fixedsticky();
});

Stickyfill:

var elements = document.querySelectorAll('.sticky');
Stickyfill.add(elements);

Key Differences

  • Implementation: fixed-sticky uses jQuery, while Stickyfill is vanilla JavaScript
  • Browser Support: Stickyfill has broader browser support, including older versions of IE
  • Flexibility: Stickyfill aims to polyfill the native position: sticky behavior, while fixed-sticky provides a custom implementation

Use Cases

  • fixed-sticky: Simpler projects with modern browser requirements
  • Stickyfill: Projects requiring broader browser support and closer adherence to native sticky behavior

Community and Maintenance

  • fixed-sticky: Maintained by Filament Group, with regular updates
  • Stickyfill: Community-driven project with less frequent updates

Both libraries aim to solve the sticky positioning problem, but they differ in their approach and target use cases. Choose based on your project's specific requirements and browser support needs.

A responsive image polyfill for <picture>, srcset, sizes, and more

Pros of Picturefill

  • Focuses on responsive images, addressing a specific web development need
  • Widely adopted and well-maintained, with regular updates
  • Extensive browser support, including older versions

Cons of Picturefill

  • Limited to image-related functionality
  • Larger file size due to broader feature set
  • May require additional configuration for optimal performance

Code Comparison

Picturefill usage:

<picture>
  <source srcset="large.jpg" media="(min-width: 800px)">
  <img src="small.jpg" alt="Image description">
</picture>

Stickyfill usage:

var elements = document.querySelectorAll('.sticky');
Stickyfill.add(elements);

While both libraries aim to enhance web functionality, they serve different purposes. Picturefill focuses on responsive images, providing a robust solution for handling various image scenarios across devices. Stickyfill, on the other hand, addresses the sticky positioning issue in CSS, offering a more targeted solution for a specific layout problem.

Picturefill's broader scope and extensive browser support make it a valuable tool for image-heavy websites, but it may be overkill for projects that don't require advanced image handling. Stickyfill's simplicity and focused approach make it an excellent choice for developers specifically looking to implement sticky positioning with cross-browser compatibility.

3,304

jQuery Plugin for Sticky Objects

Pros of Sticky

  • Lightweight and simple implementation
  • Easy to use with minimal setup required
  • Supports both jQuery and vanilla JavaScript versions

Cons of Sticky

  • Less feature-rich compared to Stickyfill
  • May not handle complex scenarios as effectively
  • Limited browser compatibility information

Code Comparison

Sticky:

$(".sticky").sticky({
  topSpacing: 0,
  zIndex: 100
});

Stickyfill:

var elements = document.querySelectorAll('.sticky');
Stickyfill.add(elements);

Key Differences

  • Stickyfill focuses on polyfilling the CSS position: sticky behavior, while Sticky provides a more general sticky positioning solution.
  • Stickyfill offers better cross-browser compatibility and aims to closely mimic native position: sticky behavior.
  • Sticky provides options for customization, such as top spacing and z-index, while Stickyfill adheres more strictly to CSS specifications.

Use Cases

  • Choose Sticky for simple sticky positioning needs with easy setup and customization options.
  • Opt for Stickyfill when aiming for a more standards-compliant approach or when dealing with complex layouts that require accurate position: sticky behavior.

Both libraries serve their purposes well, with Sticky offering simplicity and Stickyfill providing a more robust polyfill for CSS position: sticky.

22,354

A light-weight, no-dependency, vanilla JavaScript engine to drive user's focus across the page

Pros of driver.js

  • More feature-rich, offering guided tours and highlighting elements
  • Active development with recent updates and larger community support
  • Provides a more interactive and user-friendly experience

Cons of driver.js

  • Larger file size and potentially more complex to implement
  • May be overkill for simple sticky element functionality
  • Requires more setup and configuration for basic use cases

Code Comparison

driver.js:

const driver = new Driver();
driver.highlight({
  element: '#my-element',
  popover: {
    title: 'Title',
    description: 'Description'
  }
});

Stickyfill:

var elements = document.querySelectorAll('.sticky');
Stickyfill.add(elements);

Summary

driver.js is a more comprehensive solution for creating interactive guided tours and highlighting elements on a webpage. It offers a rich set of features but may be excessive for simple sticky element functionality. Stickyfill, on the other hand, is a lightweight polyfill specifically designed for sticky positioning, making it more suitable for basic sticky element needs. The choice between the two depends on the specific requirements of the project and the desired level of interactivity and functionality.

Convert Figma logo designs to code with AI

Visual Copilot

Introducing Visual Copilot: A new AI model to turn Figma designs to high quality code using your components.

Try Visual Copilot

README


⚠ Unmaintained!

Stickyfill did a good job while the browsers were implementing position: sticky support. You can now safely use stickies without a polyfill, all modern browsers support them natively.




Polyfill for CSS position: sticky

The most accurate sticky polyfill out in the wild.

Check out the demo and use cases test page.

What it does

  • supports top-positioned stickies,

  • works in IE9+,

  • disables itself in older IEs and in browsers with native position: sticky support,

  • mimics original position: sticky behavior:

    • uses parent node as a boundary box,
    • behaves nicely with horizontal page scrolling,
    • only works on elements with specified top,
    • mimics native top and margin-bottom behavior,
    • works with table cells removed for consistency until Firefox makes a native implementation

What it doesn't

  • doesn't support left, right, bottom or combined stickies,
  • doesn't work in overflowed blocks,
  • doesn't parse your CSS! Launch it manually.

Installation   Usage   Pro tips   API   Feature requests   Bug reports   Contributing   Buy me a beer


Installation

NPM

npm install stickyfilljs --save

Yarn

yarn add stickyfilljs

Raw ES6 module

stickyfill.es6.js

Old fashioned

Download minified production ES5 script:

stickyfill.min.js

Include it on your page:

<script src="path/to/stickyfill.min.js"></script>

Usage

First things first, make sure your stickies work in the browsers that support them natively, e.g.:

<div class="sticky">
    ...
</div>
.sticky {
    position: -webkit-sticky;
    position: sticky;
    top: 0;
}

Then apply the polyfill:

JS:

var elements = document.querySelectorAll('.sticky');
Stickyfill.add(elements);

or JS + jQuery:

var elements = $('.sticky');
Stickyfill.add(elements);

Also worth having a clearfix:

.sticky:before,
.sticky:after {
    content: '';
    display: table;
}

Pro tips

  • top specifies sticky’s position relatively to the top edge of the viewport. It accepts negative values, too.
  • You can push sticky’s bottom limit up or down by specifying positive or negative margin-bottom.
  • Any non-default value (not visible) for overflow, overflow-x, or overflow-y on any of the ancestor elements anchors the sticky to the overflow context of that ancestor. Simply put, scrolling the ancestor will cause the sticky to stick, scrolling the window will not. This is expected with overflow: auto and overflow: scroll, but often causes confusion with overflow: hidden. Keep this in mind, folks!

Check out the test page to understand stickies better.

API

Stickyfill

Stickyfill.addOne(element)

element – HTMLElement or iterable element list (NodeList, jQuery collection, etc.). First element of the list is used.

Adds the element as a sticky. Returns new Sticky instance associated with the element.

If there’s a sticky associated with the element, returns existing Sticky instance instead.

Stickyfill.add(elementList)

elementList – iterable element list (NodeList, jQuery collection, etc.) or single HTMLElement.

Adds the elements as stickies. Skips the elements that have stickies associated with them.

Returns an array of Sticky instances associated with the elements (both existing and new ones).

Stickyfill.refreshAll()

Refreshes all existing stickies, updates their parameters and positions.

All stickies are automatically refreshed after window resizes and device orientations changes.

There’s also a fast but not very accurate layout change detection that triggers this method. Call this method manually in case automatic detection fails.

Stickyfill.removeOne(element)

element – HTMLElement or iterable element list (NodeList, jQuery collection, etc.). First element of the list is used.

Removes sticky associated with the element.

Stickyfill.remove(elementList)

elementList – iterable element list (NodeList, jQuery collection, etc.) or single HTMLElement.

Removes stickies associated with the elements in the list.

Stickyfill.removeAll()

Removes all existing stickies.

Stickyfill.forceSticky()

Force-enable the polyfill, even if the browser supports position: sticky natively.

Stickyfill.stickies

Array of existing Sticky instances.

Stickyfill.Sticky

Sticky class. You can use it directly if you want:

const sticky = new Stickyfill.Sticky(element);

Throws an error if there’s a sticky already bound to the element.

Sticky.refresh()

Refreshes the sticky, updates its parameters and position.

Sticky.remove()

Removes the sticky. Restores the element to its original state.

Feature requests

TL;DR

These features will never be implemented in Stickyfill:

  • Callbacks for sticky state changes
  • Switching classes between different sticky states
  • Other features that add non-standard functionality

If your request isn’t about one of these, you are welcome to create an issue. Please check existing issues before creating new one.

Some reasoning

Stickyfill is a polyfill. This means that it implements a feature (sticky positioning in this case) that already exists in some browsers natively, and allows to use this feature in the browsers that don’t support it yet and older versions of the browsers that didn’t support it at the time. This is its only purpose.

This also means that Stickyfill does nothing in the browsers that do support sticky positioning. Which, in turn, means that those browsers won’t support any additional non-standard features.

Bug reports

Check existing issues before creating new one. Please provide a live reproduction of a bug.

Contributing

Prerequisites

  • Install Git 😱
  • Install node
  • Install grunt-cli
  • Clone the repo, cd into the repo folder, run npm install (or yarn if you are fancy).

Ok, you are all set.

Building and testing

cd into the repo folder and run grunt. It will build the project from /src/stickyfill.js into /dist and run the watcher that will rebuild the project every time you change something in the source file.

Make changes to the source file. Stick to ES6 syntax.

Open /test/index.html in a browser that doesn’t support position: sticky to check that everything works as expected. Compare the results to the same page in a browser that supports position: sticky.

Commit the changes. DO NOT commit the files in the /dist folder. DO NOT change the version in package.json.

Make a pull request 👍

Adding / removing / updating npm packages

Use Yarn, dont’t forget to commit yarn.lock.

Using Stickyfill?

🍻 Buy me a beer

License

MIT license.

NPM DownloadsLast 30 Days