
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 < 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.
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.
@Ben – Glad you have been enjoying the blog.
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?
@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.
Okay I see… but in Joomla, modules are usually coded in PHP… would the same coding above still work?
@Jene – Joomla uses PHP and HTML, so this technique should work.
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.
@Jene – Not a problem just use the contact form and let me know exactly you need help with.
valuable info thanks
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
@Michael – You could probably achieve what you are wanting using unbind, see: http://docs.jquery.com/Events/unbind.
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?
@Michael – Sorry, I haven’t used unbind before either, so I can’t help much more :)
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 ?
@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.