I am triggering a navigation by clicking a toggle for it. This toggle adds a class "active" on the nav element, to show or hide it. To animate it, the nav element has some styles per default, a transition statement and some opposing styles via the class "active".
To prevent transition problems with display:none/block, I first toggle this property, then add/remove the class.
The weird thing now:
To actually see the transition, I need to set up a delay after toggling the display property. But: this delay can also be 0.
Does anyone know why this delay is needed, if it can actually be zero?
Here's an example (simplified):
CSS:
#nav-main {
display: none;
transition: all 2s;
opacity: 0.0;
}
#nav-main.active {
opacity: 1.0;
}
JS:
$(function() {
$("#open").on("click", function(e) {
e.preventDefault();
$("#nav-main").show();
$("#nav-main").addClass("active");
});
$("#open-delay").on("click", function(e) {
e.preventDefault();
$("#nav-main").show();
window.setTimeout(function() {
$("#nav-main").addClass("active");
}, 0);
});
$("#close").on("click", function(e) {
e.preventDefault();
$("#nav-main").removeClass("active");
// here the delay is needed, because we have to wait
// for the animation to finish, before we hide the element
window.setTimeout(function() {
$("#nav-main").hide();
}, 2000);
});
});
Working example
https://jsfiddle.net/k9gp3qe2/1/
Thanks for your help
Arne