I need to make a horizontal accordion these days.
You can see that there is a small "jump" of the color boxes on right hand side when clicking the color box. I don't know why the small "jump" occurs.
I then use animate instead of hide and show to try to solve the problem.
Even if the width animates to 0px, the element still has a few pixels, so I try to hide it after the animation. But the hide() function doesn't do anything. Why?
During debugging, I find that the alert("good") runs right before the animation ends. Why?
Thank you for answering my questions.
Here is the code:
<html>
<head>
<style type="text/css">
.accordion { padding: 3px; margin: 0px; float: left; width: 300px;}
.accordionTitle { cursor: pointer; padding: 3px; margin: 0px; float: left;width:20px; height:150px; }
.clearBoth { clear: both; }
</style>
<script src="jquery-1.3.2.min.js"></script>
<script type="text/javascript" >
$(function() {
var elementToTransform = ".accordion"
$(elementToTransform).hide();
var currentAccordionPanel = ($(".currentAccordionPanel").show())[0];
$(".accordionTitle").click(function() {
if ($(this).next(elementToTransform)[0] != currentAccordionPanel)
{
$(currentAccordionPanel).hide(400);
$(this).next(elementToTransform).show(400);
/*$(currentAccordionPanel).animate({width:"0px"},{duration:200,queue:false},{},function(){alert("good")});
$(this).next(elementToTransform).animate({width:"300px"},{duration:200,queue:false});
$(currentAccordionPanel).hide(); // the element still has few pixels even width animates to 0, so I hide it
*/
currentAccordionPanel = $(this).next(elementToTransform)[0];
}
})
});
</script>
</head>
<body>
<div style="width:800px;">
<div id="cat1">
<div class="accordionTitle" style="background:blue">1</div>
<div class="accordion currentAccordionPanel" id="p1">content 1</div>
</div>
<div id="cat2">
<div class="accordionTitle" style="background:red">2</div>
<div class="accordion" id="p2">content 2</div>
</div>
<div id="cat3">
<div class="accordionTitle" style="background:yellow">3</div>
<div class="accordion" id="p3">content 3</div>
</div>
<div id="cat4">
<div class="accordionTitle" style="background:green">4</div>
<div class="accordion" id="p4">content 4</div>
</div>
<div id="cat5">
<div class="accordionTitle" style="background:red">5</div>
<div class="accordion" id="p5">content 5</div>
</div>
<div class="clearBoth"></div>
</div>
</body>
</html>