javascript - 带有下拉菜单的响应式导航栏不起作用

标签 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>

菜单包含左侧的主页 Logo 图像和右侧的 4 个菜单项。

在事件的“主页”链接之后是“程序”下拉按钮,该按钮下方应该有一个下拉菜单“成功”。此下拉列表未正确加载且设计失真。有任何解决此问题的帮助吗?

最佳答案

我建议不要使用 float 进行布局。 Flexboxes 在这方面要好得多,并且现在在浏览器中得到广泛支持。

float 会破坏 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>

关于javascript - 带有下拉菜单的响应式导航栏不起作用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/59057000/

相关文章:

javascript - Java Web Audio Api-如何将base64Sting保存到音频文件

javascript - Jquery - 2 Ajax 请求无法在同一页面上工作

javascript - 在javascript中遍历递归关系矩阵

html - 在业务催化剂中设计表单样式,用于数据捕获

javascript - jquery clone 仅向克隆元素添加类

javascript - 如何在 javascript/jQuery 中创建命名空间和类

javascript - 使用 jQuery 从字符串中删除 ' &raquo; '

jQuery:为什么 $(this) 在 $ ("*") 中选择整个内容

python - 并排输入表单 - HTML、Flask

html - 如何创建一个带有 Logo 的CSS半圆?