dynamic-sticky-sidebardynamic-sticky-sidebar
Guide
  • Vanilla JS
  • React
  • Vue
API
Demo
npm
  • English
  • فارسی
  • العربية
  • Deutsch
  • Français
  • Italiano
GitHub
Guide
  • Vanilla JS
  • React
  • Vue
API
Demo
npm
  • English
  • فارسی
  • العربية
  • Deutsch
  • Français
  • Italiano
GitHub
  • Guide

    • Introduction
    • Getting Started
    • Layout Requirements
    • Dynamic Headers
    • How It Works
    • Troubleshooting
  • Frameworks

    • Vanilla JS
    • React
    • Vue
  • Reference

    • API Reference
    • Live Demo

Getting Started

Install

npm
npm i dynamic-sticky-sidebar
pnpm
pnpm add dynamic-sticky-sidebar
yarn
yarn add dynamic-sticky-sidebar
CDN
<script src="https://unpkg.com/dynamic-sticky-sidebar"></script>
<!-- exposes window.StickySidebar.stickySidebar -->

1. Lay out the page

Put the sidebar in a grid (or a row-direction flex) next to the content, and stop it from stretching to the full row height:

<header class="site-header">…</header>

<div class="layout">
    <aside id="sidebar">…</aside>
    <main>…</main>
</div>
.site-header {
    position: sticky;
    top: 0;
    z-index: 10;
}

.layout {
    display: grid;
    grid-template-columns: 280px 1fr;
    gap: 32px;
    align-items: start; /* important: the sidebar keeps its own height */
}

Why grid?

A grid or row-flex parent lets the library use native sticky behaviour in both directions. Block layouts still work, but scrolling up then uses a JS fallback. See Layout requirements.

2. Attach the sidebar

Vanilla
import { stickySidebar } from 'dynamic-sticky-sidebar';

const sidebar = stickySidebar('#sidebar', {
    header: '.site-header',
    gap: 16,
    media: '(min-width: 768px)',
});

// later, e.g. when the page unmounts
sidebar.destroy();
React
import { StickySidebar } from 'dynamic-sticky-sidebar/react';

export function Layout({ children }) {
    return (
        <div className="layout">
            <StickySidebar header=".site-header" gap={16} media="(min-width: 768px)">
                …
            </StickySidebar>
            <main>{children}</main>
        </div>
    );
}
Vue
<script setup>
import { vStickySidebar } from 'dynamic-sticky-sidebar/vue';

const sticky = { header: '.site-header', gap: 16, media: '(min-width: 768px)' };
</script>

<template>
    <div class="layout">
        <aside v-sticky-sidebar="sticky">…</aside>
        <main><slot /></main>
    </div>
</template>

That's it. Scroll down and the sidebar follows the content until its end, then sticks. Scroll up and it comes back and sticks under the header, wherever the header currently is.

3. Try it

Scroll inside the box below. The sub-nav slides away on scroll down and returns on scroll up, and the sidebar follows it.

Open in new tab
Shopscroll inside this box ↓
CategoriesBrandsDeals
Filter 1
Filter 2
Filter 3
Filter 4
Filter 5
Filter 6
Filter 7
Filter 8
Filter 9
Filter 10
Filter 11
Filter 12
Filter 13
Filter 14
Filter 15
Filter 16
Filter 17
Filter 18 (last)
Card 1
Card 2
Card 3
Card 4
Card 5
Card 6
Card 7
Card 8
Card 9
Card 10
Card 11
Card 12
Card 13
Card 14
Card 15
Card 16
Card 17
Card 18
Card 19
Card 20
Card 21
Card 22
Card 23
Card 24
footer

Next steps

  • Layout requirements: grid vs. flex vs. block, and common pitfalls
  • Dynamic headers: multiple headers, JS-animated headers and state-driven offsets
  • API reference: all options and instance methods
Edit this page on GitHub
Previous
Introduction
Next
Layout Requirements