To achieve a gradual height increase when revealing all the media content, you can use a CSS transition on the max-height property instead of the property. Here's how you can modify your CSS and make minimal adjustments to your JavaScript:
And I have following JavaScript:
/**
* Media coverage reveal more
*/
const mediaCoverage = document.querySelector('#media .content');
const showAllMediaBtn = document.querySelector('#show-more-media');
if (showAllMediaBtn) {
showAllMediaBtn.addEventListener('click', (e) => {
e.preventDefault();
if (mediaCoverage.classList.contains('all')) {
mediaCoverage.classList.remove('all');
showAllMediaBtn.innerHTML = 'Show More';
} else {
mediaCoverage.classList.add('all');
showAllMediaBtn.innerHTML = 'Show Less';
showAllMediaBtn.classList.add('mt-5');
}
});
}
Right now showAllMediaBtn is working perfectly, but what I want is mediaCoverage should increase its height gradually. I want animation in reveling all.
height
Modify your CSS:
Replace height with max-height in .media .content and add a transition:
Update .media .content.all to set a large max-height and adjust overflow:
.media .content.all {
max-height: 9999px; /* Set to a large value to accommodate content */
overflow: visible; /* Ensure overflow is visible when expanded */
}
Explanation:
max-height vs. height: Animating from a fixed height to height: auto isn't directly possible with CSS transitions. By using max-height, you can animate between two numerical values.
Transitioning max-height: The transition property on max-height allows the element to smoothly expand and collapse.
Setting a large max-height: Since you might not know the exact height of the expanded content, setting max-height to a large value ensures all content is visible.
Your JavaScript remains the same because you're toggling the all class, which now triggers the CSS transition.