I've set up a grid of buttons, and my goal is to have the grid fade out when a button is clicked, then fade in with a new div in its place. This involves animating opacity from 1 to 0 and vice versa while toggling the display:none
property for content replacement.
I've managed to get this working (though it may not be the most elegant solution as I'm still learning about animations). However, I'm encountering an issue with the back button within the new div. The new div fades out smoothly, but the original grid initially snaps back before fading in, and I'm unsure how to resolve this.
(Note: I'm implementing this in Angular, but I've converted it to vanilla JS) Here's my code snippet, along with a link to the CodePen for reference:
function showAccordion(){
document.getElementById('faq-support-boxes-grid')?.classList.add('fadeOut');
setTimeout(() => {
document.getElementById('faq-support-boxes-grid')?.classList.add('hide-element');
document.getElementById('accordion-container')?.classList.add('fadeIn');
document.getElementById('accordion-container')?.classList.add('show-element');
}, 1000);
}
function resetAccordion(){
document.getElementById('accordion-container')?.classList.remove('fadeIn');
document.getElementById('accordion-container')?.classList.add('fadeOut');
setTimeout(() => {
document.getElementById('accordion-container')?.classList.remove('show-element');
}, 1000);
setTimeout(() =>{
document.getElementById('faq-support-boxes-grid')?.classList.remove('fadeOut');
document.getElementById('faq-support-boxes-grid')?.classList.remove('hide-element');
document.getElementById('faq-support-boxes-grid')?.classList.add('fadeIn');
},2000)
}
.faq-support-boxes-grid {
background-color: #fff;
display: grid;
gap: 1rem;
grid-template-columns: repeat(3, 1fr);
grid-template-rows: repeat(2, 1fr);
opacity: 1;
.feature-box{
background-color:#a6a6a6;
text-align:center;
img{
display: inline;
width:40%;
}
}
}
.fadeOut {
animation: fadeOutTop 1s ease-out;
animation-fill-mode: forwards;
}
.fadeIn {
animation: fadeInTop 1s ease-out 1s;
animation-fill-mode: forwards;
}
.accordion-container{
transform: translateY(-30rem);
opacity: 0;
display: none;
transition: all 1s;
}
.accordion-container > div{
display: none;
}
.accordion-back{
transform: translateY(-10%);
display: block;
color:#325390 !important;
}
.show-element{
display: block !important;
}
.hide-element{
display: none !important;
}
@keyframes fadeOutTop{
0%{
opacity: 1;
transform: translate(0rem);
}
100%{
opacity: 0;
transform: translateY(-30rem);
}
}
@keyframes fadeInTop{
100%{
opacity: 1;
transform: translate(0rem);
}
0%{
opacity: 0 ;
transform: translateY(-30rem);
}
}
<div id="faq-support-boxes-grid" class="faq-support-boxes-grid">
<div id="feature-box-about" onclick="showAccordion()" class="feature-box" tabindex="1">
<img src="https://www.adaptivewfs.com/wp-content/uploads/2020/07/logo-placeholder-image.png" >
<h3>Heading 1</h3>
<p>some text</p>
</div>
<div id="feature-box-security" onclick="showAccordion()" class="feature-box" tabindex="2">
<img src="https://www.adaptivewfs.com/wp-content/uploads/2020/07/logo-placeholder-image.png" >
<h3>Heading 2</h3>
<p>some text</p>
</div>
<div id="feature-box-using" class="feature-box" tabindex="3">
<img src="https://www.adaptivewfs.com/wp-content/uploads/2020/07/logo-placeholder-image.png" >
<h3>Heading 3</h3>
<p>some text</p>
</div>
<div id="feature-box-account" class="feature-box" tabindex="4">
<img src="https://www.adaptivewfs.com/wp-content/uploads/2020/07/logo-placeholder-image.png" >
<h3>Heading 4</h3>
<p>some text</p>
</div>
<div id="feature-box-billing" class="feature-box" tabindex="5">
<img src="https://www.adaptivewfs.com/wp-content/uploads/2020/07/logo-placeholder-image.png" >
<h3>Heading 5</h3>
<p>some text</p>
</div>
<div id="feature-box-support" class="feature-box" tabindex="6">
<img src="https://www.adaptivewfs.com/wp-content/uploads/2020/07/logo-placeholder-image.png" >
<h3>Heading 6</h3>
<p>some text</p>
</div>
</div>
<div id="accordion-container" class="accordion-container">
<button class="accordion-back btn btn-secondary" onclick="resetAccordion()">
Back
</button>
</div>