ICode9

精准搜索请尝试: 精确搜索
首页 > 编程语言> 文章详细

javascript – 如何使用Jquery切换元素

2019-07-27 01:35:59  阅读:135  来源: 互联网

标签:html javascript jquery 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类来打开和关闭菜单,该类在元素上设置所需的宽度.试试这个:

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');
  });
});

标签:html,javascript,jquery,css
来源: https://codeday.me/bug/20190727/1549414.html

本站声明: 1. iCode9 技术分享网(下文简称本站)提供的所有内容,仅供技术学习、探讨和分享;
2. 关于本站的所有留言、评论、转载及引用,纯属内容发起人的个人观点,与本站观点和立场无关;
3. 关于本站的所有言论和文字,纯属内容发起人的个人观点,与本站观点和立场无关;
4. 本站文章均是网友提供,不完全保证技术分享内容的完整性、准确性、时效性、风险性和版权归属;如您发现该文章侵犯了您的权益,可联系我们第一时间进行删除;
5. 本站为非盈利性的个人网站,所有内容不会用来进行牟利,也不会利用任何形式的广告来间接获益,纯粹是为了广大技术爱好者提供技术内容和技术思想的分享性交流网站。

专注分享技术,共同学习,共同进步。侵权联系[81616952@qq.com]

Copyright (C)ICode9.com, All Rights Reserved.

ICode9版权所有