像图中的css一样制作下拉菜单

时间:2017-02-07 14:44:21

标签: javascript jquery html css css3

我对如何重现以下内容有疑问:

enter image description here

我有这样的菜单:

body {
  background: white;
  margin: 0;
  padding: 0;
}
/*
/ nav
*/

nav {
  width: 100%;
  background: #000;
  border-bottom: 5px solid white;
  position: relative;
  font-family: 'Decker';
}
nav:after {
  content: '';
  height: 8px;
  width: 100%;
  background: inherit;
  position: absolute;
  bottom: -15px;
  left: 0px;
  z-index: 1;
}
nav ul {
  display: -webkit-box;
  display: -ms-flexbox;
  display: flex;
  max-width: 100%;
  margin: auto;
  -webkit-box-pack: center;
  -ms-flex-pack: center;
  justify-content: center;
  -webkit-box-align: center;
  -ms-flex-align: center;
  align-items: center;
  padding-bottom: .6em;
}
nav li {
  display: inline-block;
  list-style: none;
  position: relative;
  padding: 0 .5em;
}
nav li:last-child a:before {
  display: none;
}
nav li a {
  color: #fff;
  display: inline-block;
  padding: 1.6em 0.6em 0.7em 0.6em;
  text-decoration: none;
  position: relative;
  font-size: 18px;
  line-height: 1;
}
nav li a:before {
  content: "|";
  display: block;
  position: absolute;
  right: -10px;
  top: 1.6em;
  -webkit-transform: translateY(-4%);
  transform: translateY(-4%);
  line-height: inherit;
  font-size: inherit;
  color: #fff;
}
nav li a:after {
  display: none;
  content: "";
  position: absolute;
  bottom: -25px;
  left: 0px;
  width: 100%;
  height: 8px;
  background: #fac312;
  z-index: 2;
}
nav li a:hover {
  background: #b42024;
  color: #fff;
  text-decoration: none;
}
nav li a.active {
  background: #b42024;
  color: #fff;
  text-decoration: none;
}
nav li a.active:after {
  display: block;
  content: "";
  position: absolute;
  bottom: -25px;
  left: 0px;
  width: 100%;
  height: 8px;
  background: #fac312;
  z-index: 2;
}
nav li a:hover:after {
  display: block;
}
<nav>
  <ul>
    <li>
      <a href="" class="active">Home</a>
    </li>
    <li>
      <a href="">About Us</a>
    </li>
    <li>
      <a href="">Products</a>
    </li>
    <li>
      <a href="">Contact</a>
    </li>
  </ul>
</nav>

但是,我不知道如何制作如上图所示的下拉列表。

我希望有人可以帮助我。提前谢谢!

3 个答案:

答案 0 :(得分:1)

嘿这就是你想要的:)我希望

body {
  background: white;
  margin: 0;
  padding: 0;
}

nav {
  width: 100%;
  background: #000;
  border-bottom: 5px solid white;
  position: relative;
}
nav:after {
  content: '';
  height: 8px;
  width: 100%;
  background: inherit;
  position: absolute;
  bottom: -15px;
  left: 0px;
  z-index: 1;
}
nav ul {
  list-style-type: none;
  margin: 0;
  padding: 0;
}
nav a {
  color: #fff;
  text-decoration: none;
}

.nav__cat {
  display: -webkit-box;
  display: -ms-flexbox;
  display: flex;
  max-width: 100%;
  margin: auto;
  -webkit-box-pack: center;
      -ms-flex-pack: center;
          justify-content: center;
  -webkit-box-align: center;
      -ms-flex-align: center;
          align-items: center;
  padding-bottom: .6em;
}

.nav__li {
  display: inline-block;
  list-style: none;
  position: relative;
  padding: 0 .5em;
}
.nav__li:last-child a:before {
  display: none;
}
.nav__li:hover > a {
  background: red;
}
.nav__li:hover > a:after {
  display: block;
}
.nav__li:hover .sub__category {
  visibility: visible;
  opacity: 1;
}

.nav__li > a {
  display: inline-block;
  padding: 25.6px 0.6em 0.7em 0.6em;
  position: relative;
  font-size: 18px;
  line-height: 1;
}
.nav__li > a:before {
  content: "|";
  display: block;
  position: absolute;
  right: -10px;
  top: 25.6px;
  -webkit-transform: translateY(-4%);
          transform: translateY(-4%);
  line-height: inherit;
  font-size: inherit;
}
.nav__li > a:after {
  display: none;
  content: "";
  position: absolute;
  bottom: -25px;
  left: 0px;
  width: 100%;
  height: 8px;
  background: #ffaf1a;
  z-index: 2;
}

.sub__category {
  visibility: hidden;
  opacity: 0;
}

.sub__category {
  position: absolute;
  top: 100%;
  left: 0px;
  min-width: 160px;
  width: 100%;
  z-index: 3;
  margin: 0 .5em;
  padding-top: 25.6px;
  -webkit-transition: all .12s linear;
  transition: all .12s linear;
}

.sub__li {
  text-align: center;
  border-bottom: 2px #000 solid;
  background: red;
}

.sub__link {
  padding: .7em 1em;
  display: block;
  font-size: 14px;
}
.sub__link:hover {
  background: #fff;
  color: #000;
}
<nav>
 <ul class="nav__cat">
  <li class="nav__li"><a href="">Menu 1</a></li>
  <li class="nav__li"><a href="">Menu 23</a>
   <ul class="sub__category">
     <li class="sub__li">
       <a href="#" class="sub__link">Subcategory</a>
     </li>
      <li class="sub__li">
       <a href="#" class="sub__link">Subcategory 2 </a>
     </li>
      <li class="sub__li">
       <a href="#" class="sub__link">Subcategory 3 </a>
     </li>
    </ul>
   </li>
  <li class="nav__li"><a href="">Menu 345</a>
   <ul class="sub__category">
     <li class="sub__li">
       <a href="#" class="sub__link">Subcategory</a>
     </li>
      <li class="sub__li">
       <a href="#" class="sub__link">Subcategory 2 </a>
     </li>
      <li class="sub__li">
       <a href="#" class="sub__link">Subcategory 3 </a>
     </li>
    </ul>
   </li>
  <li class="nav__li"><a href="">Menu 4567</a></li>
  <li class="nav__li"><a href="">Menu 56789</a></li>
 </ul>
</nav>

答案 1 :(得分:0)

对于初学者,你显然需要在每个菜单中都有一些内容!

请务必在整页中打开此内容。

为了达到你想要的效果,仍然需要做更多的造型工作,但希望这有助于你朝着正确的方向前进。

body {
  background: white;
  margin: 0;
  padding: 0;
}

/*
/ nav
*/
nav {
  width: 100%;
  background: #000;
  border-bottom: 5px solid white;
  position: relative;
  font-family: 'Decker';
}

nav:after {
  content: '';
  height: 8px;
  width: 100%;
  background: inherit;
  position: absolute;
  bottom: -15px;
  left: 0px;
  z-index: 1;
}

nav .mainmenu {
  /*display: -webkit-box;
  display: -ms-flexbox;
  display: flex;*/
  text-align: center;
  max-width: 100%;
  margin: auto;
  -webkit-box-pack: center;
  -ms-flex-pack: center;
  justify-content: center;
  -webkit-box-align: center;
  -ms-flex-align: center;
  align-items: center;
  padding-bottom: .6em;
}

nav .firstlevel {
  display: inline-block;
  list-style: none;
  position: relative;
  padding: 0 .5em;
}

nav .firstlevel:last-child a:before {
  display: none;
}

nav .firstlevel a {
  width: 120px;
  color: #fff;
  display: inline-block;
  padding: 1.6em 0.6em 0.7em 0.6em;
  text-decoration: none;
  position: relative;
  font-size: 18px;
  line-height: 1;
}

nav .firstlevel a:before {
  content: "|";
  display: block;
  position: absolute;
  right: -10px;
  top: 1.6em;
  -webkit-transform: translateY(-4%);
  transform: translateY(-4%);
  line-height: inherit;
  font-size: inherit;
  color: #fff;
}

nav .firstlevel a:after {
  display: none;
  content: "";
  position: absolute;
  bottom: -25px;
  left: 0px;
  width: 100%;
  height: 8px;
  background: #fac312;
  z-index: 2;
}

nav .firstlevel a:hover {
  background: #b42024;
  color: #fff;
  text-decoration: none;
}

nav .firstlevel a.active {
  background: #b42024;
  color: #fff;
  text-decoration: none;
}

nav .firstlevel a.active:after {
  display: block;
  content: "";
  position: absolute;
  bottom: -25px;
  left: 0px;
  width: 100%;
  height: 8px;
  background: #fac312;
  z-index: 2;
}

nav .firstlevel a:hover:after {
  display: block;
}

.firstlevel{
  vertical-align: top;
}

.submenu_group{
  display:none;
  padding-left: 0px;
}

li{
  list-style-type: none;
}

li .submenu{
  display: block;
  width: 120px;
}

.active:hover .submenu_group{
  display: block;
}
<body>
  <nav>
    <ul class="mainmenu">
      <li class='firstlevel active'>
        <a href="" class="active">Home</a>
        <ul class='submenu_group'>
          <li>
            <a class='submenu'>first item</a>
          </li>
          <li>
            <a class='submenu'>second item</a>
          </li>
          <li>
            <a class='submenu'>third item</a>
          </li>
        </ul>
      </li>
      <li class='firstlevel'>
        <div>
          <a href="">About Us</a>
        </div>
      </li>
      <li class='firstlevel'>
        <div>
          <a href="">Products</a>
        </div>
      </li>
      <li class='firstlevel'>
        <div>
          <a href="">Contact</a>
        </div>
      </li>
    </ul>
  </nav>
</body>

答案 2 :(得分:0)

这里有一个改进的@Our_Benfactors答案 fiddle

&#13;
&#13;
		body {
		  background: white;
		  margin: 0;
		  padding: 0;
		}

		/*
		/ nav
		*/
		nav {
		  width: 100%;
		  background: #000;
		  border-bottom: 5px solid white;
		  position: relative;
		  font-family: 'Decker';
		}

		nav:after {
		  content: '';
		  height: 8px;
		  width: 100%;
		  background: inherit;
		  position: absolute;
		  bottom: -15px;
		  left: 0px;
		  z-index: 1;
		}

		nav .mainmenu {
		  /*display: -webkit-box;
		  display: -ms-flexbox;
		  display: flex;*/
		  text-align: center;
		  max-width: 100%;
		  margin: auto;
		  -webkit-box-pack: center;
		  -ms-flex-pack: center;
		  justify-content: center;
		  -webkit-box-align: center;
		  -ms-flex-align: center;
		  align-items: center;
		  padding-bottom: .6em;
		}

		nav .firstlevel {
		  display: inline-block;
		  list-style: none;
		  position: relative;
		  padding: 0 .5em;
		}

		nav .firstlevel:last-child a:before {
		  display: none;
		}

		nav .firstlevel a {
		  width: 120px;
		  color: #fff;
		  display: inline-block;
		  padding: 1.6em 0.6em 0.7em 0.6em;
		  text-decoration: none;
		  position: relative;
		  font-size: 18px;
		  line-height: 1;
		}

		nav .firstlevel a:before {
		  content: "|";
		  display: block;
		  position: absolute;
		  right: -10px;
		  top: 1.6em;
		  -webkit-transform: translateY(-4%);
		  transform: translateY(-4%);
		  line-height: inherit;
		  font-size: inherit;
		  color: #fff;
		}

		nav .firstlevel a:after {
		  display: none;
		  content: "";
		  position: absolute;
		  bottom: -25px;
		  left: 0px;
		  width: 100%;
		  height: 8px;
		  background: #fac312;
		  z-index: 2;
		}

		nav .firstlevel a:hover {
		  background: #b42024;
		  color: #fff;
		  text-decoration: none;
		}

		nav .firstlevel a.active {
		  background: #b42024;
		  color: #fff;
		  text-decoration: none;
		}

		nav .firstlevel a.active:after {
		  display: block;
		  content: "";
		  position: absolute;
		  bottom: -25px;
		  left: 0px;
		  width: 100%;
		  height: 8px;
		  background: #fac312;
		  z-index: 2;
		}
		.active .submenu_group a:after {
			background: transparent;
		}
		.active .submenu_group a {
			background:#b42024;
		}
		.active .submenu_group a:hover {
			background: #fff;
			color:#000;
			border-top: 2px inset #000;
			border-bottom: 2px inset #000;
		}
		.active .submenu_group li:first-child a:hover {
			border-top: 0;

		}
		nav .firstlevel a:hover:after {
		  display: block;
		}

		.firstlevel{
		  vertical-align: top;
		}

		.submenu_group{
		  display:none;
		  padding-left: 0px;
		}
		.active .submenu_group li:first-child a {
			margin-top: 28px;
		}
		.active .submenu_group a {
			padding: 10px;
		}
		.active:hover .submenu_group {
			display: block;
			position: absolute;
			background: #000;
		}
		li{
		  list-style-type: none;
		}

		li .submenu{
		  display: block;
		  width: 120px;
		}

		.active:hover .submenu_group{
		  display: block;
		}
&#13;
		<body>
		  <nav>
			<ul class="mainmenu">
			  <li class='firstlevel active'>
				<a href="" class="active">Home</a>
				<ul class='submenu_group'>
				  <li>
					<a class='submenu'>first item</a>
				  </li>
				  <li>
					<a class='submenu'>second item</a>
				  </li>
				  <li>
					<a class='submenu'>third item</a>
				  </li>
				</ul>
			  </li>
			  <li class='firstlevel'>
				<div>
				  <a href="">About Us</a>
				</div>
			  </li>
			  <li class='firstlevel'>
				<div>
				  <a href="">Products</a>
				</div>
			  </li>
			  <li class='firstlevel'>
				<div>
				  <a href="">Contact</a>
				</div>
			  </li>
			</ul>
		  </nav>
		</body>
&#13;
&#13;
&#13;

相关问题