Blog

November 3rd, 2009
jquery

Horizontal Sliding Panel

After my last few post’s on jQuery I am getting the hang of it. I want to make a jQuery sliding panel but make it a little different. I always see tutorials on sliding panels pretty much all coming from the top and sliding down into view. I don’t know about you but that’s pretty boring now, the best one I’ve seen recently is by Jon Phillips click here to view. It appears from a point behind the button then returns to that point. I want mine a bit like this but I want it to keep its fixed height as it slides into view from the left then slide back.

This has been updated please click here for the latest version

Click here to view the demo

The HTML

1
2
3
4
5
6
7
8
9
10
<div id="panel"> <!--the hidden panel -->
       <div class="content">
              <!--insert your content here -->
       </div>	
</div>
<!--if javascript is disabled use this link-->
	<a href="/about.html" onclick="return()">
              <div id="tab"> <!-- this will activate your panel. -->
              </div> 
         </a>

As usual its nice and simple to set up and understand the HTML. You can insert your content now or you can wait till the end, up to you. I used a mixture of images and text in my example. If the user does not have Javascript enabled we need to send them to the about page otherwise the tab is pointless.

The CSS

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
#tab {
	width:50px;
	height:150px;
	position:fixed;
	left:0px;
	top:100px;
	display:block;
	cursor:pointer;
}
#panel {
	position:fixed;
	left:0px;
	top:50px;
	background-color:#999999;
	height:500px;
        display:none;/*hide the panel if Javascript is not running*/
}
#panel .content {
	width:290px;
	margin-left:70px;
}

Above is the basic styling for the effect. You don’t have to use the fixed property but I wanted the panel to look like it was attached the the side of the window, this might not be your taste so you can change it to absolute. If you have basic CSS knowledge all the above should be pretty easy to understand.

The Jquery

Im going to dive straight into my version with the call back functions.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
$(document).ready(function(){
 
   $("#panel, .content").hide(); //hides the panel and content from the user
 
   $('#tab').toggle(function(){ //adding a toggle function to the #tab
      $('#panel').stop().animate({width:"400px", opacity:0.8}, 500, function() {//sliding the #panel to 400px
	  $('.content').fadeIn('slow'); //slides the content into view.
	  });  
   },
   function(){ //when the #tab is next cliked
   $('.content').fadeOut('slow', function() { //fade out the content 
      $('#panel').stop().animate({width:"0", opacity:0.1}, 500); //slide the #panel back to a width of 0
	  });
   });
 
});

I have not added comments to my last scripts but I really think it helps show what each line does. Its all explained above, you can change a few of the values but be careful when doing so, I found if you took out the opacity it was not as smooth on the sliding, so just make sure each time you change something your check your browser.

I mentioned that I used a callback function, this runs an effect once another has finished. I found with a lot of the jquery sliding panels out there they didn’t seem to care for the content. It would get squashed as the panel was sliding back, to me that does not look right. I used the callback function to handle this, once the panel had finished sliding into view the content faded in and when you closed it the content would fade then the animation of the panel would happen. I really liked this, very easy to achieve as well. Just make sure you close everything off as you go along.

In my example I have added shadows and round corners to make it look better. So here are the last finishing touches.

Last bit of styling

You can add all this to the #tab and the #panel to make it look like a little nicer.

1
2
3
4
5
6
7
8
9
/*Rounded corners*/
	-moz-border-radius-topright: 20px;
	-webkit-border-top-right-radius: 20px;
	-moz-border-radius-bottomright: 20px;
	-webkit-border-bottom-right-radius: 20px;
/*Drop shadow*/
	filter:progid:DXImageTransform.Microsoft.DropShadow(sProperties);
	-webkit-box-shadow: 1px 1px 5px #000;  
	-moz-box-shadow: 1px 1px 5px #000;

I hope this has worked and you like it. Please use it freely across your sites, I would love to see it.

Related Posts

Wordle tool
You sometimes see this effect in films, I think vacancy was one that used t...
Creating Multiple Widget Areas
After finally getting round to redesigning this website using WordPress as ...
Animating with webkit CSS3
I have been looking into webkit CSS3 lately as I had to produce some animat...

33 Comments

  1. Suraj
    March 9, 2010

    Hi Kevin,
    Thanks for the great tutorual on the horizontal sliding panel – a lifesaver! Just one question: how do I make the panel slide out from right to left, anchored to the right side of the screen?
    Hope you can help.
    Thanks, Suraj

  2. admin
    March 9, 2010

    Hi, Glad you liked it.

    To answer your question, just edit the CSS positioning, where it has left:0; in #panel and #tab change left for right. Then its just a case of changing the positioning of the rounded corners to make it look right.

    Hope that’s what you were after, would love it see the site you are using it on.

    Kevin.

  3. anar
    March 16, 2010

    excellent! Thanks!

  4. Jon Phillips
    March 18, 2010

    Hey Kevin, that’s pretty sweet! Thanks for mentioning my sliding panel (which I’m re-working now) :)

  5. Lindsay Kay
    May 22, 2010

    Crisp delivery Kevin – nice terse code, instantly usable.

    cheers!

  6. David
    June 14, 2010

    Hi i have no idea why this isn’t working?

  7. admin
    June 14, 2010

    Looking at your site, you don’t seem to be using my code…

  8. rezvan
    July 20, 2010

    Hi kevin,thanks for the great sliding panel,I have a question.I want to put button in front of panel ,and slide both together ,what should I do??

  9. admin
    July 23, 2010

    Do you mean you want to add a button in to the slider? If so just add the button into the content div.

  10. rezvan
    July 24, 2010

    I want about us div in your demo, slide with panel. it asked in this forum,but I can’t understand his answer .please guide me if you can,I need it!!
    http://stackoverflow.com/questions/2665368/jquery-animating-left-position-of-absolutely-positioned-div-when-sliding-pane/3279370#3279370

    if you know about it please put you code here for me,
    thanks in advance.

  11. lorenzo
    August 29, 2010

    hi Kevin, this is great info, i am trying to implement it on a wordpress site. can you think of any reason why this system is only working for firefox for my blog? (i’m on a testing environment so i can’t actually send you a link), but firefox works perfectly, IE, chrome, opera and safari all ignore the onclick event.

    i’m totally puzzled because the code is lifted from your example and works in a standalone page and in the wp only in firefox… any suggestion will be very welcome!

    how would you change the code to do a mouseover instead?

  12. Paul
    September 17, 2010

    This is just what I was looking for. I’m implementing this to have 4 tab button for different items to slide out. Only problem is (I think like the person above) I want the button (tab div) to slide out with the panel, as if it were part of the panel. If I add the the tab or panel div to the content it becomes invisible obviously. Do you have any suggestions on how I may achieve this? I’ve playing with it for a while.
    Thanks in advance.

  13. Paul
    September 17, 2010

    Oops, I meant add the tab to the panel or content div.

  14. admin
    September 17, 2010

    @Paul have you tried adding .animate to the jquery and animating it to the left the width of your panel? If that makes any sense i think it should work know problem, I will work on it today and post the code.

    @lorenzo sorry its not working in other browsers, I will go through it today and make sure I haven’t missed a , or ; somewhere that’s usually the cause.

  15. admin
    September 17, 2010

    @Paul

    try this you maybe have to play with the timing

    $(document).ready(function(){

    $(“#panel, .content”).hide(); //hides the panel and content from the user

    $(‘#tab’).toggle(function(){ //adding a toggle function to the #tab
    $(‘#tab’).stop().animate({left:”400px”, opacity:0.8},500);
    $(‘#panel’).stop().animate({width:”400px”, opacity:0.8}, 500, function() {//sliding the #panel to 400px
    $(‘.content’).fadeIn(‘slow’); //slides the content into view

    });
    },
    function(){ //when the #tab is next cliked
    $(‘.content’).fadeOut(‘slow’, function() { //fade out the content
    $(‘#tab’).stop().animate({left:”-400px”, opacity:0.1},500);
    $(‘#panel’).stop().animate({width:”0″, opacity:0.1}, 500); //slide the #panel back to a width of 0
    });
    });

    });

  16. Paul
    September 20, 2010

    Thanks for that. It worked perfectly. This is the finished code if anyone is interested. My example is aligned to the right though; not the left.

    $(function(){
    $(“#panel, .content”).hide():;

    $(‘#tab’).toggle(function(){
    $(‘#tab’).stop().animate({right:”840px”, opacity:1},500);
    $(‘#panel’).stop().animate({width:”840px”, opacity:1}, 500, function() {
    $(‘.content’).fadeIn(‘slow’); //slides the content into view.
    });
    },
    function(){ //when the #tab is next cliked
    $(‘.content’).fadeOut(‘slow’, function() {
    $(‘#tab’).stop().animate({right:”0px”, opacity:1},500);
    $(‘#panel’).stop().animate({width:”0″, opacity:1}, 500);
    });
    });

    });

  17. Juan
    October 7, 2010

    I hope you still look at this blog post, I have a problem and Im wondering, why is that the panel hides behind the image, is there a way to make it cover the image?
    I’m kind of new with jquery.

    Greetings

  18. admin
    October 8, 2010

    It will be based on 2 things.
    1. where in the sliding panel placed in your HTML?
    2. do you have any z-index properties on your images?

    Have a play with the position of the Sliding panel in your HTML, im sure that will sort it.

  19. Robe
    October 20, 2010

    Hi Kevin! Tnx about your script. I tried to upgrade jquery-min.js to the latest version (1.4.2), but works only with 1.3.2. Any idea?

  20. robe
    October 20, 2010

    Hi Kevin, thanks for this tutorial.
    is it possible implement 2 or more button with relative sliding panel?

  21. admin
    October 21, 2010

    Sure is…

    You can apply it to as many buttons as you like, just create a new #tab (ie #anotherTab) style in your css, add in the new HTML (same as before) and then assign the id to your jquery code. Hope that makes sense.

    I will look into it for you. But it should work with the latest jquery.

  22. Chely
    October 26, 2010

    Hi Kevin, your tutorial really helped me =), I have a question: is there any way that the tab could slide on the load event of the page?

    Thanks again =)

  23. Juan
    November 19, 2010

    Regarding my post on the 7th of October, it was the z-index properties :)
    thanks

  24. admin
    November 23, 2010

    @Chely Glad I could help, having you tried using:

    $(window).load(function () {
    // run code
    });

  25. hedgehog
    December 1, 2010

    Hi there!
    I really appreciate your code!
    I’m just wondering why my panel (which is smaller than yours) doesnt get a “slide” effect but it’s just fading Oo
    Im kinda lost :P
    Thanks in advance :)

  26. admin
    December 2, 2010

    Without seeing the code I can only guess. Im thinking it could either be that your div id is not the same as the div id in the jQuery so its not applying the effect. Or its the positioning in the CSS of the panel div.

    Sorry I cant be much help just hard to fix the problem without seeing what you are seeing.

    Do you have a link to your site?

  27. hedgehog
    December 2, 2010

    eggiog.net/flv (it’s a custom template under heavy development :P )
    i tried using position:fixed on the panel div, but it just wont work as well, so i took it off since it requires some extra calculations to fit in my template…
    thank you for your help!

  28. Dwaynne
    January 31, 2011

    Hi, Kevin -

    Code works great in 1.3.2, but 1.4.1 upwards it breaks. Do you have an update lying around? Spent the last 24 hours troubleshooting and that’s the only answer I came up with after switching libraries.

    I also see that previous commenter had the same issue…

    I need to use 1.4.2 for some other interface effects.

    Appreciate any assistance you can levy.

  29. admin
    February 1, 2011

    Hi Dwaynne, sorry your having this trouble, how about with the new jQuery 1.5 update? I will have a look when I get a free moment.

  30. Ron
    March 9, 2011

    Hi – Sorry, confirmed that it breaks using jQuery 1.5.1. Had it working perfectly too but needed to add another script (which won’t work with 1.3.2!) so I’m screwed either way : (

    Of course there is a possibility that I messed up your script by modifying it. I needed to make the panel work on multiple objects on the same page so I passed a unique ID to the script. If it helps here is what I did:

    $(document).ready(function(){

    $(“#panel, .content”).hide(); //hides the panel and content from the user

    $(‘.submitform’).toggle(function(){ //adding a toggle function to the #tab
    var element = $(this);
    var Id = element.attr(“id”);
    $(‘#panel’+Id).stop().animate({width:”320px”, opacity:0.8}, 250, function() {//sliding the #panel to 400px
    $(‘#content’+Id).fadeIn(‘fast’); //slides the content into view.
    });
    },
    function(){ //when the #tab is next cliked
    var element = $(this);
    var Id = element.attr(“id”);
    $(‘#content’+Id).fadeOut(‘fast’, function() { //fade out the content
    $(‘#panel’+Id).stop().animate({width:”0″, opacity:0.1}, 250); //slide the #panel back to a width of 0
    });
    });

    });

    Thanks!

  31. admin
    March 18, 2011

    We have an update people to the problems in the latest version of jQuery view it here http://www.iamkreative.co.uk/design/horizontal-sliding-panel/

  32. Ryan
    October 12, 2011

    I can’t seem to get the script to work with any version of jQuery other than 1.3.2. I see you say there was an update to the latest version and I was wondering if you could provide the correct link?

    Thanks for the help!

  33. Sam
    April 16, 2012

    Hey, your demo doesn’t work for me, I use Firefox.

Leave a comment