To implement CSS classes effectively, follow these steps:
- Firstly, designate a class as hidden and assign it to your desired element.
- To achieve a smooth transition effect, define the
transition
property for the attributes within your hidden class.
- Upon loading, eliminate the specified class using the
setTimeout()
function.
You can also utilize custom attributes to determine when the "hidden-class" should be removed after a certain delay!
Ensure to establish a default value in case the attribute is not provided.
This technique allows you to display multiple elements at varying intervals, all configured in the HTML code.
for (let el of document.querySelectorAll('.onload-hidden')) {
setTimeout(() => el.classList.remove('onload-hidden'),
el.getAttribute('data-delay') || 1000); // Specified delay or fallback delay
}
.onload-hidden {
visibility: hidden;
opacity: 0;
}
div {
transition: 1s;
}
<div>
<h1>Always displayed</h1>
<p>This <div> will always remain visible.</p>
</div>
<div class="onload-hidden" data-delay="4000">
<h1>Initially concealed - 1</h1>
<p>This <div> will appear after a specific delay!</p>
</div>
<div class="onload-hidden" data-delay="500">
<h1>Initially obscured - 2</h1>
<p>This <div> will become visible after a set delay!</p>
</div>