Tall sidebars get cut off
Plain sticky pins the top of a long sidebar, so its last items stay out of reach until the page ends.
Pins the bottom on the way down and the top on the way up.
1.8 kB gzipped0 dependenciesTypeScript
Two-way sticky positioning for sidebars taller than the screen. It follows headers that slide away, collapse or resize, and works with plain JS, React and Vue.
The orange column is a filter sidebar, taller than the window. Its top sits just under the header.
Why not just position: sticky
Plain sticky pins the top of a long sidebar, so its last items stay out of reach until the page ends.
→ Pins the bottom on the way down and the top on the way up.
A fixed top value fits one header state. When the header slides away, the sidebar stays where it was.
→ Follows the header's bottom edge every frame while it moves.
Banners, search bars and compact modes change the header height while the page is open.
→ Re-measures on resize, class and style changes.
Setup
Point it at the sidebar and the header. Put both columns in a grid that doesn't stretch the sidebar. Scroll handling, header tracking and cleanup are done for you.
The browser's own position: sticky moves the sidebar on every frame. The library only changes the sticky rules when the scroll direction flips or the header moves.
import { stickySidebar } from 'dynamic-sticky-sidebar';
stickySidebar('#sidebar', {
header: '.site-header',
media: '(min-width: 768px)',
});
import { StickySidebar } from 'dynamic-sticky-sidebar/react';
<StickySidebar header=".site-header" media="(min-width: 768px)">
<Filters />
</StickySidebar>
<script setup>
import { vStickySidebar } from 'dynamic-sticky-sidebar/vue';
const sticky = { header: '.site-header', media: '(min-width: 768px)' };
</script>
<template>
<aside v-sticky-sidebar="sticky"><Filters /></aside>
</template>
.layout {
display: grid;
grid-template-columns: 280px 1fr;
align-items: start; /* keep the sidebar at its own height */
}
Docs