This JavaScript code snippet helps you to create an accordion with smooth transition. It comes with a simple and clean interface to open/close accordion content. It opens one item at a time and close other. You can integrate this accordion for FAQs and sliding down navigations.
How to Create JavaScript Accordion with Smooth Transition
1. First of all, create the HTML structure as follows:
<section class="section_accordion"> <h1>Animated and Responsive Accordion<br><small>Pure Javascript and CSS</small></h1> <div class="accordion"> <div class="card is-active"> <a class="card__title is-active" data-toggle="collapse" href="#content1"> Lorem ipsum dolor <svg class="icon icon-keyboard_arrow_down" xmlns="http://www.w3.org/2000/svg" width="24" height="24" viewBox="0 0 24 24"> <path d="M7.41 7.84L12 12.42l4.59-4.58L18 9.25l-6 6-6-6z"/> </svg> </a> <div id="content1" class="collapse is-active" data-parent=".accordion"> <div class="card__content"> Lorem ipsum dolor sit amet, consectetur adipisicing elit. Sapiente, illo, vero. Blanditiis nam quae, maiores possimus totam doloribus aliquam, non, aliquid dolores cum distinctio porro, itaque recusandae ad aperiam voluptates! </div> </div> </div> <div class="card"> <a class="card__title" data-toggle="collapse" href="#content2"> Lorem ipsum dolor sit amet <svg class="icon icon-keyboard_arrow_down" xmlns="http://www.w3.org/2000/svg" width="24" height="24" viewBox="0 0 24 24"> <path d="M7.41 7.84L12 12.42l4.59-4.58L18 9.25l-6 6-6-6z"/> </svg> </a> <div id="content2" class="collapse" data-parent=".accordion"> <div class="card__content"> Lorem ipsum dolor sit amet, consectetur adipisicing elit. Sapiente, illo, vero. Blanditiis nam quae, maiores possimus totam doloribus aliquam, non, aliquid dolores cum distinctio porro, itaque recusandae ad aperiam voluptates! </div> </div> </div> <div class="card"> <h4 class="card__title" data-toggle="collapse" data-target="#content3"> Lorem ipsum dolor sit <svg class="icon icon-keyboard_arrow_down" xmlns="http://www.w3.org/2000/svg" width="24" height="24" viewBox="0 0 24 24"> <path d="M7.41 7.84L12 12.42l4.59-4.58L18 9.25l-6 6-6-6z"/> </svg> </h4> <div id="content3" class="collapse" data-parent=".accordion"> <div class="card__content"> Lorem ipsum dolor sit amet, consectetur adipisicing elit. Sapiente, illo, vero. Blanditiis nam quae, maiores possimus totam doloribus aliquam, non, aliquid dolores cum distinctio porro, itaque recusandae ad aperiam voluptates! </div> </div> </div> </div> </section>
2. After that, add the following CSS styles into your project:
.collapse { overflow: hidden; transition: 0.4s cubic-bezier(0.4, 0, 0.2, 1); transition-property: opacity, height; will-change: opacity, contents; } .collapse:not(.is-active) { height: 0; opacity: 0; } .accordion { --background: #f3f3f3; --border-radius: 12px; border-radius: var(--border-radius); background: var(--background); box-shadow: 0 2px 6px rgba(0, 0, 0, 0.075); border: 1px solid #d9d9d9; } .accordion .card { overflow: hidden; margin: -1px -1px 1px; background: var(--background); border: 1px solid #d9d9d9; transition: 0.3s cubic-bezier(0.4, 0, 0.2, 1); will-change: contents; text-align: left; } .accordion .card:first-child { border-radius: var(--border-radius) var(--border-radius) 0 0; } .accordion .card:last-child { border-radius: 0 0 var(--border-radius) var(--border-radius); margin-bottom: -1px; } .accordion .card:not(:last-child):not(.is-active) { border-bottom-color: transparent; } .accordion .card.is-active { background: #fff; border-radius: var(--border-radius); } .accordion .card.is-active + .card { border-top-color: transparent; } .accordion .card__title { display: flex; justify-content: space-between; align-items: center; min-height: 3.5rem; margin: 0; margin-bottom: -1px; padding: 0.5rem 1.25rem; color: rgba(0, 0, 0, 0.8); text-decoration: none; font-size: 18px; font-weight: 600; -webkit-user-select: none; -moz-user-select: none; -ms-user-select: none; user-select: none; } .accordion .card__title:hover .icon { opacity: 0.8; } .accordion .card__title.is-active .icon { transform: rotateX(180deg); opacity: 1; } .accordion .card__title .icon { width: 28px; height: 28px; fill: currentColor; opacity: 0.3; transition: 0.3s cubic-bezier(0.4, 0, 0.2, 1); margin-right: -0.25rem; } .accordion .card__content { padding: 0 1.25rem 1.25rem; } * { box-sizing: border-box; line-height: calc(1em + 0.5rem); } html { font: 16px/1.5 "Nunito Sans", sans-serif; -webkit-font-smoothing: antialiased; text-rendering: optimizeLegibility; } h1 { margin-bottom: 3rem; font-size: 2rem; font-weight: 400; color: gray; } a { color: inherit; text-decoration: none; } a, [data-toggle] { cursor: pointer; }
3. Finally, add the following JavaScript code and done.
(function Accordion() { const triggers = document.querySelectorAll('[data-toggle="collapse"]'); let activeToggle; triggers && triggers.forEach(trigger => { trigger.collapseTarget = document.querySelector( trigger.hash || trigger.dataset.target); trigger.collapseTarget.dataset.parent && trigger.collapseTarget.classList.contains("is-active") && ( activeToggle = trigger); trigger.addEventListener("click", event => { event.preventDefault(); event.stopPropagation(); toggle(trigger); }); // Remove height when end open transition trigger.collapseTarget.addEventListener("transitionend", ({ target }) => { if (!target.classList.contains("is-active")) return; target.style.height = null; }); }); function toggle(trigger) { if (trigger.collapseTarget.classList.contains("is-active")) { close(trigger); activeToggle = null; } else { activeToggle && activeToggle.collapseTarget.dataset.parent && close(activeToggle); trigger.collapseTarget.dataset.parent && (activeToggle = trigger); open(trigger); } } function close(trigger) { setHeight(trigger.collapseTarget); trigger.parentElement.classList.remove("is-active"); trigger.classList.remove("is-active"); trigger.collapseTarget.classList.remove("is-active"); setTimeout(() => { trigger.collapseTarget.style.height = null; }, 0); } function open(trigger) { trigger.classList.add("is-active"); trigger.parentElement.classList.add("is-active"); setTimeout(() => { setHeight(trigger.collapseTarget); trigger.collapseTarget.classList.add("is-active"); }, 0); } function setHeight(target) { target.style.height = target.scrollHeight + "px"; } })();
That’s all! hopefully, you have successfully integrated this accordion code snippet into your project. If you have any questions or facing any issues, feel free to comment below.
Similar Code Snippets:
I code and create web elements for amazing people around the world. I like work with new people. New people new Experiences.
I truly enjoy what I’m doing, which makes me more passionate about web development and coding. I am always ready to do challenging tasks whether it is about creating a custom CMS from scratch or customizing an existing system.