document.querySelectorAll('.sidebarCategory').forEach(el =>{
el.addEventListener('click', e =>{
let sub = el.nextElementSibling
if(sub.style.maxHeight){
el.classList.remove('opened')
sub.style.maxHeight=null
}
else{
document.querySelectorAll('.sidebarSubcategories').forEach(element => {
element.style.maxHeight=null
element.previousElementSibling.classList.remove('opened')
})
el.classList.add('opened')
sub.style.maxHeight = sub.scrollHeight + "px";
}
})
})
document.querySelectorAll('.sidebarSubcategories > div:not(.sidebarSubSubcategories)').forEach(el =>{
el.addEventListener('click', e =>{
let sub = el.nextElementSibling
if(sub!=null && sub.classList.contains('sidebarSubSubcategories'))
{
if(sub.style.maxHeight){
sub.style.maxHeight=null
sub.style.marginBottom=0
sub.style.padding='0 30px'
}
else{
sub.style.marginBottom='15px'
sub.style.padding='15px 30px'
sub.style.maxHeight = sub.scrollHeight + 30 + "px"
el.parentNode.style.maxHeight=el.parentNode.style.maxHeight.slice(0,el.parentNode.style.maxHeight.length-2) + sub.scrollHeight + 30 + "px"
}
}
})
})
.main{
position: fixed;
top: 50%;
left: 50%;
transform: translate(-50%,-50%);
width: 400px;
height: 300px;
overflow-y: auto;
background-color: aquamarine;
}
.sidebarCategory{
color: white;
position: relative;
height: 60px;
min-height: 60px;
display: flex;
align-items: center;
justify-content: flex-start;
font-size: 20px;
flex-wrap: wrap;
font-weight: 600;
fill: white!important;
cursor: pointer;
padding-left: 15px;
transition: 0.4s;
}
.sidebarCategory.opened{
background-color: rgba(3, 63, 109, 0.588);
}
.sidebarSubcategories{
max-width: 100%;
position: relative;
background-color: rgba(3, 63, 109, 0.588);
max-height: 0;
transition: 0.4s;
overflow-y: hidden;
}
.sidebarSubcategories > div:not(.sidebarSubSubcategories){
max-height: 45px;
/* height: 45px; */
position: relative;
min-height: 45px;
color: white;
cursor: pointer;
overflow-y: hidden;
display: flex;
align-items: center;
justify-content: flex-start;
flex-wrap: wrap;
padding: 0 30px;
border-bottom: 1px solid var(--blue);
}
.sidebarSubSubcategories{
max-height: 0;
overflow-y: hidden;
transition: 0.4s;
background-color: white;
border-radius: 5px;
display: flex;
justify-content: center;
align-items: center;
flex-wrap: wrap;
margin: 0 15px;
}
.sidebarSubSubcategories > div{
cursor: pointer;
transition: 0.2s;
display: flex;
justify-content: center;
border-radius: 5px;
align-items: center;
padding: 10px;
width: 49%;
}
.sidebarSubSubcategories > div:hover{
background-color: #f3f3f3;
}
<body>
<div class="main">
<div class="sidebarCategory">
Lorem ipsum dolor sit amet.
</div>
<div class="sidebarSubcategories">
<div>
Subcategory
</div>
<div class="sidebarSubSubcategories">
<div>Sub Subcategory</div>
<div>Sub Subcategory</div>
<div>Sub Subcategory</div>
<div>Sub Subcategory</div>
</div>
<div>
Subcategory
</div>
<div>
Subcategory
</div>
<div>
Subcategory
</div>
</div>
</div>
It appears that the first drop-down does not close smoothly when interacting with the second drop-down menu. This issue arises from nested accordions where changing the max-height property causes a problem in the transition of the outer accordion on interaction with the inner accordion.</p>
</div></questionbody>
<exquestionbody>
<div class="question">
<p><div>
<div>
<pre class="lang-js"><code>document.querySelectorAll('.sidebarCategory').forEach(el =>{
el.addEventListener('click', e =>{
let sub = el.nextElementSibling
if(sub.style.maxHeight){
el.classList.remove('opened')
sub.style.maxHeight=null
}
else{
document.querySelectorAll('.sidebarSubcategories').forEach(element => {
element.style.maxHeight=null
element.previousElementSibling.classList.remove('opened')
})
el.classList.add('opened')
sub.style.maxHeight = sub.scrollHeight + "px";
}
})
})
document.querySelectorAll('.sidebarSubcategories > div:not(.sidebarSubSubcategories)').forEach(el =>{
el.addEventListener('click', e =>{
let sub = el.nextElementSibling
if(sub!=null && sub.classList.contains('sidebarSubSubcategories'))
{
if(sub.style.maxHeight){
sub.style.maxHeight=null
sub.style.marginBottom=0
sub.style.padding='0 30px'
}
else{
sub.style.marginBottom='15px'
sub.style.padding='15px 30px'
sub.style.maxHeight = sub.scrollHeight + 30 + "px"
el.parentNode.style.maxHeight=el.parentNode.style.maxHeight.slice(0,el.parentNode.style.maxHeight.length-2) + sub.scrollHeight + 30 + "px"
}
}
})
})
.main{
position: fixed;
top: 50%;
left: 50%;
transform: translate(-50%,-50%);
width: 400px;
height: 300px;
overflow-y: auto;
background-color: aquamarine;
}
.sidebarCategory{
color: white;
position: relative;
height: 60px;
min-height: 60px;
display: flex;
align-items: center;
justify-content: flex-start;
font-size: 20px;
flex-wrap: wrap;
font-weight: 600;
fill: white!important;
cursor: pointer;
padding-left: 15px;
transition: 0.4s;
}
.sidebarCategory.opened{
background-color: rgba(3, 63, 109, 0.588);
}
.sidebarSubcategories{
max-width: 100%;
position: relative;
background-color: rgba(3, 63, 109, 0.588);
max-height: 0;
transition: 0.4s;
overflow-y: hidden;
}
.sidebarSubcategories > div:not(.sidebarSubSubcategories){
max-height: 45px;
/* height: 45px; */
position: relative;
min-height: 45px;
color: white;
cursor: pointer;
overflow-y: hidden;
display: flex;
align-items: center;
justify-content: flex-start;
flex-wrap: wrap;
padding: 0 30px;
border-bottom: 1px solid var(--blue);
}
.sidebarSubSubcategories{
max-height: 0;
overflow-y: hidden;
transition: 0.4s;
background-color: white;
border-radius: 5px;
display: flex;
justify-content: center;
align-items: center;
flex-wrap: wrap;
margin: 0 15px;
}
.sidebarSubSubcategories > div{
cursor: pointer;
transition: 0.2s;
display: flex;
justify-content: center;
border-radius: 5px;
align-items: center;
padding: 10px;
width: 49%;
}
.sidebarSubSubcategories > div:hover{
background-color: #f3f3f3;
}
<body>
<div class="main">
<div class="sidebarCategory">
Lorem ipsum dolor sit amet.
</div>
<div class="sidebarSubcategories">
<div>
Subcategory
</div>
<div class="sidebarSubSubcategories">
<div>Sub Subcategory</div>
<div>Sub Subcategory</div>
<div>Sub Subcategory</div>
<div>Sub Subcategory</div>
</div>
<div>
Subcategory
</div>
<div>
Subcategory
</div>
<div>
Subcategory
</div>
</div>
</div>
</body>