Created
February 6, 2024 12:44
-
-
Save kaineer/64fe752d25f8d41103ebe93fad6f9164 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
let items = document.querySelectorAll('.accordeon-item-title'); | |
let openItems = []; | |
// const maxOpened = document.querySelector(".accordeon").dataset.tabsLimit; | |
const maxOpened = () => document.querySelector(".accordeon").dataset.tabsLimit; | |
items.forEach((item, index) => { | |
item.addEventListener('click', () => { | |
if (item.parentElement.classList.contains("accordeon-item--open")) { | |
let indexInOpenItems = openItems.indexOf(index); | |
if (indexInOpenItems !== -1) { | |
openItems.splice(indexInOpenItems, 1); | |
} | |
item.parentElement.classList.remove("accordeon-item--open"); | |
} else { | |
openItems.push(index); | |
item.parentElement.classList.add("accordeon-item--open"); | |
if (maxOpened() < 1) { | |
return; | |
} | |
if (openItems.length > maxOpened()) { | |
let firstOpenedIndex = openItems.shift(); | |
items[firstOpenedIndex].parentElement.classList.remove("accordeon-item--open"); | |
} | |
} | |
}); | |
}); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment