Two Direction Sticky Sidebar For Vanilla JS
1. Include the Two Direction Sticky Sidebar’s JavaScript on the page.
<script src="sticky-sidebar.min.js"></script>
2. Add the data-sticky-sidebar attribute to your sidebar. The script queries for this attribute on initialization.
<aside data-sticky-sidebar>
<nav>
<a href="/articles">Articles</a>
<a href="/tutorials">Tutorials</a>
<a href="/tools">Tools</a>
</nav>
</aside> 3. Pass configuration directly in HTML. All available HTML data attributes:
data-top-gap (integer or "auto"): Margin in pixels between the top of the viewport and the sidebar during sticky mode. Defaults to 0. Pass "auto" to use the sidebar’s computed document offset on page load.data-bottom-gap (integer): Margin in pixels between the bottom of the viewport and the sidebar. Defaults to 0.data-mobile-width (integer): Screen width in pixels at or below which the sticky effect turns off. Defaults to 0, meaning sticky stays active at all viewport widths.<aside data-sticky-sidebar data-top-gap="120" data-bottom-gap="32" data-mobile-width="1024"> <!-- sidebar content --> </aside>
Q: Does this library work with multiple sidebars on the same page?
A: No. The library works only the first matching element it finds.
Q: The sidebar snaps to an incorrect position when the page loads. How do I fix this?
A: This usually happens when the sidebar sits below a fixed header. Set data-top-gap="auto" to tell the script to calculate the initial offset from the sidebar’s actual position in the document.
Q: Can I update the gap values after the page has loaded?
A: Yes. Update data-top-gap or data-bottom-gap directly on the element via JavaScript. The MutationObserver inside the script detects attribute changes and reinitializes with the new values automatically.
Q: Does the sticky behavior break inside CSS Grid or Flexbox layouts?
A: No. The script sets position: sticky and height: fit-content on the sidebar element. These apply correctly inside flex and grid containers as long as the sidebar’s parent container is taller than the sidebar itself.
Q: What happens if data-mobile-width is not set?
A: Sticky behavior stays active at all viewport widths. Set it to a pixel value such as 1024 to disable the effect on screens narrower than that breakpoint.
The post Two Direction Sticky Sidebar For Vanilla JS appeared first on CSS Script.
Two Direction Sticky Sidebar is a pure JavaScript utility that implements bi-directional sticky sidebar behavior…
Linked Lists is a JavaScript & CSS UI component that connects multiple checked list items…
LANSING, MI (WOWO) Governor Gretchen Whitmer has expanded Michigan’s state of emergency as severe weather…
LANSING, MI (WOWO) Advocates and lawmakers are urging Michigan Governor Gretchen Whitmer to grant clemency…
A proof-of-concept (PoC) exploit has been publicly released for a newly disclosed vulnerability in Microsoft’s…
INDIANAPOLIS, IND. (WOWO) State leaders in Indiana are supporting a major new investment aimed at…
This website uses cookies.