如何在css中修复导航栏下拉菜单

时间:2015-11-25 17:03:50

标签: html css drop-down-menu

当我将鼠标悬停在服务菜单中时,我的下拉菜单出现问题。 Here's the problem (on Codepen)



*{
	margin: 0;
	padding: 0;
}
body{
	margin: 0 auto;
	width: 800px;
}
a{
	font-family: 'Alegreya Sans Regular';
}

#navigation{
	height: 113px;
	width: 800px;
	background-color: #17384D;
}
.weblogo{
	margin-top: 34px;
	margin-left: 20px;
}
nav{
	float: right;
	margin-top: 60px;
	margin-right: 60px;	
	

}

nav ul ul{
	display: none;

}
nav ul li:hover > ul {
		display: block;

	}
nav ul li{
	display: inline-block;
	padding-left: 45px;

}
nav ul li a{
	text-decoration: none;
	list-style: none;
	color: #FAD46B;
	font-size: 1.3em;

}
.active{
	border-bottom: 2px solid #FF972C;
	padding-left: 0px;
}

<div id="navigation">
		<img class="weblogo"src="weblogo.png"></img>

		<nav>
			<ul>
				<li class="active"><a href="#">home</a></li>
				<li><a href="#">work</a></li>
				<li><a href="#">about</a></li>
				<li class="dropdown"><a href="#">services</a>
					<ul>
						<li><a href="#">Search</a></li>
						<li><a href="#">Search</a></li>
						<li><a href="#">Search</a></li>
						<li><a href="#">Search</a></li>
					</ul>
				</li>
				<li><a href="#">contact</a></li>
			</ul>
		</nav>
&#13;
&#13;
&#13;

1 个答案:

答案 0 :(得分:0)

创建子菜单的更好方法是

用这个

替换你的html

HTML

    <div id="navigation">
    <img class="weblogo"src="weblogo.png"></img>

    <nav>
        <ul>
            <li class="active"><a href="#">home</a></li>
            <li><a href="#">work</a></li>
            <li><a href="#">about</a></li>
            <li class="dropdown"><a onClick="toggleNavPanel('panel')" >services</a>

                <div id="panel">
                  <div>
                 <ul>
                    <li><a href="#">Search</a></li>
                    <li><a href="#">Search</a></li>
                    <li><a href="#">Search</a></li>
                    <li><a href="#">Search</a></li>
                </ul>
                  </div>
               </div>

            </li>
            <li><a href="#">contact</a></li>
        </ul>
    </nav>

并将此添加到您的css

#panel {
  position: absolute;
  background-color: red;
  height: 0;
  width: 100px;
  top: 120px;
  transition: height 0.3s linear 0s;
  overflow:hidden;
 }

添加此javascript

    function toggleNavPanel(x) {
  var panel = document.getElementById(x), maxH="300px";
    if(panel.style.height == maxH) {
      panel.style.height = "0px";
  }else {
    panel.style.height = maxH;
  }
}

同样从你的css中删除它

nav ul li:hover > ul {
    display: block;
}