<html>
<head>
<style>
body {
margin: 0;
padding: 0;
}
.header {
border: 1px solid #AAAAAA;
height: 100px;
}
.container {
display: flex;
height: calc(100% - 100px);
width: 100%;
}
.menu {
position: relative;
width: 150px;
height: 100%;
border: 1px solid #AAAAAA;
}
.arrow {
width: 20px;
height: 30px;
line-height: 30px;
text-align: center;
position: absolute;
top: 50%;
right: 0;
cursor: pointer;
background: #B7B7B7;
color: #FFFFFF;
}
ul {
list-style: none;
padding: 0;
}
ul li {
width: 100%;
height: 40px;
line-height: 40px;
display: block;
text-align: center;
cursor: pointer;
}
ul li:hover {
background: #AAAAAA;
}
.content {
flex: 1;
}
</style>
<script>
var isShow = true;
function arrow() {
isShow = !isShow;
let menu = document.getElementById('menu');
let arrow = document.getElementById('arrow');
if (isShow) {
menu.style.transform = "translateX(0)";
menu.style.transition = "all 2s"; // 增加过渡效果
arrow.style.right = "0";
arrow.innerText = "<";
} else {
menu.style.transform = "translateX(-100%)";
menu.style.transition = "all 2s"; // 增加过渡效果
arrow.style.right = "-20px";
arrow.innerText = ">";
}
}
</script>
</head>
<body>
<div class="header"></div>
<div class="container">
<div id='menu' class="menu">
<ul>
<li>菜单一</li>
<li>菜单二</li>
</ul>
<span id="arrow" class="arrow" onclick="arrow()"><</span>
</div>
<div class="content">
</div>
</div>
</body>
</html>
其他:目前是通过 translateX 来隐藏侧边栏的,也可以结合动画来实现