CSSScript Demo Page

Two Direction Sticky Sidebar Demo

Download Back To CSSScript.Com

A vanilla JS sticky sidebar script with two way scroll behavior, live data attributes, and responsive mobile cutoff support.

Pure JavaScript No build step Live attributes Responsive cutoff
Scroll the page to inspect the sticky behavior. The sidebar pins at the top or bottom when its full content has been reached.
Core Behavior

Sticky when short, sliding when tall.

The sidebar stays fixed within the viewport when its content fits inside the available height. When the sidebar content is taller than the viewport, it moves with the reader until the top or bottom edge reaches the configured gap.

This pattern keeps navigation, filters, summaries, and calls to action reachable without cutting off long sidebar content on smaller screens.

Top gap Controls the resting distance between the sidebar and the top of the viewport.
Bottom gap Preserves breathing room below the sidebar before it locks to the lower edge.
Mobile width Turns off sticky positioning at or below the selected screen width.
Live updates MutationObserver refreshes the layout when data attributes change.
Demo Workflow

Adjust the sidebar while the page stays open.

The range controls in the sidebar update the same data attributes used by the library. Each change takes effect immediately, so the sticky offset, lower limit, and responsive cutoff can be tested without a reload.

1
Scroll down until the long sidebar reaches its lower content. The sidebar stops at the bottom gap instead of disappearing above the viewport.
2
Scroll back up until the sidebar top returns. The same element reverses direction and pins to the configured top gap.
3
Change the controls during the scroll session. The displayed attributes and sticky position update through live data-attribute monitoring.
Content States

Sidebar height changes expose both modes.

The compact preset demonstrates standard sticky behavior for a sidebar that fits inside the viewport. The extended preset adds enough sidebar content to demonstrate the two-direction scrolling logic that makes this library useful for long menus and dense filter panels.

Compact sidebar

The element uses a stable top gap because every sidebar item can remain visible at once.

Extended sidebar

The element moves between top and bottom boundaries so hidden items can come into view while the page scrolls.

Practical Limits

Designed for one active sticky sidebar.

The script initializes the first element matching data-sticky-sidebar or the legacy data-sticky="true" selector. A page that needs several independent sticky sidebars would need an extension that loops through every matching element and stores separate state for each one.

The demo keeps one active sidebar so every control maps to the library's current initialization model.