Sticky Sidebar jQuery plugin
Posted on March 21, 2011 by Phil 133 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.
133 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 -
Leonard February 23, 2012 at 5:08 pm
Great plugin but there is one issue I’m having, so I’ve got a bit of a feature request / any ideas one for you:
I’m using it on a page where there are accordion elements that are closed by default and are opened up once in the page. Once opened the sidebar stops too high up the page, this I can fix if I resize the window as it seems to fire the function to recalculate it’s bounds but I can’t find a way to make it recalculate when the page gets longer. Any suggestions?
-
Isaac February 28, 2012 at 5:16 am
wow looks amazing, does this play well with the 960 grid system?
-
Mr. Raymon March 14, 2012 at 10:34 pm
I have a problem with the plugin. I use the plugin in a project what works perfectly in local (my PC) but I upload the webpage project on a site and curiously on the pages with much height get stuck.
Plugin’s error? Browser’s error? I don’t understand :/
Thanks and Regards. Excuse me for my ‘english’ is very bad.
-
Pingback: 2012년 3월 16일 it 기술 동향 |
-
-
Pingback: ECCUBE おしゃれなECサイトのスクロールする買い物カゴの設置 | 戯れ問答
-
Jason Kilp March 28, 2012 at 9:50 pm
Hi Phil! Excellent plugin and thanks for all of your work.
I’m having an issue (nothing to do with how your plugin is supposed to work), and wanted to see if you can help.
Because of a variety of reasons… the plugin is constrained within a DIV whose height is set dynamically. FF and Webkit like to calculate heights differently, so that makes it loads of fun for me.
But my question is: Is there a way to set the plugin so that if you have scrolled almost all of the way to the bottom that it automatically triggers the plugin to move to the bottom of its container?
-
Ge Cao March 30, 2012 at 8:00 am
Great!
-
Pingback: Tweet Heat - The hottest Tweets of the Month [March 2012] | Inspired Magazine
-
Pingback: 40 Recently Released jQuery Plugins
-
-
Dirgeis April 12, 2012 at 2:10 pm
Hi I would like the css code to remove the background color of a static sidebar (widgetbar)- I need to override the existing color. Please help. I’ve tried several codes.
Here’s my the source code – I want the bottom part of the sidebar to match the top; home, shopping cart, sign in, register, about us – then second section of the sidebar; jewelry, all jewelry etc… has a grey background and I wanted clear like the top section. I would also like to change the font but most importantly removing the grey background from the sidebar would be greatly appreciated – see code here…
-
Pingback: 22 Excellent & Recent jQuery Plugins « The Creative Project
-
Pingback: 40 Recently Released jQuery Plugins
-
seo services April 26, 2012 at 10:05 am
Ive been here a few times and it seems like your articles get much more informative every time. Keep it up I appreciate reading them.
-
-
-
Alex May 8, 2012 at 6:20 pm
Hi, thank you for this great plugin! Here’s a little bug I came across. I am developing this tricky website where my default text container is 700px wide. One one page it has to be 755px wide. For this reason the “constrain” param doesn’t work and the sidebar gets over my footer.
-
Robert May 10, 2012 at 2:01 pm
Hi, this is an awesome plugin, although I am developing a responsive layout, and when the page is resized below 789 wide it collapses into a single column, I am having trouble adapting the code to compensate destroying does work when resized but how would I “un-destroy” an element can anyone help me?
Thanks.
-
Thomas May 10, 2012 at 9:56 pm
Really nice plugin. Just thought I might want to share a bug I found:
Using stickysidebar.js with JQueryUI I noticed this error when trying to resize a dialog – Line: 131
Error: Object doesn’t support property or method ‘data’.References the code in the reset function
var data = $sb.data(“stickySB”);If you have the time, then it might be worth looking into. Don’t worry about it if you don’t.
I did ‘fix’ it by using $(this).data instead of $sb.data but I’ve got a feeling that is just wrong





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?