javascript - 如何使用 Jquery 切换元素

标签 javascript jquery html css

我正在尝试使用 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/

相关文章:

javascript - 如何从 Javascript 类构造函数中访问分配给对象的变量名称?

javascript - 灯箱弹出窗口仅出现一次

javascript - 检查多维输入数组中的值

jquery - 显示时给元素一个宽度 :none;

html - 与 Chrome 不同,Firefox 的 WebRTC SDP 对象(本地描述)不包含 DataChannel 信息?

jQuery:单击以隐藏元素

javascript - 链接到特定幻灯片

javascript - 在浏览器中使用 ES6 模块时,我应该在 `script` 标签中引用文件吗

javascript - 将 Circletype.js 与 jQuery animate 相结合

html - 当前菜单颜色 CSS 导航