So, this is my code, opening a dropdown menu $smallScreenMenu after clicking on $iconMenu1
Javascript code:
const $iconMenu1 = $('#iconMenu1')
const $smallScreenMenu = $('#smallScreenMenu')
$($iconMenu1.on('click', ()=>{
if ($smallScreenMenu.css('display') == 'block'){
$smallScreenMenu.css('display', 'none');
}
else{
$smallScreenMenu.css('display', 'block');
}
CSS code:
.icon-menu{
right: 15px;
position: absolute;
top: 17px;
font-size: 30px;
background-color: transparent;
border: none;
}
#smallScreenMenu{
display: none;
position: absolute;
right: 0px;
text-align: right;
HTML code:
<button class='icon-menu' id='iconMenu1'></button>
<div id='smallScreenMenu'>
<ul>
<li><a href='#'>Products</a></li>
<li><a href='#'>About</a></li>
</ul>
</div>
So, it works, but I would like to make it slide rather than instantly appear/disappear. Is it possible to edit this code and get what I want, or do I have to write my code from scratch? I tried using slideDown() and slideUp(), but didn't get any succes with that. Also thanks in advance for help
Toggle a class for your menu instead of manipulating styles directly. Then use CSS-transitions to animate for example a maximum-height
or other CSS-properties:
const $iconMenu1 = $('#iconMenu1');
const $smallScreenMenu = $('#smallScreenMenu');
$($iconMenu1.on('click', () => {
$smallScreenMenu.toggleClass('closed');
}));
.icon-menu {
right: 15px;
position: absolute;
top: 17px;
font-size: 30px;
background-color: transparent;
border: none;
}
#smallScreenMenu {
display: block;
position: absolute;
right: 10px;
top: 60px;
text-align: right;
background: rgba(0,255,0,0.1);
overflow-y: hidden;
max-height: 360px;
transition: all 360ms ease-in-out;
box-shadow: 2px 4px 12px #bfbfbf;
}
#smallScreenMenu.closed {
max-height: 0;
}
#smallScreenMenu ul {
list-style-type: none;
margin: 0.5em 1em;
padding: 0;
}
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<button class='icon-menu' id='iconMenu1'>Menu</button>
<div id='smallScreenMenu' class="closed">
<ul>
<li><a href='#'>Products</a></li>
<li><a href='#'>About</a></li>
<li><a href='#'>Another</a></li>
<li><a href='#'>Another</a></li>
<li><a href='#'>Another</a></li>
</ul>
</div>