Подскажите, пожалуйста как сделать так чтобы при клике на одино меню аккордиона, закрывались все остальные, но оставался только тот на который ты нажимаешь
Вот код:
<div class="block">
<ul class="accordion__list">
<li class="accordion">
<button class="accordion__control" aria-expanded="false">
<span class="accordion__title">How can I get into Meta RL?</span>
<div class="accordion__icon">
<div class="plus-height"></div>
<div class="plus-width"></div>
</div>
</button>
<div class="accordion__content" aria-hidden="true">Lorem ipsum, dolor sit amet consectetur adipisicing elit. Velit magni autem delectus eveniet sint! Modi doloribus facere blanditiis ipsum nulla architecto temporibus. Repellat distinctio quos praesentium totam sunt ratione? Consectetur.</div>
</li>
<li class="accordion">
<button class="accordion__control" aria-expanded="false">
<span class="accordion__title">When is the NFT launch?</span>
<div class="accordion__icon">
<div class="plus-height"></div>
<div class="plus-width"></div>
</div>
</button>
<div class="accordion__content" aria-hidden="true">Lorem ipsum, dolor sit amet consectetur adipisicing elit. Velit magni autem delectus eveniet sint! Modi doloribus facere blanditiis ipsum nulla architecto temporibus. Repellat distinctio quos praesentium totam sunt ratione? Consectetur.</div>
</li>
<li class="accordion">
<button class="accordion__control" aria-expanded="false">
<span class="accordion__title">Where can I get a WAVE NFT?</span>
<div class="accordion__icon">
<div class="plus-height"></div>
<div class="plus-width"></div>
</div>
</button>
<div class="accordion__content" aria-hidden="true">Lorem ipsum, dolor sit amet consectetur adipisicing elit. Velit magni autem delectus eveniet sint! Modi doloribus facere blanditiis ipsum nulla architecto temporibus. Repellat distinctio quos praesentium totam sunt ratione? Consectetur.</div>
</li>
</ul>
</div>
li {
list-style: none
}
.accordion__list {
width: 578px;
}
.accordion__control {
display: flex;
align-items: center;
justify-content: space-between;
padding: 0 20px 0 30px;
width: 100%;
height: 72px;
border: none;
background-color: #222;
cursor: pointer;
}
.accordion__title {
font-family: DMMono;
font-weight: 400;
font-size: 16px;
line-height: 24px;
color: #fff;
}
.accordion__icon {
position: relative;
width: 16px;
height: 18px;
}
.plus-height {
position: absolute;
left: 50%;
transform: translateX(-50%);
width: 2px;
height: 18px;
background-color: #fff;
transition: 0.4s;
}
.plus-height.active {
width: 16px;
}
.plus-width {
position: absolute;
top: 50%;
transform: translateY(-50%);
width: 16px;
height: 2px;
background-color: #fff;
transition: 0.4s;
}
.plus-width.active {
height: 16px;
}
.accordion__content {
max-height: 0;
opacity: 0;
overflow: hidden;
will-change: max-height;
transition: all 0.5s ease-out;
box-sizing: content-box;
padding: 0 35px 0 31px;
font-family: Inter;
font-weight: 400;
font-size: 12px;
line-height: 19px;
color: #c4c4c4;
background-color: #222;
}
.accordion__content.active {
opacity: 1;
padding: 0 35px 29px 31px;
}
document.addEventListener('DOMContentLoaded', () => {
const accordions = document.querySelectorAll('.accordion');
accordions.forEach(el => {
el.addEventListener('click', (e) => {
const self = e.currentTarget;
const control = self.querySelector('.accordion__control');
const content = self.querySelector('.accordion__content');
const pluswidth = self.querySelector('.plus-width');
const plusheight = self.querySelector('.plus-height');
content.classList.toggle('active');
pluswidth.classList.toggle('active');
plusheight.classList.toggle('active');
self.classList.toggle('active');
if (self.classList.contains('active')) {
control.setAttribute('aria-expanded', true);
content.setAttribute('aria-hidden', false);
content.style.maxHeight = content.scrollHeight + 'px';
} else {
control.setAttribute('aria-expanded', false);
content.setAttribute('aria-hidden', true);
content.style.maxHeight = null;
}
});
});
});