Hit enter after type your search item

The sticky sidebar based on JavaScript / jQuery

The sticky-sidebar is a smooth and light-weight pure JavaScript based plug-in. The sticky sidebar created by using this plug-in does not calculate all dimensions but just the necessary ones.

The sidebar that sticks can be small sized or large while the plug-in will handle it smoothly. You may also use jQuery option for creating the sidebar if you want to.

Demo1 Demo2
Developer page Download plug-in

Installing the sidebar plug-in

You may install the sticky-sidebar plug-in via NPM or Bower:

Using Bower:

bower install sticky-sidebar

Using NPM:

npm install sticky-sidebar

You may also get the required JavaScript file by downloading the compressed package from GitHub website. For that, use the above-provided links.

The markup

You need to follow certain guidelines for the markup in order to sticky sidebar working. The main div element contains the sticky bar div and the main content div as shown in the structure below:


 

See the demos below with JavaScript and jQuery with complete code.

A simple example of sticky sidebar with small content

In this demo, the sidebar contains a little content that does not scroll. The main content is scrollable; so as you scroll down, you will see the left bar sticks:

JavaScript sticky sidebar

See online demo and code

As such, this is pure JavaScript based solution – no other dependency file is required. You just need to include the sticky-sidebar.js file. This is how the script is used for the demo:


 

You may specify the topSpacing option for the distance where sidebar stuck from the top. You also need to specify the container which is the main div in that case.

Besides, the div containing the content for the sticky sidebar is also specified in JavaScript. See the complete code and output on the demo page.

A demo with scrollable sidebar

For this demo, the sidebar with more content is created so that it is scrollable. As you scroll down the page, the sidebar will stick as its content ends after scrolling while the main content towards the right side keeps on scrolling, have a look:

JavaScript sticky sidebar 2

See online demo and code

An example of using jQuery for sticky sidebar

The output is the same as in the first example, except I used jQuery for creating the sticky sidebar. For that, you need to include the jQuery library (if not using already) along with the lightweight JavaScript file (jquery-sticky-sidebar.min.js).

See online demo and code

The script for this demo:


 

See the complete code including the markup on the demo page.

This div height required for enabling the sticky sidebar