固定的响应式顶部栏覆盖了第二个固定的顶部栏

时间:2019-01-08 06:58:58

标签: javascript html css html5 css3


所以我有一个CSS问题,我无法解决。我到处搜索答案,然后终于在stackoverflow来到这里。我正在制作一个具有两个固定导航栏的网站,一个位于顶部,另一个位于另一个。两者也都响应。当我调整窗口大小时,第一个导航栏具有填充,因此基本上它的高度变大并覆盖第二个导航栏。您能提出任何克服此问题的方法吗?这是我的代码:

function myFunction() {
  var x = document.getElementById("myTopnav");
  if (x.className === "topnav") {
    x.className += " responsive";
  } else {
    x.className = "topnav";
  }
}
body {
  margin: 0;
  font-family: Arial, Helvetica, sans-serif;
}

.topnav {
  overflow: hidden;
  background-color: #333;
  position:fixed;
  top:0;
  left:0;
  right:0;
}

.topnav a {
  float: left;
  display: block;
  color: #f2f2f2;
  text-align: center;
  padding: 14px 16px;
  text-decoration: none;
  font-size: 17px;
}

.topnav a:hover {
  background-color: #ddd;
  color: black;
}

.active {
  background-color: #4CAF50;
  color: white;
}

.topnav .icon {
  display: none;
}

@media screen and (max-width: 600px) {
  .topnav a:not(:first-child) {display: none;}
  .topnav a.icon {
    float: right;
    display: block;
  }
}

@media screen and (max-width: 600px) {
  .topnav.responsive {position: relative;}
  .topnav.responsive .icon {
    position: absolute;
    right: 0;
    top: 0;
  }
  .topnav.responsive a {
    float: none;
    display: block;
    text-align: left;
  }
}
.secondnav {
background:red;
width:100%;
height:42px;
position:fixed;
top:47px;
left:0;
right:0;
}
<html>
<head>
<meta name="viewport" content="width=device-width, initial-scale=1">
<link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/font-awesome/4.7.0/css/font-awesome.min.css">
</head>
<body>
<div class="topnav" id="myTopnav">
  <a href="#home" class="active">Home</a>
  <a href="#news">News</a>
  <a href="#contact">Contact</a>
  <a href="#about">About</a>
  <a href="#help" class="active">Help</a>
  <a href="#terms">Terms</a>
  <a href="#privacy">Privacy</a>
  <a href="#cookies">Cookies</a>
  <a href="javascript:void(0);" class="icon" onclick="myFunction()">
    <i class="fa fa-bars"></i>
  </a>
</div>
<div class="secondnav">
</div>
</body>
</html>

当我调整窗口大小时,第一个导航栏的高度增加,因此覆盖了第二个导航栏,如何解决此问题?并预先感谢!

1 个答案:

答案 0 :(得分:0)

您应该在位置固定的另一个div中放置两个导航。并删除两个导航的位置修复,这里我确实修改过代码和CSS和HTML,希望这会有所帮助!

function myFunction() {
  var x = document.getElementById("myTopnav");
  if (x.className === "topnav") {
    x.className += " responsive";
  } else {
    x.className = "topnav";
  }
}
body {
  margin: 0;
  font-family: Arial, Helvetica, sans-serif;
}
.outer {
  position: fixed;
  top: 0px;
  widtH: 100%;
}

.topnav {
  overflow: hidden;
  background-color: #333;
}

.topnav a {
  float: left;
  display: block;
  color: #f2f2f2;
  text-align: center;
  padding: 14px 16px;
  text-decoration: none;
  font-size: 17px;
}

.topnav a:hover {
  background-color: #ddd;
  color: black;
}

.active {
  background-color: #4CAF50;
  color: white;
}

.topnav .icon {
  display: none;
}

@media screen and (max-width: 600px) {
  .topnav a:not(:first-child) {display: none;}
  .topnav a.icon {
    float: right;
    display: block;
  }
}

@media screen and (max-width: 600px) {
  .topnav.responsive {position: relative;}
  .topnav.responsive .icon {
    position: absolute;
    right: 0;
    top: 0;
  }
  .topnav.responsive a {
    float: none;
    display: block;
    text-align: left;
  }
}
.secondnav {
background:red;
width:100%;
height:42px;
}
<html>
<head>
<meta name="viewport" content="width=device-width, initial-scale=1">
<link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/font-awesome/4.7.0/css/font-awesome.min.css">
</head>
<body>
<div class="outer">
  <div class="topnav" id="myTopnav">
    <a href="#home" class="active">Home</a>
    <a href="#news">News</a>
    <a href="#contact">Contact</a>
    <a href="#about">About</a>
    <a href="#help" class="active">Help</a>
    <a href="#terms">Terms</a>
    <a href="#privacy">Privacy</a>
    <a href="#cookies">Cookies</a>
    <a href="javascript:void(0);" class="icon" onclick="myFunction()">
      <i class="fa fa-bars"></i>
    </a>
  </div>
  <div class="secondnav">
</div>

</div>
</body>
</html>