正如您在下面的代码段中所看到的,当我点击菜单图标时,显示/隐藏动画已正确完成,但这不适用于项目的内容。
此外,当类'active-menu'附加到菜单时,'item'类正确应用display:grid属性,但是一旦关闭,项目的内容就像不再被认为那样了作为菜单的直接子,但直接是容器,这是错误的。
我无法清楚地知道问题出在哪里,所以我需要你的建议(我希望我的解释清楚)
let topBar = document.getElementById("tobBar");
let menu = document.getElementById("menu");
ico.addEventListener('click', (e) => {
ico.classList.toggle("toggle-color");
menu.classList.toggle("active-menu");
})
body {
background-color: #eee;
}
.container {
width: 370px;
height: 550px;
background: darkgray;
z-index: -2;
margin: 0 auto;
border-radius: 20px;
}
/* * * TOP BAR * * */
.top-bar {
height: 50px;
width: 100%;
background: #00b0ff;
display: flex;
align-items: center;
z-index: 999;
border-top-left-radius: inherit;
border-top-right-radius: inherit;
}
.top-bar-ico {
width: 50px;
text-align: center;
cursor: pointer;
z-index: 999;
}
.toggle-color {
color: white;
}
/* * * * * */
/* * * MENU * * */
.menu {
height: 500px;
transition: 0.5s linear;
width: 0;
z-index: 999;
display: flex;
flex-direction: column;
padding: 0 5px;
}
.active-menu {
width: 90%;
background: #00b0ff;
z-index: 999;
border-bottom-left-radius: inherit;
}
.active-menu > * {
background-color: white;
}
/* * * * * */
/* * * MENU ITEMS * * */
.menu > .item {
height: 50px;
}
.item:not(:first-child) {
margin-top: 10px;
}
.item {
display: grid;
grid-template-columns: 50px 1fr;
grid-gap: 5px;
}
/* * * * * */
<html>
<head>
<script defer src="https://use.fontawesome.com/releases/v5.0.2/js/all.js"></script>
</head>
<body>
<div class="container">
<div class="top-bar">
<div id="ico" class="top-bar-ico">
<i class="fa fa-coffee"></i>
</div>
<div class="top-bar-title">
<p>Click the icon</p>
</div>
</div>
<div id="menu" class="menu">
<div id="menuItem" class="item">
<div class="item-icon">
<i class="fa fa-coffee"></i>
</div>
<div class="item-title">
Menu item
</div>
<div class="item-text">
lalalala
</div>
</div>
<div class="item">2</div>
<div class="item">3</div>
<div class="item">4</div>
<div class="item">5</div>
<div class="item">6</div>
</div>
</div>
</body>
</html>
答案 0 :(得分:1)
您需要将overflow:hidden
设置为.menu
。见下文:
let topBar = document.getElementById("tobBar");
let menu = document.getElementById("menu");
ico.addEventListener('click', (e) => {
ico.classList.toggle("toggle-color");
menu.classList.toggle("active-menu");
})
&#13;
body {
background-color: #eee;
}
.container {
width: 370px;
height: 550px;
background: darkgray;
z-index: -2;
margin: 0 auto;
border-radius: 20px;
}
/* * * TOP BAR * * */
.top-bar {
height: 50px;
width: 100%;
background: #00b0ff;
display: flex;
align-items: center;
z-index: 999;
border-top-left-radius: inherit;
border-top-right-radius: inherit;
}
.top-bar-ico {
width: 50px;
text-align: center;
cursor: pointer;
z-index: 999;
}
.toggle-color {
color: white;
}
/* * * * * */
/* * * MENU * * */
.menu {
height: 500px;
transition: 0.5s linear;
width: 0;
z-index: 999;
display: flex;
flex-direction: column;
overflow-x:hidden;
}
.active-menu {
width: 90%;
padding: 0 5px;
background: #00b0ff;
z-index: 999;
border-bottom-left-radius: inherit;
}
.active-menu>* {
background-color: white;
}
/* * * * * */
/* * * MENU ITEMS * * */
.menu>.item {
height: 50px;
}
.item:not(:first-child) {
margin-top: 10px;
}
.item {
display: grid;
grid-template-columns: 50px 1fr;
grid-gap: 5px;
}
/* * * * * */
&#13;
<html>
<head>
<script defer src="https://use.fontawesome.com/releases/v5.0.2/js/all.js"></script>
</head>
<body>
<div class="container">
<div class="top-bar">
<div id="ico" class="top-bar-ico">
<i class="fa fa-coffee"></i>
</div>
<div class="top-bar-title">
<p>Click the icon</p>
</div>
</div>
<div id="menu" class="menu">
<div id="menuItem" class="item">
<div class="item-icon">
<i class="fa fa-coffee"></i>
</div>
<div class="item-title">
Menu item
</div>
<div class="item-text">
lalalala
</div>
</div>
<div class="item">2</div>
<div class="item">3</div>
<div class="item">4</div>
<div class="item">5</div>
<div class="item">6</div>
</div>
</div>
</body>
</html>
&#13;