Sticky Sidebar jQuery plugin

Posted on March 21, 2011 by Phil   176 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.

176 Responses So Far

← Older Comments
  1. Sherri September 11, 2012 at 5:12 pm

    Hi there,
    Just wondering if you’ve made this into a WordPress plugin?? That would be AWESOME!!
    I’m working on a site that the customer has requested such a feature.

  2. Pingback: 8 Cool jQuery Sticky Scroll Plugins | Graphic & Web Design Inspiration + Resources

  3. Pingback: 25 Essential jQuery Plugin ToolKit for Developers

  4. Wesley Burden October 10, 2012 at 9:58 am

    Phil – thanks for this wicked little plugin.

    I’ve been having issues with containing the sidebar in it’s parent element and I believe 960 grid is to blame…

    The stick sidebar was ignoring the parent element and continuing on beyond and over the site footer. After much fiddling it seems that 960gs is to blame. Once I removed the 960gs styles and replaced with my own it works fine.

    Not exactly sure why 960gs causes this issue, I think either something to do with the clearfix it applies or the fact that it makes grid elements display:inline.

  5. Pingback: Best jQuery Plugins for Web Development

  6. Pingback: 25 Fabulous Essential jQuery Plugin ToolKit for Developers | Smashing Spy

  7. Guido P. November 19, 2012 at 3:48 am

    Hi there,

    very nice plugin. Many thanks for that, but i got one little problem.

    Imagine following structure:

    ################
    # fixed header # <<– height: 100px
    ################
    # main #
    # col1 | col3 #
    ################
    # footer #
    ################

    Acutally my submenu disappears from the fixed positionied header and when the scrolling reaches top: 0 – the submenu is getting repositioned correctly.

    What i want to have is that repositioning of the sticky element – my submenu – starts at top: 100

    How can i tell the script that theres a fixed 100px height header, and repositioning shold start at this offset??

    With best regards
    guido

  8. Pingback: jQueryでスクロールについてくるメニューの設置方法 | お気軽無料ホームページ作成ガイド

  9. Pingback: 2012年度优秀jQuery插件精选100个 | 奶油天空

  10. Gilad December 29, 2012 at 6:04 pm

    Thanks a lot, this plugin look very good.
    I have a question – if I want to limit the scrolling until it gets to my footer (and to scroll over it), how can I do that ?
    Thanks a lot !

  11. Pingback: 每个web开发者必备的40个jquery插件 | iluther-专注web技术iluther-专注web技术

  12. Pingback: Project: Sticky Sidebar | Skillcrush

  13. Chris January 30, 2013 at 6:37 pm

    Hello,
    this is a great plugin!
    I was wondering if it’s possible to disable the sticky sidebar in case a #link is clicked?

    So I would have those 2 options:

    #1: User scrolls page -> sticky sidebar plugin is active

    #2: User clicks a link -> sticky sidebar is disabled till the page is scrolled again (#1)

    I created a little fiddle for this scenario, but I’m not sure how to do that best.
    http://jsfiddle.net/CeUcJ/

    I would be very thankful for your help!

  14. Pingback: 25 Essential jQuery Plugin ToolKit for Developers - Baqar Abbas

  15. Pingback: Week of 4/13/12 – TDAS Creative Wiki

  16. Pingback: 分享9个滚动粘卷jQuery Sticky Scroll插件

  17. Pingback: Sticky Scrolls - 12 Free Plugins | ViewLikeUs - Check Sites in Various Resolutions! | Blog

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