我如何获得像这张照片的CSS html菜单下拉菜单

时间:2017-12-07 14:49:15

标签: html css menu dropdown

如何获得像这张图片一样好的或类似的HTML / CSS下拉菜单 http://i65.tinypic.com/2s8sc4j.png

我的style.php中有这段代码



/*----- Toggle Button -----*/

.toggle-nav {
  display: none;
}


/*----- Menu -----*/

@media screen and (min-width: 860px) {
  .menu {
    width: 100%;
    padding: 10px 18px;
    box-shadow: 0px 1px 1px rgba(0, 0, 0, 0.15);
    border-radius: 3px;
    background: #303030;
  }
}

.menu ul {
  display: inline-block;
}

.menu li {
  margin: 0px 50px 0px 0px;
  float: left;
  list-style: none;
  font-size: 17px;
}

.menu li:last-child {
  margin-right: 0px;
}

.menu a {
  text-shadow: 0px 1px 0px rgba(0, 0, 0, 0.5);
  color: #777;
  transition: color linear 0.15s;
}

.menu a:hover,
.menu .current-item a {
  text-decoration: none;
  color: #66a992;
}


/*----- Search -----*/

.search-form {
  float: right;
  display: inline-block;
}

.search-form input {
  width: 200px;
  height: 30px;
  padding: 0px 8px;
  float: left;
  border-radius: 2px 0px 0px 2px;
  font-size: 13px;
}

.search-form button {
  height: 30px;
  padding: 0px 7px;
  float: right;
  border-radius: 0px 2px 2px 0px;
  background: #66a992;
  font-size: 13px;
  font-weight: 600;
  text-shadow: 0px 1px 0px rgba(0, 0, 0, 0.3);
  color: #fff;
}


/*----- Responsive -----*/

@media screen and (max-width: 1150px) {
  .wrap {
    width: 90%;
  }
}

@media screen and (max-width: 970px) {
  .search-form input {
    width: 120px;
  }
}

@media screen and (max-width: 860px) {
  .menu {
    position: relative;
    display: inline-block;
  }
  .menu ul.active {
    display: none;
  }
  .menu ul {
    width: 100%;
    position: absolute;
    top: 120%;
    left: 0px;
    padding: 10px 18px;
    box-shadow: 0px 1px 1px rgba(0, 0, 0, 0.15);
    border-radius: 3px;
    background: #303030;
  }
  .menu ul:after {
    width: 0px;
    height: 0px;
    position: absolute;
    top: 0%;
    left: 22px;
    content: '';
    transform: translate(0%, -100%);
    border-left: 7px solid transparent;
    border-right: 7px solid transparent;
    border-bottom: 7px solid #303030;
  }
  .menu li {
    margin: 5px 0px 5px 0px;
    float: none;
    display: block;
  }
  .menu a {
    display: block;
  }
  .toggle-nav {
    padding: 20px;
    float: left;
    display: inline-block;
    box-shadow: 0px 1px 1px rgba(0, 0, 0, 0.15);
    border-radius: 3px;
    background: #303030;
    text-shadow: 0px 1px 0px rgba(0, 0, 0, 0.5);
    color: #777;
    font-size: 20px;
    transition: color linear 0.15s;
  }
  .toggle-nav:hover,
  .toggle-nav.active {
    text-decoration: none;
    color: #66a992;
  }
  .search-form {
    margin: 12px 0px 0px 20px;
    float: left;
  }
  .search-form input {
    box-shadow: -1px 1px 2px rgba(0, 0, 0, 0.1);
  }
}

<nav class="menu">
  <ul class="active">
    <li class="current-item"><a href="#">Home</a></li>
    <li><a href="#">My Work</a></li>
    <li><a href="#">About Me</a></li>
    <li><a href="#">Get in Touch</a></li>
    <li><a href="#">Blog</a></li>
  </ul> <a class="toggle-nav" href="#">&#9776;</a>
  <form class="search-form">
    <input type="text">
    <button>Search</button>
  </form>
</nav>
&#13;
&#13;
&#13;

我的代码的结果如下: http://i67.tinypic.com/qp1l5d.png

点击下拉菜单未打开。我可以实现它或获得类似上面的图片的CSS / HTML菜单吗? 非常感谢您的时间和贡献是我的解决方案

1 个答案:

答案 0 :(得分:0)

我的解决方案是获得附加照片的效果,即修改.menu ul.active类给出它:

  • transition: all .5s;用于平滑滚动
  • overflow:hidden;所以当没有切换时隐藏菜单项
  • height:0px;给它一个默认的高度 - * important

我会创建一个名为.drop的新类,并使用JavaScript在ul之上和之后切换这个新类。请参阅下面的两组修改代码,这里是工作解决方案的JSFiddle链接 - https://jsfiddle.net/2vqp39nd/2/

CSS更改 -

.menu ul.active {
    transition: all .5s;
    height:0px;
    overflow:hidden;
  }

  .drop {
  height:170px!important;
  /* Set this at required height */
}

JavaScript更改 -

var ul = document.getElementsByClassName('active')[0],
  btn = document.getElementsByClassName('toggle-nav')[0];

  function dropDown () {
        ul.classList.toggle('drop');
    }

  btn.addEventListener('click',dropDown,false);