Accordion with Transition and Height property
HTML
<div class="accordion_holder">
<div class="accordion_item">
<div class="w3-row title" onclick="toggle_accordion(this);">
title 1 here
</div>
<div class="w3-row content">
<div class="w3-col">
content here content here<br/>content here<br/>content here<br/>content here<br/>content here<br/>content here<br/>
</div>
</div>
</div>
<div class="accordion_item">
<div class="w3-row title" onclick="toggle_accordion(this);">
title 2 here
</div>
<div class="w3-row content">
<div class="w3-col">
content here<br/>content here<br/>content here<br/>content here<br/>content here<br/>
</div>
</div>
</div>
</div>
CSS
.accordion_holder .accordion_item {
margin-bottom: 5px;
border: solid 2px #eee;
}
.accordion_holder .title {
padding: 10px;
background-color: #eee;
}
.accordion_holder .content {
height: 0px;
overflow: hidden;
-webkit-transition: height 0.5s linear;
-moz-transition: height 0.5s linear;
-ms-transition: height 0.5s linear;
-o-transition: height 0.5s linear;
transition: height 0.5s linear;
}
JavaScript
function toggle_accordion(clicked_element) {
var father = closest_parent(clicked_element, 'accordion_item');
console.log(father);
var accordion_content = father.getElementsByClassName("content")[0];
var destination = 0;
var current_height = accordion_content.offsetHeight;
if (current_height == 0)
{
accordion_content.style.height = "auto";
destination = accordion_content.offsetHeight;
accordion_content.style.height = "0px";
}
accordion_content.style.height = destination+"px";
}
/************************************
** Find Closest Parent with Class **
***********************************/
function closest_parent (current_element, class_name) {
var parent = current_element.parentElement;
if (parent) {
if (parent.className.indexOf(class_name) >= 0)
{
//We found requested parent
return parent;
}
else
{
return closest_parent (parent, class_name);
}
}
else
{
return false;
}
}