1 回答
TA贡献1993条经验 获得超5个赞
切换菜单的类,而不是直接操作样式。然后使用 CSS 转换来制作动画,例如一个maximum-height或其他 CSS 属性:
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>
- 1 回答
- 0 关注
- 151 浏览
添加回答
举报