下拉式响应式导航栏不起作用

时间:2019-11-26 18:07:58

标签: javascript jquery html css twitter-bootstrap

我正在创建一个具有下拉菜单的响应式导航栏。下面是我的代码:

我有内联的CSS代码和媒体查询用于测试目的。所以看起来可能很长。

<link href="https://cdnjs.cloudflare.com/ajax/libs/font-awesome/4.7.0/css/font-awesome.min.css" rel="stylesheet" />
<link href="../../themes/bootstrap/starterkits/THEMENAME/css/style.css" rel="stylesheet" />
<link href="//fonts.googleapis.com/css?family=Raleway" rel="stylesheet" type="text/css" />
<style type="text/css">body {margin:0;font-family:Arial}

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

.home {
  float: left;
  overflow: hidden;
}

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

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

.topnav .icon {
  display: none;
}

.dropdown {
  float: right;
  overflow: hidden;
}



.dropdown .dropbtn {
  font-size: 17px;    
  border: none;
  outline: none;
  color: black;
  padding: 14px 16px;
  background-color: inherit;
  margin: 0;
}

.dropdown-content {
  display: none;
  position: absolute;
  background-color: #ffffff;
  min-width: 160px;
  box-shadow: 0px 8px 16px 0px rgba(0,0,0,0.2);
  z-index: 1;
}

.dropdown-content a {
  float: none;
  color: black;
  padding: 12px 16px;
  text-decoration: none;
  display: block;
  text-align: left;
}

.topnav a:hover, .dropdown:hover .dropbtn {
  background-color: #555;
  color: #D5DBDB;
}

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

.dropdown:hover .dropdown-content {
  display: block;
}

@media screen and (max-width: 600px) {
  .topnav a:not(:first-child), .dropdown .dropbtn {
    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;
  }
  .topnav.responsive .dropdown {float: none;}
  .topnav.responsive .dropdown-content {position: relative;}
  .topnav.responsive .dropdown .dropbtn {
    display: block;
    width: 100%;
    text-align: left;
  }
}
</style>
<div class="panel">
<div class="topnav" id="myTopnav">
<div class="home"><a href="#"><img src="../../sites/default/files/logo_0.png" /></a></div>

<div class="dropdown">
<div class="dropbtn"><a href="#">Programs</a></div>

<div class="dropdown-content"><a href="#">Success</a></div>
</div>
<a href="#">Contact</a> 
<a href="#">Families</a> 
<a href="#">About</a> 
<a class="icon" href="javascript:void(0);" onclick="myFunction()" style="font-size:15px;">☰</a></div>
</div>
<script>
function myFunction() {
  var x = document.getElementById("myTopnav");
  if (x.className === "topnav") {
    x.className += " responsive";
  } else {
    x.className = "topnav";
  }
}
</script>

菜单左侧包含Home logo图片,右侧包含4个菜单项。

右边的最后一项是“程序”菜单,其下方应有一个下拉“成功”字样。此下拉菜单未正确加载,并且设计失真。对解决这个问题有帮助吗?

2 个答案:

答案 0 :(得分:1)

我建议不要使用浮点数进行布局。 Flexbox对此要好得多,并且近来在浏览器中得到了广泛的支持。

浮动元素打破了html元素的流程。

此外,.dropbtn上有一个填充,使项目不对齐。

body {
        margin:0;
        font-family:Arial
      }

      .topnav {
        display: flex;
        justify-content: flex-end;
        overflow: hidden;
        background-color: #ffffff;
      }

      .home {
        margin-right: auto;
      }

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

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

      .topnav .icon {
        display: none;
      }

      .dropdown {
        overflow: hidden;
      }

      .dropdown .dropbtn {
        font-size: 17px;    
        border: none;
        outline: none;
        color: black;
        background-color: inherit;
        margin: 0;
      }

      .dropdown-content {
        display: none;
        position: absolute;
        background-color: #ffffff;
        min-width: 160px;
        box-shadow: 0px 8px 16px 0px rgba(0,0,0,0.2);
        z-index: 1;
      }

      .dropdown-content a {
        color: black;
        padding: 12px 16px;
        text-decoration: none;
        display: block;
        text-align: left;
      }

      .topnav a:hover, .dropdown:hover .dropbtn {
        background-color: #555;
        color: #D5DBDB;
      }

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

      .dropdown:hover .dropdown-content {
        display: block;
      }

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

      @media screen and (max-width: 600px) {
        .topnav.responsive {position: relative;}
        .topnav.responsive .icon {
          position: absolute;
          right: 0;
          top: 0;
        }
        .topnav.responsive a {
          display: block;
          text-align: left;
        }
        .topnav.responsive .dropdown {float: none;}
        .topnav.responsive .dropdown-content {position: relative;}
        .topnav.responsive .dropdown .dropbtn {
          display: block;
          width: 100%;
          text-align: left;
        }
      }
<!doctype html>
<html>
  <head>
    <link href="https://cdnjs.cloudflare.com/ajax/libs/font-awesome/4.7.0/css/font-awesome.min.css" rel="stylesheet" />
    <link href="../../themes/bootstrap/starterkits/THEMENAME/css/style.css" rel="stylesheet" />

    <link href="//fonts.googleapis.com/css?family=Raleway" rel="stylesheet" type="text/css" />
  </head>

  <body>
    <div class="panel">
      <div class="topnav" id="myTopnav">
        <div class="home">
          <a href="#">
            <img src="../../sites/default/files/logo_0.png" />
          </a>
        </div>

        <a href="#">About</a> 
        <a href="#">Families</a> 
        <a href="#">Contact</a> 

        <div class="dropdown">
          <div class="dropbtn">
            <a href="#">Programs</a>
          </div>
          
          <div class="dropdown-content">
            <a href="#">Success</a>
          </div>
        </div>
        
        <a class="icon" href="javascript:void(0);" onclick="myFunction()" style="font-size:15px;">☰</a>
      </div>
    </div>

    <script>
      function myFunction() {
        var x = document.getElementById("myTopnav");
        if (x.className === "topnav") {
          x.className += " responsive";
        } else {
          x.className = "topnav";
        }
      }
    </script>
  </body>
</html>

答案 1 :(得分:0)

您不需要将下拉按钮设为div标签,而应将其设为button标签。

所以改变

<div class="dropbtn"><a href="#">Programs</a></div>

<button class="dropbtn"><a style="padding: 0px" href="#">Programs</a></button>

这应该正确加载下拉菜单,并使其与导航栏中的其他项目内联。希望这可以帮助您走上正确的轨道,而无需更改大多数原始代码。

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

.home {
  float: left;
  overflow: hidden;
}

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

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

.topnav .icon {
  display: none;
}

.dropdown {
  float: right;
  overflow: hidden;
}



.dropdown .dropbtn {
  font-size: 17px;    
  border: none;
  outline: none;
  color: black;
  padding: 14px 16px;
  background-color: inherit;
  margin: 0;
}

.dropdown-content {
  display: none;
  position: absolute;
  background-color: #ffffff;
  min-width: 160px;
  box-shadow: 0px 8px 16px 0px rgba(0,0,0,0.2);
  z-index: 1;
}

.dropdown-content a {
  float: none;
  color: black;
  padding: 12px 16px;
  text-decoration: none;
  display: block;
  text-align: left;
}

.topnav a:hover, .dropdown:hover .dropbtn {
  background-color: #555;
  color: #D5DBDB;
}

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

.dropdown:hover .dropdown-content {
  display: block;
}

@media screen and (max-width: 600px) {
  .topnav a:not(:first-child), .dropdown .dropbtn {
    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;
  }
  .topnav.responsive .dropdown {float: none;}
  .topnav.responsive .dropdown-content {position: relative;}
  .topnav.responsive .dropdown .dropbtn {
    display: block;
    width: 100%;
    text-align: left;
  }
}
<link href="https://cdnjs.cloudflare.com/ajax/libs/font-awesome/4.7.0/css/font-awesome.min.css" rel="stylesheet" />
<link href="../../themes/bootstrap/starterkits/THEMENAME/css/style.css" rel="stylesheet" />
<link href="//fonts.googleapis.com/css?family=Raleway" rel="stylesheet" type="text/css" />
<style type="text/css">body {margin:0;font-family:Arial}
</style>
<div class="panel">
<div class="topnav" id="myTopnav">
<div class="home"><a href="#"><img src="../../sites/default/files/logo_0.png" /></a></div>

<div class="dropdown">
<button class="dropbtn"><a style="padding: 0px" href="#">Programs</a></button>
<div class="dropdown-content"><a href="#">Success</a></div>
</div>
<a href="#">Contact</a> 
<a href="#">Families</a> 
<a href="#">About</a> 
<a class="icon" href="javascript:void(0);" onclick="myFunction()" style="font-size:15px;">☰</a></div>
</div>
<script>
function myFunction() {
  var x = document.getElementById("myTopnav");
  if (x.className === "topnav") {
    x.className += " responsive";
  } else {
    x.className = "topnav";
  }
}
</script>