我正在尝试使用 HTML、CSS 和 JS 创建导航栏。但是,我在尝试切换汉堡包时遇到问题,所以当您单击汉堡包时,一旦幻灯片菜单滑出,当您再次单击汉堡包时,它会隐藏幻灯片菜单。
为此,我创建了两个函数,并内联调用了 opennav
函数。
function openNav() {
document.getElementById("mySidenav").style.width = "250px";
}
function closeNav() {
document.getElementById("mySidenav").style.width = "0";
}
@import url('https://fonts.googleapis.com/css?family=Rubik');
body {
margin: 0;
font-family: "Rubik", sans-serif
}
.topnav {
position: relative;
overflow: hidden;
background-color: #122B3C;
height: 65px;
}
.topnav a {
float: left;
color: #f2f2f2;
text-align: center;
padding: 14px 16px;
text-decoration: none;
font-size: 17px;
}
.topnav a.active {
background-color: #4CAF50;
color: white;
}
.topnav-centered a {
float: none;
position: absolute;
top: 50%;
left: 50%;
transform: translate(-50%, -50%);
}
.topnav-right {
float: right;
}
.hamburger {
display: inline-block;
cursor: pointer;
position: fixed;
z-index: 999;
}
.home-hamburger {
position: static;
}
.nav-center {
position: absolute;
left: 48%;
width: 40px;
margin-left: -20px;
/* halve the width of the image */
}
.doch {
position: fixed !important;
top: 0;
}
.bar1,
.bar2,
.bar3 {
width: 40px;
height: 3px;
background-color: #ffffff;
margin: 7px 0;
transition: 0.4s;
}
.sidenav {
height: 100%;
width: 0;
position: fixed;
z-index: 1;
top: 0;
left: 0;
background-color: #304C58;
overflow-x: hidden;
transition: 0.5s;
padding-top: 60px;
}
.sidenav a {
padding: 8px 8px 8px 16px;
text-decoration: none;
font-size: 25px;
color: #122B3C;
display: block;
transition: 0.3s;
text-transform: uppercase;
font-weight: 600;
margin-bottom: 10px;
}
.sidenav a:hover {
color: #f1f1f1;
}
.sidenav .closebtn {
position: absolute;
top: 0;
right: 25px;
font-size: 36px;
margin-left: 50px;
}
3 @media screen and (max-height: 450px) {
.sidenav {
padding-top: 15px;
}
.sidenav a {
font-size: 18px;
}
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.3/jquery.min.js"></script>
<div class="topnav">
<div class="topnav-centered">
<a href="#home"><img src="https://via.placeholder.com/350x150" alt=""></a>
</div>
<a href="#news">
<div id="toggle" class="hamburger" onclick="openNav()">
<div class="bar1"></div>
<div class="bar2"></div>
<div class="bar3"></div>
</div>
</a>
</div>
<div style="padding-left:16px">
<h2>Responsive Top Navigation with Centered and Right-Aligned Links</h2>
<p>Resize the browser window to see the responsive effect.</p>
</div>
<div id="mySidenav" class="sidenav">
<a href="#">What we do</a>
<a href="#">How we do it</a>
<a href="#">Team</a>
<a href="#">Work for us</a>
<a href="#">Jobs</a>
<a href="#">Contact Us</a>
</div>
我的问题的可待因是 https://codepen.io/mrsalami/pen/JadoyR
最佳答案
为此,您不一定需要 jQuery。您当前的纯 JS 逻辑可以很容易地修改,以根据需要进行这项工作。
首先,不要使用内联事件处理程序或 CSS,两者都是不好的做法。相反,使用不显眼的事件处理程序。然后,您可以创建一个事件处理程序,通过在元素上切换 CSS 类来打开和关闭菜单,该类在元素上设置所需的 width
。试试这个:
document.getElementById('toggle').addEventListener('click', function() {
document.getElementById("mySidenav").classList.toggle('open');
});
@import url('https://fonts.googleapis.com/css?family=Rubik');
body {
margin: 0;
font-family: "Rubik", sans-serif
}
.topnav {
position: relative;
overflow: hidden;
background-color: #122B3C;
height: 65px;
}
.topnav a {
float: left;
color: #f2f2f2;
text-align: center;
padding: 14px 16px;
text-decoration: none;
font-size: 17px;
}
.topnav a.active {
background-color: #4CAF50;
color: white;
}
.topnav-centered a {
float: none;
position: absolute;
top: 50%;
left: 50%;
transform: translate(-50%, -50%);
}
.topnav-right {
float: right;
}
.hamburger {
display: inline-block;
cursor: pointer;
position: fixed;
z-index: 999;
}
.home-hamburger {
position: static;
}
.nav-center {
position: absolute;
left: 48%;
width: 40px;
margin-left: -20px;
/* halve the width of the image */
}
.doch {
position: fixed !important;
top: 0;
}
.bar1,
.bar2,
.bar3 {
width: 40px;
height: 3px;
background-color: #ffffff;
margin: 7px 0;
transition: 0.4s;
}
.sidenav {
height: 100%;
width: 0;
position: fixed;
z-index: 1;
top: 0;
left: 0;
background-color: #304C58;
overflow-x: hidden;
transition: 0.5s;
padding-top: 60px;
}
.sidenav.open {
width: 250px;
}
.sidenav a {
padding: 8px 8px 8px 16px;
text-decoration: none;
font-size: 25px;
color: #122B3C;
display: block;
transition: 0.3s;
text-transform: uppercase;
font-weight: 600;
margin-bottom: 10px;
}
.sidenav a:hover {
color: #f1f1f1;
}
.sidenav .closebtn {
position: absolute;
top: 0;
right: 25px;
font-size: 36px;
margin-left: 50px;
}
3 @media screen and (max-height: 450px) {
.sidenav {
padding-top: 15px;
}
.sidenav a {
font-size: 18px;
}
}
<div class="topnav">
<div class="topnav-centered">
<a href="#home"><img src="https://via.placeholder.com/350x150" alt=""></a>
</div>
<a href="#news">
<div id="toggle" class="hamburger">
<div class="bar1"></div>
<div class="bar2"></div>
<div class="bar3"></div>
</div>
</a>
</div>
<div style="padding-left:16px">
<h2>Responsive Top Navigation with Centered and Right-Aligned Links</h2>
<p>Resize the browser window to see the responsive effect.</p>
</div>
<div id="mySidenav" class="sidenav">
<a href="#">What we do</a>
<a href="#">How we do it</a>
<a href="#">Team</a>
<a href="#">Work for us</a>
<a href="#">Jobs</a>
<a href="#">Contact Us</a>
</div>
如果您确实想使用 jQuery,则将 JS 更改为:
$(function() {
$('#toggle').click(function() {
$('#mySidenav').toggleClass('open');
});
});
关于javascript - 如何使用 Jquery 切换元素,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/51981003/