Horizontal Expand Collapse Using jQuery

The verticle expand collapse can be achived using jQuery slideToggle(), but if you want to expand and collapse an element horizontally, you may have to write little more than just slideToggle(), because there’s no jQuery method for sliding element horizontally. Let’s find out how we can create a horizontal sliding element, that expands and collapse on button click.

We’ll use jQuery .animate() method, which lets us create custom animation effects on any numeric CSS property, such as width, height, or left with numeric values, which can be animated.

jQuery

Trick is to compare and change value of element CSS property “margin-left”, if we combine jQuery .animate() method here, we’ll end-up with smooth looking horizontal slide effect.

JQUERY
1234567891011121314151617181920212223

<script type="text/javascript">
$(document).ready(function() {
	var hideWidth = '-490px'; //width that will be hidden
	var collapsibleEl = $('.collapsible'); //collapsible element
        var buttonEl =  $(".collapsible button"); //button inside element
        collapsibleEl.css({'margin-left': hideWidth}); //on page load we'll move and hide part of elements
	$(buttonEl).click(function()
	{
		var curwidth = $(this).parent().offset(); //get offset value of the parent element
		if(curwidth.left>0) //compare margin-left value
		{
			//animate margin-left value to -490px
			$(this).parent().animate({marginLeft: hideWidth}, 300 );
			$(this).html('&raquo;'); //change text of button
		}else{
			//animate margin-left value 0px
			$(this).parent().animate({marginLeft: "0"}, 300 );	
			$(this).html('&laquo;'); //change text of button
		}
	});
});
</script>

Style

Since we are moving the elements left and right, our elements have fixed width of 500px here. You can change it later depending on your requirement. We’ll be hiding 80% of elements to the left; hence our button should be floated to the right, so users can click on it to expand and collapse. So here’s the CSS we need for our element.

CSS
123456789101112131415161718192021222324252627

<style type="text/css">
<!--
.collapsible {
	margin-bottom: 20px;
	background: #EBEBEB;
	border: 1px solid #CCCCCC;
	padding: 10px;
	width: 500px;
	font: 12px Arial, Helvetica, sans-serif;
	color: #333333;
}
.collapsible button {
	height: 135px;
	float: right;
	border: 1px solid #999999;
	background: #999999;
	color: #CECECE;
	margin-left: 10px;
}
.collapsible button:hover {
	background: #990000;
	border: 1px solid #990000;
}
-->
</style>

HTML

Here’s our HTML elements, which we are trying to slide (expand/collapse) horizontally. You can checkout the demo in next page or download the whole code. Don’t forget to share and comment if you find this helpful. Good luck!

HTML
12345678910

<div class="collapsible">
<button>&raquo;</button>
Contrary to popular belief, Lorem Ipsum is not simply random text.
</div>
<div class="collapsible">
<button>&raquo;</button>
Contrary to popular belief, Lorem Ipsum is not simply random text.
</div>

Download Demo

  • 7 Comments

    Add Comment
    • Sampath
      Can you please post the code from right to left.
    • PD
      Thanks.. It works for me now.
    • Prajnashree
      Hi it works fine. but i need a small favor as I am facing some issue while using this. it works fine in a html page but when I am putting inside a server tag like form tag or inside any content place holder which is having the attribute of runat="server", everything is working fine but the only thing is, it is working with double click of the button instead of single click.
    • Adrian
      Hi, I toke your code and it work partially. The problem is that it doesn't work to hide elements if the element itself is exactly near the page. I mean, if I had a div with (margin: 0;} and the html,body with {margin: 0}, on page load the element is hidden,and I can expand, but than I can't collapse again,it doesn't hide when I click on button.Can you have any suggestion ?
    • Johnson
      I need this slide right to left. how can customize your code.
      • Paul Johnson
        Hi Saran. The slider works great. I've been trying to adapt this to 'right to left' as well. Can you help?
    • Nalin
      Hi Saran, I have downloaded your code, but with html tags, div is not getting collapse (code is mentioned in below) Do you have any solution for that,because i am using tags in my page?
      12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758
      &lt;%----%&gt;
      
          &lt;%----%&gt;
      
          
              $(document).ready(function() {
                  var hideWidth = '-190px'; //width that will be hidden
                  var collapsibleEl = $('.collapsible'); //collapsible element
                  var buttonEl = $(".collapsible button"); //button inside element
      
                  //collapsibleEl.css({ 'margin-left': hideWidth }); //on page load we'll move and hide part of elements
      
                  $(buttonEl).click(function() {
                      var curwidth = $(this).parent().offset(); //get offset value of the element            
                      if (curwidth.left &gt; 0) //compare margin-left value
                      {
                          //animate margin-left value to -490px
                          $(this).parent().animate({ marginLeft: hideWidth }, 500);
                          $(this).html('&raquo;'); //change text of button                    
                      } else {
                          //animate margin-left value 0px
                          $(this).parent().animate({ marginLeft: "0" }, 500);
                          $(this).html('&laquo;'); //change text of button
                      }
                  });
              });
      
          
      
          
              .collapsible
              {
                  margin-bottom: 20px;
                  background: #EBEBEB;
                  border: 1px solid #CCCCCC;
                  padding: 10px;
                  width: 200px;
                  font: 12px Arial, Helvetica, sans-serif;
                  color: #333333;
                  display: block;
                  overflow: hidden;
              }
              .collapsible button
              {
                  height: 135px;
                  float: right;
                  border: 1px solid #999999;
                  background: #999999;
                  color: #CECECE;
                  margin-left: 10px;
              }
              .collapsible button:hover
              {
                  background: #990000;
                  border: 1px solid #990000;
              }
          
              &raquo;