I'm trying to build a small modal plugin but I'm struggling to find a way to hide the modal and be able to fade it in/out using only CSS3 transitions.
Obviously, I can't animate display: none to inherit and using opacity and visibility isn't an option because the content is still technically there, it just can't be seen so it prevents elements beneath it from being used (buttons etc.)
So my thinking was to use jQuery to switch between display: hidden and inherit and then use jQuery to add a class of 'shown' which would trigger the transition. But it's not working. I'm just getting an instant show (as with the blackout element behind). The CSS transitions are working though, I've tested them without the 'display' stuff.
My jQuery so far:
// Modal open trigger
$('.modal-trigger').click(function() {
modal = $('#' + $(this).data('modal-trigger'));
modal.css({'display':'inherit'}).addClass('modal-show');
$('#blackout').css({'display':'inherit'}).addClass('show');
});
Any ideas?
CSS transitions don't take effect if the element is hidden (display: none
) when the transition is applied. Also, it doesn't matter what order the CSS changes are made in your JavaScript function, the browser will compute them all at once.
A quick solution is to force the browser to process the display
change first, then the transition
.
Try this:
modal.css({'display':'inherit'});
setTimeout(function(){ modal.addClass('modal-show'); }, 0);
Another way is to avoid using display: none
at all. Use:
.hide
{
opacity: 0;
pointer-events: none;
}
.show
{
opacity: 1;
pointer-events: auto;
}
pointer-events
allows the element to be invisible, but not interfere by swallowing mouse events. You should probably avoid using this except for small aesthetic things though, since the element might still interfere with e.g. form tab ordering. Also, IE support for the property is only from IE 11, if you care about that.