具有下拉菜单的响应式导航栏,可在下拉列表中滑动

时间:2018-01-15 13:54:07

标签: html css

我尝试使用下拉菜单制作响应式导航栏。我想要下拉菜单打开下拉菜单容器下面的verticaly(幻灯片)。我尝试了一些东西但却无法实现我想要的东西。我希望它能像在图片上一样。我想和Google Pixel 2"和" LG V30"滑下来和#34;我们是谁"和"我们做什么"。

enter image description here



.navbar {
  background-color: #31577C;
  overflow: hidden;
  width: 100%;
}

ul {
  font-family: Arial;
  text-shadow: 0px 0px 20px black;
  list-style-type: none;
  margin: 0 auto;
  padding: 0;
  text-align: center;
}

li {
  float: left;
}

li a,
.dropbtn {
  display: inline-block;
  color: white;
  text-align: center;
  padding: 14px 16px;
  text-decoration: none;
}

li a:hover,
.dropdown:hover {
  background-color: #97C026;
  transition: background-color 0.5s ease;
}

.dropbtn {
  background-color: #31577C;
}

li.dropdown {
  display: inline-block;
}

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

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

.dropdown-content a:hover {
  background-color: #97C026;
}

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

.active {
  background-color: #97C026;
}

@media only screen and (max-width: 768px) {
  ul {
    position: static;
    text-align: center;
  }
  li {
    margin-bottom: 1px;
  }
  ul li,
  li a {
    width: 100%;
  }
  .dropdown-content a {
    text-align: center;
  }
}

<div class="navbar">
  <ul align="center">
    <li><a href="Naslovna.html">Naslovna</a></li>
    <li class="dropdown"><a href="Uređaji.html" class="dropbtn">Uređaji</a>
      <div class="dropdown-content">
        <a href="Pixel.html">Google Pixel 2 </a>
        <a href="V30.html">LG V30</a>
      </div>
    </li>
    <li><a href="Softver.html">Softver</a></li>
    <li class="active"><a href="Registracija.html">Registracija </a> </li>
  </ul>
</div>
&#13;
&#13;
&#13;

1 个答案:

答案 0 :(得分:1)

试试这个代码段。我已删除Uređaji.html上的超链接,否则您无法使用下拉菜单。希望这不是问题,不得不改变几个css部分。

&#13;
&#13;
(function($) { // Begin jQuery
  $(function() { // DOM ready
    // If a link has a dropdown, add sub menu toggle.
    $('nav ul li a:not(:only-child)').click(function(e) {
      $(this).siblings('.nav-dropdown').toggle();
      // Close one dropdown when selecting another
      $('.nav-dropdown').not($(this).siblings()).hide();
      e.stopPropagation();
    });
    // Clicking away from dropdown will remove the dropdown class
    $('html').click(function() {
      $('.nav-dropdown').hide();
    });
    // Toggle open and close nav styles on click
    $('#nav-toggle').click(function() {
      $('nav ul').slideToggle();
    });
    // Hamburger to X toggle
    $('#nav-toggle').on('click', function() {
      this.classList.toggle('active');
    });
  }); // end DOM ready
})(jQuery); // end jQuery
&#13;
@charset "UTF-8";
.navigation {
  height: 4rem;
  background: #31577C;
  margin-top: -0.5rem;
}

.nav-container {
  max-width: 1000px;
  margin: 0;
}

nav {
  float: left;
}

nav ul {
  list-style: none;
  margin: 0;
  padding: 0;
}

nav ul li {
  float: left;
  position: relative;
}

nav ul li a,
nav ul li a:visited {
  display: block;
  padding: 0 20px;
  line-height: 4rem;
  background: #31577C;
  color: #ffffff;
  text-decoration: none;
}

nav ul li a:hover,
nav ul li a:visited:hover {
  background: #97C026;
  color: #ffffff;
}

nav ul li a:not(:only-child):after,
nav ul li a:visited:not(:only-child):after {
  padding-left: 4px;
  content: ' ▾';
}

nav ul li ul li {
  min-width: 190px;
}

nav ul li ul li a {
  padding: 15px;
  line-height: 20px;
}

.nav-dropdown {
  position: absolute;
  display: none;
  z-index: 1;
  box-shadow: 0 3px 12px rgba(0, 0, 0, 0.15);
}


/* Mobile navigation */

.nav-mobile {
  display: none;
  position: absolute;
  top: 0;
  right: 0;
  background: #31577C;
  height: 4rem;
  width: 4rem;
}

@media only screen and (max-width: 798px) {
  .nav-mobile {
    display: block;
  }
  nav {
    width: 100%;
    padding: 70px 0 15px;
  }
  nav ul {
    display: none;
  }
  nav ul li {
    float: none;
  }
  nav ul li a {
    padding: 15px;
    line-height: 20px;
  }
  nav ul li ul li a {
    padding-left: 30px;
  }
  .nav-dropdown {
    position: static;
  }
}

@media screen and (min-width: 799px) {
  .nav-list {
    display: block !important;
  }
}

#nav-toggle {
  position: absolute;
  left: 18px;
  top: 22px;
  cursor: pointer;
  padding: 10px 35px 16px 0px;
}

#nav-toggle span,
#nav-toggle span:before,
#nav-toggle span:after {
  cursor: pointer;
  border-radius: 1px;
  height: 5px;
  width: 35px;
  background: #ffffff;
  position: absolute;
  display: block;
  content: '';
  transition: all 300ms ease-in-out;
}

#nav-toggle span:before {
  top: -10px;
}

#nav-toggle span:after {
  bottom: -10px;
}

#nav-toggle.active span {
  background-color: transparent;
}

#nav-toggle.active span:before,
#nav-toggle.active span:after {
  top: 0;
}

#nav-toggle.active span:before {
  transform: rotate(45deg);
}

#nav-toggle.active span:after {
  transform: rotate(-45deg);
}
&#13;
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>

<section class="navigation">
  <div class="nav-container">
    <nav>
      <div class="nav-mobile"><a id="nav-toggle" href="#!"><span></span></a></div>
      <ul class="nav-list">

        <li><a href="Naslovna.html">Naslovna</a></li>
        <li><a href="#">Uređaji</a>

          <ul class="nav-dropdown">
            <li><a href="Pixel.html">Google Pixel 2 </a></li>
            <li><a href="V30.html">LG V30</a></li>
          </ul>

        </li>
        <li><a href="Softver.html">Softver</a></li>
        <li class="active"><a href="Registracija.html">Registracija </a> </li>
      </ul>
    </nav>
  </div>
</section>
&#13;
&#13;
&#13;