自适应导航栏图标不起作用

时间:2018-08-01 06:18:08

标签: html css

好的,所以当我在codepen.io上看到这支笔时,我试图制作一个响应式导航栏,其中的家伙使用纯CSS制作了响应式导航栏。 我想知道我是否可以做同样的事情,所以我试图复制它,我做了大多数事情,但是我有一个问题。当我单击菜单图标时,它什么也没做。

这是我的代码 index.html

 .navbar {
  height: 50px;
  width: 100%;
  background: #333;
  position: absolute;
  top: 0;
  left: 0;
  color: white;
  font-family: Arial;
}
.navbar h3 {
  float: left;
  margin-left: 20px;
  margin-top: 14px;
}
.navbar .nav-links a {
  float: right;
  padding: 15px;
  color: white;
  text-decoration: none;
}
.navbar a:hover {
  float: right;
  padding: 15px;
  background: #555;
  color: white;
  text-decoration: none;
}
.navbar span,
.navbar input {
  display: none;
}
@media (max-width: 550px) {
  .navbar .nav-links a {
    display: none;
  }
  .navbar span {
    float: right;
    font-size: 30px;
    margin-right: 20px;
    display: block;
  }
  .navbar input[type="checkbox"] {
    display: none;
  }
}
<div class="navbar">
          <h3>Augma Tech</h3>
          <div class="nav-links" id="nav-links">
            <a href="#">Contact</a>
            <a href="#">Services</a>
            <a href="#">Home</a>
          </div>
          <span>
            <label for="check">&#9776;</label>
            <input type="checkbox" name="check" id="check">
          </span>
</div>

    .navbar {
  height: 50px;
  width: 100%;
  background: #333;
  position: absolute;
  top: 0;
  left: 0;
  color: white;
  font-family: Arial;
}
.navbar h3 {
  float: left;
  margin-left: 20px;
  margin-top: 14px;
}
.navbar .nav-links a {
  float: right;
  padding: 15px;
  color: white;
  text-decoration: none;
}
.navbar a:hover {
  float: right;
  padding: 15px;
  background: #555;
  color: white;
  text-decoration: none;
}
.navbar span,
.navbar input {
  display: none;
}
@media (max-width: 550px) {
  .navbar .nav-links a {
    display: none;
  }
  .navbar span {
    float: right;
    font-size: 30px;
    margin-right: 20px;
    display: block;
  }
  .navbar input[type="checkbox"] {
    display: none;
  }
}

1 个答案:

答案 0 :(得分:0)

根据您附带的密码笔:https://codepen.io/jo_Geek/pen/xgbaEr

这是工作代码:

请参见小提琴:https://jsfiddle.net/d07g34zv/10/

tj_mdio_bus            22185  0 
mvDmaDrv                4988  0 
mvIntDrv                5077  18446744073709551614 
mvMbusDrv               4849  18446744073709551614
   * {
 box-sizing: border-box;
}
body {
  margin: 0px;
    font-family: Arial;
}

.nav {
  height: 50px;
  width: 100%;
  background-color: #4d4d4d;
  position: relative;
}

.nav > .nav-header {
  display: inline;
}

.nav > .nav-header > .nav-title {
  display: inline-block;
  font-size: 22px;
  color: #fff;
  padding: 10px 10px 10px 10px;
}

.nav > .nav-btn {
  display: none;
}

.nav > .nav-links {
  display: inline;
  float: right;
  font-size: 18px;
}

.nav > .nav-links > a {
  display: inline-block;
  padding: 13px 10px 13px 10px;
  text-decoration: none;
  color: #efefef;
}

.nav > .nav-links > a:hover {
  background-color: rgba(0, 0, 0, 0.3);
}

.nav > #nav-check {
  display: none;
}

@media (max-width:600px) {
  .nav > .nav-btn {
    display: inline-block;
    position: absolute;
    right: 0px;
    top: 0px;
  }
  .nav > .nav-btn > label {
    display: inline-block;
    width: 50px;
    height: 50px;
    padding: 13px;
  }
  .nav > .nav-btn > label:hover {
    background-color: rgba(0, 0, 0, 0.3);
  }
  .nav > .nav-btn > label > span {
    display: block;
    width: 25px;
    height: 10px;
    border-top: 2px solid #eee;
  }
  .nav > .nav-links {
    position: absolute;
    display: block;
    width: 100%;
    background-color: #333;
    height: 0px;
    transition: all 0.3s ease-in;
    overflow-y: hidden;
    top: 50px;
    left: 0px;
  }
  .nav > .nav-links > a {
    display: block;
    width: 100%;
  }
  .nav > #nav-check:not(:checked) + .nav-links {
    height: 0px;
  }
  .nav > #nav-check:checked + .nav-links {
    height: calc(100vh - 50px);
    overflow-y: auto;
  }
}