sticky-sidebar-v2
v1.3.0
Published
A JavaScript plugin for making smart and high performance sticky sidebars.
Maintainers
Readme
Sticky Sidebar v2 
Pure JavaScript plugin for making smart and high performance sticky sidebars, originally written by Ahmed Bouhuolia.
Why the fork?
This project was forked from the original Sticky Sidebar, written by Ahmed Bouhuolia. As the original project is currently inactive, this fork is an attempt to revive the project and add some improvements.
Examples
Sidebar taller than the content
For complete documentation and examples see blixhavn.github.io/sticky-sidebar-v2
Why is sticky sidebar so awesome?
- It does not re-calculate all dimensions when scrolling, just necessary dimensions.
- Super smooth without incurring scroll lag or jank and no page reflows.
- It has event trigger on each affix type to hook your code under particular situation.
- Handle the sidebar when is tall or too short compared to the rest of the container.
- Zero dependencies and super simple to setup.
Install
You can download sticky sidebar v2 from Yarn, NPM or just simply download it from this page and link to the sticky-sidebar.js file in your project folder.
Yarn
If you are using Yarn as package manager:
yarn add sticky-sidebar-v2NPM
If you are using NPM as package manager:
npm install sticky-sidebar-v2Usage
Your website's html structure has to be similar to this in order to work:
<div class="main-content">
<div class="sidebar">
<div class="sidebar__inner">
<!-- Content goes here -->
</div>
</div>
<div class="content">
<!-- Content goes here -->
</div>
</div>Note that inner sidebar wrapper .sidebar__inner is optional but highly recommended, if you don't write it yourself, the script will create one for you under class name inner-wrapper-sticky. but this may cause many problems.
If your content is inside a fixed-height div with a scrollbar, this must be specified with the scrollContainer option.
For the above example, you can use the following JavaScript:
<script type="text/javascript" src="./js/sticky-sidebar.js"></script>
<script type="text/javascript">
var sidebar = new StickySidebar('.sidebar', {
topSpacing: 20,
bottomSpacing: 20,
containerSelector: '.main-content',
innerWrapperSelector: '.sidebar__inner',
scrollContainer: '#main-viewport'
});
</script>Via jQuery/Zepto
You can configure sticky sidebar as a jQuery plugin, just include jquery.sticky-sidebar.js instead sticky-sidebar.js file than configure it as any jQuery plugin.
<script type="text/javascript" src="./js/jquery.js"></script>
<script type="text/javascript" src="./js/jquery.sticky-sidebar.js"></script>
<script type="text/javascript">
$('#sidebar').stickySidebar({
topSpacing: 60,
bottomSpacing: 60
});
</script>Make sure to include jquery.sticky-sidebar.js script file after jquery.js.
Via a bundler
When installed from NPM, import the class:
import StickySidebar from 'sticky-sidebar-v2';
const sidebar = new StickySidebar('.sidebar', {
containerSelector: '.main-content',
innerWrapperSelector: '.sidebar__inner'
});With CommonJS, the class is the default export: require('sticky-sidebar-v2').default.
The jQuery plugin looks for window.jQuery when it loads, so it is meant for <script> tags. With a bundler, use the class directly.
The module can be imported during server-side rendering (Nuxt, Next.js and similar), but only create the instance in the browser, for example in onMounted or useEffect.
Methods
updateSticky() - Recalculates the cached dimensions and repositions the sidebar. Size changes of the sidebar and its container, and window resizes, are picked up automatically. Call this when the layout changes in other ways, for example when content inserted above the container moves it down.
destroy() - Removes inline styles, the sticky class and all event listeners.
sidebar.updateSticky();
sidebar.destroy();With jQuery, pass the method name: $('.sidebar').stickySidebar('destroy').
Dynamic pages
If the sidebar or container element is replaced, for example on client-side navigation, destroy the old instance and create a new one for the new elements:
import { onMounted, onBeforeUnmount } from 'vue';
import StickySidebar from 'sticky-sidebar-v2';
let sidebar;
onMounted(() => { sidebar = new StickySidebar('.sidebar', { containerSelector: '.main-content' }); });
onBeforeUnmount(() => sidebar.destroy());Sidebar taller than the content
The plugin sticks one element, and only while that element is shorter than its container. A sidebar that is already the tallest column has nowhere to move, so it stays put. Which column is taller usually depends on the content being shown, and that can change after the page is set up.
Pass the other column as otherColumnSelector. The shorter column is the sticky one, and this is checked again whenever either column changes height:
const sidebar = new StickySidebar('.sidebar', {
containerSelector: '.main-content',
innerWrapperSelector: '.column__inner',
otherColumnSelector: '.content'
});Give both columns an inner wrapper with that selector. A column without one gets a wrapper created for it. While the article is shorter, the article stays in view as the sidebar scrolls past. When the article grows taller than the sidebar, the sidebar sticks instead. A column that is not displayed, such as display: none, is left out of the comparison. Affix events are fired on the column that is sticking, which can be either one, and the column that stops sticking receives the static event. See the demo.
Browser Support
Sticky Sidebar v2 works in all modern browsers including Internet Explorer 9 and above, but if you want it to work with IE9, should include requestAnimationFrame polyfill before sticky sidebar code. For backwards compatibility, a polyfill for ResizeObserver can also be included.
If you have any issues with browser compatibility don’t hesitate to Submit an issue.
License
Sticky Sidebar v2 is released under the MIT license. Have at it.
Maintained by Øystein Blixhavn
Originally made by Ahmed Bouhuolia
