I have two div containers with content in them and two buttons outside the container like so:
<div id="containers">
<div id="container_1">
awesome container 1 content
</div>
<div id="container_2">
awesome container 2 content
</div>
</div>
<div id="container_1_button">
Click me to open container 1
</div>
<div id="container_2_button">
Click me to open container 2
</div>
I need to be able to animate width and left: to give the effect of the content sliding from the left when clicked. But only one can be open at a time. psuedo code would be: IE: if Container 1 width > 0 set width:0, set left:0 and the same for container 2. So that way if someone clicks on button 2 while container 1 is open it collapses container 1 and then opens container 2 and vice versa.
I have achieve the animation of the width/left pos on one element by using the following jquery:
//the Jquery collapsible left hand sidebarfor mapit
$('#map_sidebar_button').toggle(function(){
collapseSidebarLeft();
$('#sidebar_left_map').animate({width:0}, 'fast');
$('.button_container').animate({left:0}, 'fast');
$('#map_sidebar_button').removeClass('sidebar_left_button_toggle');
},function(){
$('#sidebar_left_map').animate({width:260}, 'fast');
$('.button_container').animate({left:260}, 'fast');
// add a class to change the background coloring and border of the button
$('#map_sidebar_button').addClass('sidebar_left_button_toggle');
});
You can see an example of the site that i need this implemented on here:
http://demo.mapitusa.com the left sidebar buttons..
My approach would be to use the
.each()method and bind a (single)clickto each button with the sameclass, and depending on which one you clicked (based on its index) then select what awesome container to display.In this scenario, both awesome containers would have the same
classtoo and will be hidden from visibility using a negative margin of the same value of theirwidth, why? because in this way they won’t show the weird effect produced while animating thewidth(the elements inside the container will look like floating and arranging themselves during the animation) … so using this html:the css for both containers and parent
then the jQuery where we assign a single
clickto the same selector (the sharedclassof both buttons)Notice that we use the
completecallback option of.animate()so the next animation will be executed until the first one has been completed, in this case the second container will show up only until the first one collapses.You can see it working here for better understanding of the effect.