function openNav() {
document.getElementsByClassName("menu-overlay").style.width = "50%";
}
function closeNav() {
document.getElementsByClassName("menu-overlay").style.width = "0";
}
.menu-overlay {
display: none;
top: 0;
height: 100%;
width: 0%;
position: absolute;
z-index: 1;
right: 0;
background-color: #ef4f50;
overflow-x: hidden;
transition: 0.5s;
}
.menu-body{
position: fixed;
top: 0;
z-index: 99999;
right: 0;
height: 100%;
background-color: #ef4f51;
width: 35%;
float: right;
}
span.closer {
font-size: 50px;
float: right;
color: white;
padding: 30px;
}
.menu-pan{
width: 100%;
float: left;
padding: 0 80px;
}
.menu-pan li {
padding: 10px 0;
}
.menu-pan li a {
color: white;
text-decoration: none;
font-size: 32px;
}
<link href="https://cdnjs.cloudflare.com/ajax/libs/simple-line-icons/2.4.1/css/simple-line-icons.css" rel="stylesheet"/>
<script src="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/js/bootstrap.min.js"></script>
<link href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/css/bootstrap.min.css" rel="stylesheet"/>
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<header>
<div class="header-top clearfix">
<a href="https://www.google.co.in/" target="_blank" class="pull-right btn btn-danger btn-round">DONATE NOW</a>
<a class="l-right toggle-menu" href="#" onclick="openNav()">
<span class="icon-menu"></span>
</a>
</div>
<div class="menu-overlay">
<div class="menu-body">
<a href="#"><span class="closer"><i class="icon-close icons" onclick="closeNav()"></i></span></a>
<ul class="menu-pan">
<li><a href="#">Home</a></li>
<li><a href="#">About</a></li>
<li><a href="#">Our Purpose</a></li>
<li><a href="#">HOME</a></li>
<li><a href="#">Contact</a></li>
</ul>
</div>
</div>
</header>
你好,这是我的菜单条形码。我希望我的导航栏打开时,我点击右侧的汉堡包图标,过渡(平滑)效果,宽度为50%&amp;剩余部分将叠加。 “https://www.w3schools.com/howto/tryit.asp?filename=tryhow_js_overlay”此链接具有相同的效果,但是从左侧开始。
答案 0 :(得分:0)
getElementsByClassName
函数返回一个数组,因此使用getElementsByClassName("menu-overlay").style
将不正确。而是像这样使用它:
function openNav() {
document.getElementsByClassName("menu-overlay")[0].style.width = "50%";
}
function closeNav() {
document.getElementsByClassName("menu-overlay")[0].style.width = "0";
}