sticky-sidebar
😎 Pure JavaScript tool for making smart and high performance sticky sidebar.
Top Related Projects
Fixed <thead>. Doesn't need any custom css/html. Does what position:sticky can't
A light-weight, no-dependency, vanilla JavaScript engine to drive user's focus across the page
A lightweight script to animate scrolling to anchor links.
Finally, a "back to top" button that behaves like a real elevator.
Give your pages some headroom. Hide your header until you need it
Quick Overview
The abouolia/sticky-sidebar
project is a lightweight and flexible JavaScript library that allows you to create sticky sidebars on your web pages. It provides a simple and customizable way to make a sidebar element stick to the viewport as the user scrolls the page.
Pros
- Lightweight: The library is small in size, weighing in at around 3KB (minified and gzipped), making it suitable for use in performance-conscious projects.
- Flexible: The library offers a range of configuration options, allowing you to customize the behavior of the sticky sidebar to fit your specific needs.
- Cross-browser Compatibility: The library is designed to work across a wide range of modern browsers, ensuring a consistent user experience.
- Dependency-free: The library does not require any external dependencies, making it easy to integrate into your existing project.
Cons
- Limited Functionality: The library is focused solely on creating sticky sidebars and does not provide any additional features or functionality beyond that.
- Potential Performance Issues: Depending on the complexity of your page and the number of sticky elements, the library may have a negative impact on performance, especially on older or less powerful devices.
- Lack of Active Maintenance: The project appears to have been last updated in 2018, which may indicate a lack of ongoing maintenance and support.
- Limited Documentation: The project's documentation is relatively sparse, which may make it more difficult for new users to get started with the library.
Code Examples
Here are a few examples of how to use the abouolia/sticky-sidebar
library:
- Basic Initialization:
var sidebar = new StickySidebar('#sidebar', {
topSpacing: 20,
bottomSpacing: 20,
containerSelector: '.container',
innerWrapperSelector: '.sidebar__inner',
stickyClass: 'is-affixed',
resizeSensor: true,
minWidth: 0
});
This code initializes a new sticky sidebar element with various configuration options, such as the top and bottom spacing, the container selector, and the sticky class name.
- Updating Sidebar Position:
sidebar.updateSticky();
This method can be used to manually update the position of the sticky sidebar, for example, after the page layout has changed.
- Destroying the Sidebar:
sidebar.destroy();
This method can be used to remove the sticky sidebar functionality and restore the element to its original state.
- Responsive Behavior:
sidebar.options.minWidth = 768;
By setting the minWidth
option, you can specify the minimum viewport width at which the sticky sidebar functionality should be enabled, allowing you to create responsive designs.
Getting Started
To use the abouolia/sticky-sidebar
library in your project, follow these steps:
- Include the library in your HTML file:
<script src="https://cdn.jsdelivr.net/npm/sticky-sidebar@3.3.1/dist/sticky-sidebar.min.js"></script>
- Create a container element for your sidebar:
<div class="container">
<div class="sidebar" id="sidebar">
<div class="sidebar__inner">
<!-- Your sidebar content goes here -->
</div>
</div>
<div class="content">
<!-- Your main content goes here -->
</div>
</div>
- Initialize the sticky sidebar in your JavaScript code:
var sidebar = new StickySidebar('#sidebar', {
topSpacing: 20,
bottomSpacing: 20,
containerSelector: '.container',
innerWrapperSelector: '.sidebar__inner',
stickyClass: 'is-affixed',
resizeSensor: true,
minWidth: 0
});
- (Optional) Customize the behavior of the sticky sidebar by adjusting the available options.
That's it! With these steps, you can easily integrate the abouolia/sticky-sidebar
library into your web project and create sticky sidebars that enhance the user experience.
Competitor Comparisons
Fixed <thead>. Doesn't need any custom css/html. Does what position:sticky can't
Pros of floatThead
- Focuses on floating table headers, providing a specialized solution for tables
- Supports complex table structures with colspan and rowspan
- Works with various scrolling containers and window scrolling
Cons of floatThead
- Limited to table headers, less versatile for general sticky elements
- May require more setup for non-table elements
- Potentially heavier performance impact due to table-specific calculations
Code Comparison
sticky-sidebar:
var sidebar = new StickySidebar('#sidebar', {
topSpacing: 20,
bottomSpacing: 20,
containerSelector: '.container',
innerWrapperSelector: '.sidebar__inner'
});
floatThead:
$('table').floatThead({
position: 'fixed',
top: 50,
scrollContainer: function($table){
return $table.closest('.table-container');
}
});
Key Differences
- sticky-sidebar is designed for creating sticky sidebars and other elements
- floatThead specializes in floating table headers
- sticky-sidebar offers more general-purpose stickiness for various elements
- floatThead provides more table-specific features and optimizations
Both libraries serve different purposes, with sticky-sidebar being more versatile for general sticky elements, while floatThead excels in handling complex table header scenarios. The choice between them depends on the specific requirements of your project, particularly whether you need sticky sidebars or floating table headers.
A light-weight, no-dependency, vanilla JavaScript engine to drive user's focus across the page
Pros of driver.js
- Provides interactive guided tours and feature highlights for web applications
- Offers more versatile functionality beyond just sticky sidebars
- Supports multiple browsers and devices with responsive design
Cons of driver.js
- Larger file size and potentially more complex implementation
- May require more setup and configuration for basic use cases
- Not specifically optimized for sidebar functionality
Code Comparison
driver.js:
const driver = new Driver();
driver.highlight({
element: '#my-element',
popover: {
title: 'Title',
description: 'Description'
}
});
sticky-sidebar:
var sidebar = new StickySidebar('#sidebar', {
topSpacing: 20,
bottomSpacing: 20,
containerSelector: '.container',
innerWrapperSelector: '.sidebar__inner'
});
Summary
While driver.js offers more comprehensive functionality for creating interactive guides and highlighting features, sticky-sidebar is more focused and lightweight, specifically designed for creating sticky sidebars. The choice between the two depends on the specific needs of the project. If you require a simple sticky sidebar, sticky-sidebar may be more appropriate. However, if you need a more versatile tool for creating interactive tours and highlighting various elements, driver.js would be the better choice.
A lightweight script to animate scrolling to anchor links.
Pros of smooth-scroll
- Lightweight and dependency-free (only 1.9 KB minified and gzipped)
- Supports multiple scroll animations and easing functions
- Extensive browser compatibility, including older versions
Cons of smooth-scroll
- Limited to scroll functionality, unlike sticky-sidebar's additional features
- Requires manual integration with other UI components
- May conflict with native smooth scrolling in modern browsers
Code Comparison
smooth-scroll:
var scroll = new SmoothScroll('a[href*="#"]', {
speed: 500,
easing: 'easeInOutCubic'
});
sticky-sidebar:
var sidebar = new StickySidebar('#sidebar', {
topSpacing: 20,
bottomSpacing: 20,
containerSelector: '.container',
innerWrapperSelector: '.sidebar__inner'
});
Key Differences
- Purpose: smooth-scroll focuses on smooth scrolling animations, while sticky-sidebar creates fixed-position sidebars
- Functionality: smooth-scroll is more specialized, whereas sticky-sidebar offers additional features like resize handling and scroll-sensitive positioning
- Integration: smooth-scroll is easier to implement for basic scrolling needs, while sticky-sidebar requires more configuration for optimal sidebar behavior
Use Cases
- smooth-scroll: Ideal for single-page websites or improving navigation experience
- sticky-sidebar: Better suited for content-heavy sites with long scrollable pages and complex layouts
Finally, a "back to top" button that behaves like a real elevator.
Pros of Elevator.js
- Elevator.js provides a simple and lightweight solution for adding a "back to top" button to a website.
- The library is easy to integrate and customize, with a straightforward API and minimal dependencies.
- Elevator.js includes smooth scrolling animation, which can improve the user experience.
Cons of Elevator.js
- Elevator.js does not provide the same level of functionality as Sticky Sidebar, which is a more comprehensive solution for managing sidebar behavior.
- The library may not be as feature-rich as some other "back to top" solutions, such as those that offer additional customization options.
Code Comparison
Sticky Sidebar (abouolia/sticky-sidebar):
var stickySidebar = new StickySidebar('#sidebar', {
topSpacing: 20,
bottomSpacing: 20,
containerSelector: '.main-content',
innerWrapperSelector: '.sidebar__inner'
});
Elevator.js (tholman/elevator.js):
var elevator = new Elevator({
element: document.querySelector('.elevator-button'),
mainElement: document.querySelector('body'),
duration: 3000,
verticalPadding: 0
});
Give your pages some headroom. Hide your header until you need it
Pros of headroom.js
- Focuses on hiding/showing header elements based on scroll direction
- Lightweight and easy to implement
- Provides smooth animations for header visibility
Cons of headroom.js
- Limited to header functionality, not suitable for general sticky elements
- Requires additional CSS for proper styling and positioning
Code Comparison
headroom.js:
var myElement = document.querySelector("header");
var headroom = new Headroom(myElement);
headroom.init();
sticky-sidebar:
var sidebar = new StickySidebar('#sidebar', {
topSpacing: 20,
bottomSpacing: 20,
containerSelector: '.container',
innerWrapperSelector: '.sidebar__inner'
});
Summary
headroom.js is specialized for header visibility management, offering a lightweight solution for hiding/showing headers based on scroll direction. It's easy to implement but limited in scope compared to sticky-sidebar.
sticky-sidebar provides a more versatile solution for creating sticky elements, particularly sidebars. It offers more customization options and is suitable for various page elements, not just headers.
While headroom.js focuses on vertical scroll behavior for headers, sticky-sidebar allows for sticky positioning of different elements with various configuration options. The choice between the two depends on the specific requirements of the project and the desired sticky behavior.
Convert designs to code with AI
Introducing Visual Copilot: A new AI model to turn Figma designs to high quality code using your components.
Try Visual CopilotREADME
Sticky Sidebar
Pure JavaScript plugin for making smart and high performance sticky sidebars.
For complete documentation and examples see abouolia.github.com/sticky-sidebar
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.
- Integrated with resize sensor to re-calculate all dimenstions of the plugin when size of sidebar or its container is changed.
- 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 jQuery plugin from Bowser, NPM or just simply download it from this page and link to the sticky-sidebar.js
file in your project folder.
Bower
If you are using bower as package manager:
bower install sticky-sidebar
NPM
If you are using NPM as package manager:
npm install sticky-sidebar
Usage
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__innner
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.
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'
});
</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 sticky-sidebar.js
script file after jquery.js
.
Usage with ResizeSensor.js
Sticky sidebar integrated with ResizeSensor.js to detect when sidebar or container is changed. To use resize sensor with this plugin just make sure to include ResizeSensor.js before sticky-sidebar.js
code whether through module loader, bundle or event inclusion as a <script>
and enable resizeSensor
option (enabled by default) and it will works.
You can choose not to include ResizeSensor.js
and sticky sidebar will continue work without any problem but without automatically detect resize changes.
Browser Support
Sticky sidebar 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.
If you have any issues with browser compatibility donât hesitate to Submit an issue.
License
Sticky Sidebar is released under the MIT license. Have at it.
Made by Ahmed Bouhuolia
Top Related Projects
Fixed <thead>. Doesn't need any custom css/html. Does what position:sticky can't
A light-weight, no-dependency, vanilla JavaScript engine to drive user's focus across the page
A lightweight script to animate scrolling to anchor links.
Finally, a "back to top" button that behaves like a real elevator.
Give your pages some headroom. Hide your header until you need it
Convert designs to code with AI
Introducing Visual Copilot: A new AI model to turn Figma designs to high quality code using your components.
Try Visual Copilot