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

95 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

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