sticky polite

Scroll down, then up to observe the behavior.

A compact, robust, zero-dependency headless utility for 'polite' sticky positioning. Imagine if position: sticky could understand when your user wants it out of the way and politely excuses itself from the viewport.

Keep important page elements highly available, while keeping the screen clear of distractions.

github repo

Key Features:

Limitations:

Installation

npm install sticky-polite

Usage

Import the package.

import "sticky-polite";

or

<script src="https://unpkg.com/sticky-polite/dist/index.cjs"></script>

Add the class and an edge offset to an element:

// inline edge offset
<header class="sticky-polite" style="top: 1rem">Header</header>

- or -

// via stylesheet
<style>
  header.sticky-polite { top: 20px }
</style>

Important Notes:

Advanced Usage

You can optionally style the element based on the state it's in.

.sticky-polite[data-polite-state="static"] {
  outline: none;
}

.sticky-polite[data-polite-state="intersect"] {
  outline: 3px solid green;
}

.sticky-polite[data-polite-state="sticky"] {
  outline: 3px solid blue;
}

Important Notes:

Examples

Nested Header

This header and footer should be constrained to their scrolling parent

footer • links • etc

scroll left / right

scroll left / right