菜鸟水平问题

时间:2017-05-01 17:37:42

标签: html css

我正在创建一个网站,其中有MenuBar ...

当我点击MenuBar时,它会变为水平但我需要它是垂直的。 我发布代码以便更容易理解。 请帮我解决这个错误。

<style>
body {margin:0;}

.navbar {
  overflow: hidden;
  background-color: gold;
  position: fixed;
  top: 0;
  width: 100%;
}

.navbar a {
  float: left;
  display: block;
  color: #f2f2f2;
  text-align: center;

  text-decoration: none;
  font-size: 17px;
}

.main {

  margin-top: 65px;
}

</style>
<style>
.dropbtn {
    background-color: red;
    color: gold;
    padding: 25px;
    font-size: 16px;
    border: none;
    cursor: pointer;
}
.dropbtn:hover, .dropbtn:focus {
    background-color: gold;
    color: red;
}

.dropdown {
    float: right;
    position: relative;
    display: inline-block;

}

.dropdown-content {
    display: none;

    background-color: #f9f9f9;
    min-width: 160px;
    overflow: auto;
    box-shadow: 0px 8px 16px 0px rgba(0,0,0,0.2);
    right: 0;
    z-index: 1;
}

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

.dropdown a:hover {background-color: blue}

.show {display:block;}
</style>
<link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/font-awesome/4.7.0/css/font-awesome.min.css">
</head>
<body>

<div class="navbar">
  <a href="#home"><img src="typ.png" width="62px"></a>
  <button onclick="myFunction()" class="dropbtn dropdown fa fa-bars fa-lg"></button>
  <div id="myDropdown" class="dropdown-content">
    <a href="#home">Home</a>
    <a href="#about">About</a>
    <a href="#contact">Contact</a>
  </div> 
</div>
<script>
/* When the user clicks on the button, 
toggle between hiding and showing the dropdown content */
function myFunction() {
    document.getElementById("myDropdown").classList.toggle("show");
}

// Close the dropdown if the user clicks outside of it
window.onclick = function(event) {
  if (!event.target.matches('.dropbtn')) {

    var dropdowns = document.getElementsByClassName("dropdown-content");
    var i;
    for (i = 0; i < dropdowns.length; i++) {
      var openDropdown = dropdowns[i];
      if (openDropdown.classList.contains('show')) {
        openDropdown.classList.remove('show');
      }
    }
  }
}
</script>
</body>

3 个答案:

答案 0 :(得分:1)

它实际上是水平的。但是,如果您尝试将其设置为垂直,您是否尝试更改导航栏的高度和宽度?例如:

.navbar {
  overflow: hidden;
  background-color: gold;
  position: fixed;
  top: 0;
      width: 200px;
height: 100%;
    }

希望这会有所帮助。祝你好运。

答案 1 :(得分:1)

您应该将myDropdown DIV重写为ul列表,其中包含li个标签,其中包含您的菜单项a标签。这是现在创建菜单的常规方法。

默认情况下这是垂直的。要使其水平,请将display: inline-block添加到li项。 (并添加list-style:none;以隐藏列表项目符号)

答案 2 :(得分:0)

提出所有建议。我清楚地怀疑了。

对于其他人,代码在这里: -

<style>
body {margin:0;}

.navbar {
  overflow: hidden;
  background-color: gold;
  position: fixed;
  top: 0;
  width: 100%;
}

.navbar a {
  float: left;
  display: block;
  color: #f2f2f2;
  text-align: center;

  text-decoration: none;
  font-size: 17px;
}

.main {

  margin-top: 65px;
}

</style>
<style>
.dropbtn {
    background-color: red;
    color: gold;
    padding: 25px;
    font-size: 16px;
    border: none;
    cursor: pointer;
}
.dropbtn:hover, .dropbtn:focus {
    background-color: gold;
    color: red;
}

.dropdown {
    float: right;
    position: relative;
    display: block;

}

.dropdown-content {
    display: none;
    float: right;
    margin-top: 65px;
    background-color: #f9f9f9;
    min-width: 160px;
    overflow: auto;
    box-shadow: 0px 8px 16px 0px rgba(0,0,0,0.2);
    right: 0;
    z-index: 1;
}

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

.dropdown li:hover {background-color: blue}

.show {display:inline-block;}
</style>
<link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/font-awesome/4.7.0/css/font-awesome.min.css">
</head>
<body>

<div class="navbar">
  <a href="#home"><img src="typ.png" width="62px"></a>
  <button onclick="myFunction()" class="dropbtn dropdown fa fa-bars fa-lg"></button> 
</div>
<ul id="myDropdown" class="dropdown-content">
    <li href="#home">Home</li>
    <li href="#about">About</li>
    <li href="#contact">Contact</li>
  </ul>
<script>
/* When the user clicks on the button, 
toggle between hiding and showing the dropdown content */
function myFunction() {
    document.getElementById("myDropdown").classList.toggle("show");
}

// Close the dropdown if the user clicks outside of it
window.onclick = function(event) {
  if (!event.target.matches('.dropbtn')) {

    var dropdowns = document.getElementsByClassName("dropdown-content");
    var i;
    for (i = 0; i < dropdowns.length; i++) {
      var openDropdown = dropdowns[i];
      if (openDropdown.classList.contains('show')) {
        openDropdown.classList.remove('show');
      }
    }
  }
}
</script>
</body>

谢谢