Sticky Sidebar jQuery plugin
Posted on March 21, 2011 by Phil 95 comments
This jQuery plugin allows you to set a number of sidebar or floating boxes that follow the user down the page as they scroll. The plugin uses absolute positioning calculated from the original css and offset to either the closest positioned parent or to the document. The plugin has been tested in IE6-9 and all the other major browsers.
The JavaScript (jQuery)
Below is the code for the plugin, to download the code with examples use the link above.
Usage
The plugin can be used for single or multiple boxes as shown in the examples below
A Single sticky sidebar
<div id="main">
<h2>Product catalogue</h2>
<ul id="products">
[...]
</ul>
</div>
<div id="side">
<div id="basket">
<h3>Your basket</h3>
<p>Total: <strong>£455.00</strong></p>
<span id="items">4</span>
</div>
</div>
For the above snippet of a HTML page we can use the following to enable the product basket as a sticky sidebar with the default options.
$('#basket').stickySidebar();
Multiple sticky boxes
<div id="left">
<h2>Left column</h2>
<ol id="navbox" class="stickybox">
<li><a href="#">Home</a></li>
<li><a href="#">About</a></li>
<li><a href="#">Contact</a></li>
</ol>
</div>
<div id="middle"></div>
<div id="right">
<h2>Right column</h2>
<div id="basket" class="stickybox">
<h3>Your basket</h3>
<p>Total: <strong>£455.00</strong></p>
<span id="items">4</span>
</div>
</div>
For the above example we can use the below to set the navbox and basket boxes to both be sticky sidebars with the same options.
$('.stickbox').stickySidebar({speed: 400, padding: 30, constrain: true})
Options
The Sticky Sidebar plugin has four options:
- speed – how long the animation of the box should take in milliseconds (default = 250)
- easing – what type of easing to use for the animation (default = “linear”)
- padding – how many pixels to pad the element from the top of the window (default = 10)
- constrain – if true stops sticky scrolling out of parent
Methods
The Sticky Sidebar plugin has two methods:
- remove – removes the sticky selected sticky sidebars and resets to original position
- destroy – removes and resets all sticky sidebars and removes event listeners
Related Posts
No related posts.
95 Responses So Far
← Older Comments-
-
DESIGNfromWITHIN January 12, 2012 at 12:34 pm
Hi I would like to do the same, have the container be a parent wrapper, without knowing the height of the sidebar.
.sticky_sidebar_container {
position: relative;
}
.sticky_sidebar {
width: 200px;
}
.post {}
and:
This slides
Here is my content of unknown height
-
Neo January 18, 2012 at 9:49 am
Hi, thx for the great post its really helpful… here is some query it would be gr8 if you can help in this.
Trying to have the sticky bar without animation but it flickers how can i avoid that. -
Joe January 19, 2012 at 9:16 am
Hi Phil,
Thanks for this. It’s almost perfect! I think the reason you’ve had a couple of comments about the constraint option is that unless a specific height value is set for the container the constraint option does not work.
For example, my container’s height is auto and is defined by the height of the content div which shares the same container as my sticky side bar. Unless I set a specific height on the container the constraint option does not work.
Thanks, I appreciate you sharing this!
-
Joe January 19, 2012 at 9:28 am
Here’s a screenshot of your demo page if I adjust the ‘left column’ to have a height of ‘auto’. The contained sidebar scrolls past the bottom.
http://cl.ly/3g3z2q0q3T3R1A0C3e1z
I am using Chrome 16 on OSX by the way.
-
Tomas February 16, 2012 at 10:35 am
Hi, great code but would it be possible to use no easing and just have it “stick”?
Thanks
-
Raf February 17, 2012 at 5:44 am
Is it possible to disable the easing completely? I tried using speed=0, but it still “jitters” around.
Thanks,
Raf





Great article and thank you. I’d like to be able to constrain a sidebar in the parent wrapper, without knowing the height of the sidebar (or the containing DIV). An example of this would be a dynamic unordered list subnav. Possible?