I am trying to use .slideToggle()
to toggle a div with display: none
to display: grid
. However this animation seems to add display: block
inline to the html and overwrite any other css class
CSS
.grid {
display: grid;
}
jQuery
$('#nav-toggle').click(function() {
$('.nav-menu').slideToggle(500, function() {
$(this).toggleClass('grid')
});
});
I had somewhat brief success using
$('#nav-toggle').click(function() {
$('.nav-menu').slideToggle(500, function() {
if ($(this).is(':visible')) {
$(this).css('display','grid');
}
});
});
.nav-menu {
display: none;
grid-template-columns: repeat(3, 1fr);
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<div id=nav-toggle>
Toggle
</div>
<div class=nav-menu>
<div>Item 1</div>
<div>Item 2</div>
<div>Item 3</div>
</div>
but the initial animation seems to play using display: block
and once the animation is finished I guess jQuery then sees the element as visible and the HTML snaps from display: block
(items stacked) to display: grid
(items spaced).
All following animations seem to play nicely with display: grid
.
How can I get the initial animation to display correctly too?