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>&pound;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>&pound;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:

  1. speed – how long the animation of the box should take in milliseconds (default = 250)
  2. easing – what type of easing to use for the animation (default = “linear”)
  3. padding – how many pixels to pad the element from the top of the window (default = 10)
  4. constrain – if true stops sticky scrolling out of parent

Methods

The Sticky Sidebar plugin has two methods:

  1. remove – removes the sticky selected sticky sidebars and resets to original position
  2. destroy – removes and resets all sticky sidebars and removes event listeners

Related Posts

No related posts.

133 Responses So Far

← Older Comments
  1. Ryan Thrash January 5, 2012 at 6:08 am

    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?

    • Phil January 5, 2012 at 8:07 pm

      Sure just use the constrain attribute and ensure the parent element is positioned relative or absolute. If the parent element is only as high as the nav menu then of course it won’t scroll anywhere as it will be constrained to that parent element.

  2. 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

  3. 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.

  4. 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!

  5. 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.

  6. 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

  7. 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

  8. 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?

  9. Isaac February 28, 2012 at 5:16 am

    wow looks amazing, does this play well with the 960 grid system?

  10. 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.

    Example of the disaster

    Plugin’s error? Browser’s error? I don’t understand :/

    Thanks and Regards. Excuse me for my ‘english’ is very bad.

    • Phil March 15, 2012 at 2:21 pm

      Seems fine to me, what specifically is the problem?

      • Mr. Raymon March 15, 2012 at 4:09 pm

        You’ve arrived come to the end of the page? The menu on the left get stuck at a point and not advancing.

        I discovered the problem. I think it the problem is the plugin. If there is a lot of content on the webpage not working correctly.

        I think it’s best to make a preloader of the content/webpage and jQuery/plugins.

      • Mr. Raymon March 24, 2012 at 1:36 am

        I need help with this and you don’t respond to my problem.

        Please help me!

        Thanks & Regards

      • Phil March 24, 2012 at 6:40 am

        Dude, I really don’t see what your problem is. Only thing that doesn’t look right to me is the jitter where you have set the animation speed to 0.

        The plugin is here for people to use, for free. I do my best to help people with it but I don’t offer support.

  11. Pingback: Sticky sidebar jQuery plugin - CodeVisually

  12. Pingback: 2012년 3월 16일 it 기술 동향 |

  13. Navin Kunwar March 21, 2012 at 10:57 am

    Seems cool, i will try it. thanks Phil :)

  14. Pingback: ECCUBE おしゃれなECサイトのスクロールする買い物カゴの設置 | 戯れ問答

  15. Pingback: Web developer tools | Simone Alati (quimo)

  16. 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?

    • Phil March 30, 2012 at 4:27 pm

      Not entirely sure I understand what you want to achieve. The sticky will work off of the amount the page is scrolled + the amount of padding you set in the options, when constrained the sticky box element won’t scroll any further than the bottom of the parent div. Set the height of your container before you init the sticky box and all should work fine.

  17. Pingback: Tweet Heat - The hottest Tweets of the Month [March 2012] | Inspired Magazine

  18. Pingback: 40 Recently Released jQuery Plugins

  19. Pingback: 40 Recently Released jQuery Plugins | Easy jQuery | Free Popular Tips Tricks Plugins API Javascript and Themes

  20. Pingback: 40 Recently Released jQuery Plugins | auto

  21. 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…

  22. Pingback: 40+ Recently Released jQuery Plugins | Takula

  23. Pingback: 22 Excellent & Recent jQuery Plugins « The Creative Project

  24. Pingback: 40 Recently Released jQuery Plugins

  25. 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.

  26. SAiNT April 26, 2012 at 10:15 am

    what an amazing plugin! exactly what i needed

  27. 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.

  28. 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.

    • Phil May 11, 2012 at 12:05 pm

      ‘destroy’ will kill the plugin completely and remove all event listeners. Use ‘remove’ instead and then reapply the plugin to the element once resized back to two columns.

  29. 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 :)

    • Phil May 11, 2012 at 12:02 pm

      Thanks Thomas, can you send me a link to look at? I can only guess that a variable also named $sb is being leaked somewhere. Your fix is fine although this.data should work as the array “should” hold already initialised jQuery objects.

      • Thomas May 11, 2012 at 5:47 pm

        Unfortuately, no, as I’m designing this website for work on localhost. Even when it is published, it is only on an intranet.

        I did recreate a test with JQueryUI but it works just fine. So, I think you’re right as the variable $sb must be appearing somewhere else as that is the only explanation I can think of as to why .data() doesn’t work.

        This led me to search through all the solution’s linked files but I couldn’t find a reference to $sb. Its really strange.

        If you say ($this).data() works just fine, then I’ll just stick with that.

      • Thomas May 11, 2012 at 5:50 pm

        Last Sentence Correction: $(this).data(“stickySB”) :)

← Older Comments

Leave a Reply

Your email address will not be published. Required fields are marked *

*

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>

Recommended reading

  • Ben Nadel Articles on “obsessively thorough web development” with Coldfusion and jQuery
  • Christian Heimann's blog – Wait till I come!! Articles on web development
  • David Walsh Articles around PHP, CSS, MooTools, jQuery and just about everything else
  • John Resig Javascript programmer and the creator of the jQuery library
  • Mary Lou (Codrops) Stunning designs with lots of cool jQuery effects

Photostream

  • Loading photostream from Flickr...