Article / Blog

Creating A Sliding Sidebar That Follows The User

Sliding sidebars that follow the user up and down the page are great for viewing what’s in your shopping cart on e-commerce sites or for site navigation.

This tutorial is based on the menu over at www.kingtray.com.

View Final Demo.

Getting Started

First of all create a new html document and paste in the following. Make sure you link to a copy of jQuery and create a blank JavaScript file called sidebar-slider.js.

<div id="container">
<div id="content">Main Content</div>
<div id="sidebar-container">
<div id="sidebar"></div>
</div>
</div>

On a real site you would obviously have things like a header and footer, but for this tutorial they aren’t needed.

Next we need some basic styling.

#container { left: -92px; margin: 0 auto; position: relative; width: 600px; }
#content { width: 560px; background: #ececec; padding: 20px; }
#sidebar-container { position: absolute; right: -184px; top: 0; width: 171px }
#sidebar { width: 171px; height: 195px; background: #333; top: 100px; position: absolute; }

For this effect to work correctly the sidebar will be absolutely positioned and have negative space on the right and we will need to relatively position the container and add some negative space on the left, to ensure the design is centered

Now we will place the following javascript code in the sidebar-slider.js file that we created earlier.

First of all we declare a couple of variables:

var name = "#sidebar";
var sidebar_top_limit = 100;
var sidebar_top_margin = 20;
var sidebar_slide_duration = 500;
  • name” will be used to select which element will slide up and down the page.
  • sidebar_top_limit” sets how close to the top of the page the sidebar is allowed.
  • sidebar_top_margin” sets the distance from the top of the browser window the sidebar is allowed, while scrolling.
  • sidebar_slide_duration” defines the speed at which the sidebar should slide.

Now we need to add a window scroll function:

$(window).scroll(function() {
});

Inside of this we add:

offset = $(document).scrollTop() + sidebar_top_margin;
if(offset &lt; sidebar_top_limit)
offset = sidebar_top_limit;
$(name).animate({top:offset},{duration:sidebar_slide_duration,queue:false});

The first part applies the top margin for when you are scrolling.

The next part applies the limit of how close to the top of the page the sidebar can go.

The last piece of code sets up the animation, using variables we set up earlier.

View Final Demo.

And there we have it a sliding sidebar that is quick to build and easy to customize.

15 Comments

Show/Hide
  1. 12:12 pm - Apr 4th, 09

    Thanks for a simple slider. I’ve been subscribed to your blog for a month or so and I’m very impressed with what you post! Keep up the good work.

  2. Matt
    2:15 pm - Apr 4th, 09

    @Ben – Glad you have been enjoying the blog.

  3. Jene
    3:42 pm - Apr 17th, 09

    Hey Matt… Thanks for this guide and I know its old, but I;m trying to incorporate something like this into a Joomla module for custom coding… any idea how I would go about doing that?

  4. Matt
    5:49 pm - Apr 17th, 09

    @Jene – I would imagine this would work just the same as with plain HTML, as long as you put the container div and all its contents inside the body tag you should be good to go.

  5. Jene
    8:37 am - Apr 18th, 09

    Okay I see… but in Joomla, modules are usually coded in PHP… would the same coding above still work?

  6. Matt
    9:13 am - Apr 18th, 09

    @Jene – Joomla uses PHP and HTML, so this technique should work.

  7. Jene
    11:43 am - Apr 20th, 09

    Thanks for the help. And not to bug you, but do you think you could be a little more detailed in where and how these pieces of code above are placed…? We could also resume this conversation over email if thats okay with you.

  8. Matt
    11:59 am - Apr 20th, 09

    @Jene – Not a problem just use the contact form and let me know exactly you need help with.

  9. 9:52 pm - Jun 25th, 09

    valuable info thanks

  10. 5:02 pm - Nov 20th, 09

    Very cool script, I really appreciate such a great tutorial.

    I wanted to ask a quick question…is there a way to add the functionality of a “toggle” so the user can opt to stop the content from following them? Maybe a simple checkbox that causes the scrolling DIV to go hidden and reveal another DIV that is static?

    I’d appreciate any help.

    Thanks,
    Michael

  11. Matt
    5:14 pm - Nov 20th, 09

    @Michael – You could probably achieve what you are wanting using unbind, see: http://docs.jquery.com/Events/unbind.

  12. Michael
    10:43 am - Nov 21st, 09

    Hi Matt,

    Yeah that looks like it would work. I am just too inexperienced with jquery to know where to add that to your tutorial. :)

    So, basically, the unbind would remove the “effect” of the scrolling with the user?

  13. Matt
    7:01 am - Nov 23rd, 09

    @Michael – Sorry, I haven’t used unbind before either, so I can’t help much more :)

  14. sunil
    9:35 pm - Jan 7th, 10

    thanks for share it. I am a fresher designer. how can we download this file? shall you give the download link for it also pls ?

  15. Matt
    7:59 pm - Jan 9th, 10

    @sunil – I don’t have a download for it at the moment, but you should be able to “view source” and get what you need.

3 Trackbacks

Show/Hide
  1. Date:
    12:08 pm - Nov 7th, 09
    Description:

    [...] Sliding sidebars [...]

  2. Date:
    2:01 pm - Mar 21st, 10
    Description:

    [...] Creating A Sliding Sidebar That Follows The User [...]

Post a Comment

Why not join the conversation? Don't have an avatar? You might want to consider getting one.

Tasty Web Bits

To celebrate the launch of the Digg iPhone App, Digg is giving away 14 custom iPads.

Smoking Apples is giving away a couple of licenses for AppZapper 2.

AppZapper 2 has been released, and is sporting a brand new, very delicious looking UI.

Envato’s new iPhone News and App Review site, iPhone.AppStorm has launched.

Win a copy of 1Password via Mac.Appstorm.

Very cool tutorial on creating a Textured Grungy 3D Type Design over at Blog.SpoonGraphics.