我正在使用教程构建一个菜单,我通过添加下拉子菜单来扩展它,当你将鼠标悬停在主菜单的项目上时,但是当我将鼠标悬停在菜单项上时,而不仅仅是那个子菜单下降,所有子菜单都下降(但显示为空)。
请参阅以下内容:
https://jsfiddle.net/zx72de55/ HTML:
<body>
<div class="header">
<h1>Basics of Web Design</h1>
<div class="nav">
<a href="#" class="hamburger">
<div class="line"></div>
<div class="line"></div>
<div class="line"></div>
</a>
<ul class="clearfix menu">
<li><a href="#">Home</a></li>
<li><a href="#">HTML</a>
<ul class="submenu">
<li><a href="#">Introduction</a></li>
<li><a href="#">sub heading 2</a></li>
<li><a href="#">sub heading 3</a></li>
<li><a href="#">sub heading 4</a></li>
<li><a href="#">sub heading 5</a></li>
<li><a href="#">sub heading 6</a></li>
</ul>
</li>
<li><a href="#">CSS</a>
<ul class="submenu">
<li><a href="#">Introduction</a></li>
<li><a href="#">sub heading 2</a></li>
<li><a href="#">sub heading 3</a></li>
</ul>
</li>
<li><a href="#">JavScript</a>
</li>
<li><a href="#">Aesthetics</a>
</li>
</ul>
</div>
</div>
</body>
</html>
CSS:
/* resets everything */
*, *:before, *:after {
margin: 0;
padding: 0;
box-sizing: border-box
}
.clearfix:after {
content: "";
display: table;
clear: both;
}
html, body {
height: 100%;
background-image: url(../images/bg-tile.png);
background-repeat: repeat;
}
body {
font: 1em'Roboto', sans-serif;
color: #555;
background-color: #fff;
-webkit-font-smoothing: antialiased;
-moz-osx-font-smoothing: grayscale;
}
ul {
list-style: none;
max-width: 900px;
margin: 0 auto;
border-left: 1px solid #ddd;
border-right: 1px solid #ddd;
background: inherit;
}
h1 {
padding: 30px 0;
font: 1.5em'Open Sans', sans-serif;
text-align: center;
}
.nav {
height: 3.6em;
border-top: 1px solid #ddd;
border-bottom: 1px solid #ddd;
background-color: #f5f5f5;
/* adds box shadow */
-webkit-box-shadow: -1px 3px 5px 0px rgba(52, 44, 77, 1);
-moz-box-shadow: -1px 3px 5px 0px rgba(52, 44, 77, 1);
box-shadow: -1px 3px 5px 0px rgba(52, 44, 77, 1);
}
ul a {
display: block;
padding: 20px;
padding-right: 0 !important;
/* important overrides media queries */
font-size: 13px;
font-weight: bold;
text-align: center;
color: #aaa;
cursor: pointer;
text-decoration: none;
}
ul a:hover {
background: #eee;
}
.nav li {
float: left;
width: 20%;
border-right: 1px solid #ddd;
position: relative;
}
.nav li > ul li{
float: left;
position: relative;
}
.nav li:last-child {
border-right: none;
}
ul.submenu {
display: none;
}
ul.submenu li {
width: 100%;
border-right: none;
}
ul.submenu li a {
width: 100%;
padding: 6px;
}
ul.menu > li:hover ul.submenu {
float: none;
display: block;
}
@media only screen and (max-width: 600px) {
.hamburger {
padding: 15px;
display: block;
}
.line {
border-bottom: 4px solid #bbb;
width: 35px;
margin-bottom: 6px;
}
.line:last-child {
margin-bottom: 0;
}
.nav li {
width: 100%;
}
.menu {
height: 0;
overflow: hidden;
transition: height 0.3s linear;
-webkit-box-shadow: -1px 3px 5px 0px rgba(52, 44, 77, 1);
-moz-box-shadow: -1px 3px 5px 0px rgba(52, 44, 77, 1);
box-shadow: -1px 3px 5px 0px rgba(52, 44, 77, 1);
}
.slide-down {
height: 275px;
}
}
Js for resposive menu:
$('.hamburger').on('click', function(e) {
// Prevent link from jumping to the top of the page
e.preventDefault();
// If menu is already showing, slide it up. Otherwise, slide it down.
$('.menu').toggleClass('slide-down');
});
答案 0 :(得分:1)
在你的CSS中制作这些chage并希望它能够发挥作用。
/* resets everything */
*, *:before, *:after {
margin: 0;
padding: 0;
box-sizing: border-box
}
.clearfix:after {
content: "";
display: table;
clear: both;
}
html, body {
height: 100%;
background-image: url(../images/bg-tile.png);
background-repeat: repeat;
}
body {
font: 1em'Roboto', sans-serif;
color: #555;
background-color: #fff;
-webkit-font-smoothing: antialiased;
-moz-osx-font-smoothing: grayscale;
}
ul {
list-style: none;
max-width: 900px;
margin: 0 auto;
/*border-left: 1px solid #ddd;
border-right: 1px solid #ddd;
background: inharit;*/
}
h1 {
padding: 30px 0;
font: 1.5em'Open Sans', sans-serif;
text-align: center;
}
.nav {
height: 3.6em;
border-top: 1px solid #ddd;
border-bottom: 1px solid #ddd;
background-color: #f5f5f5;
/* adds box shadow */
-webkit-box-shadow: -1px 3px 5px 0px rgba(52, 44, 77, 1);
-moz-box-shadow: -1px 3px 5px 0px rgba(52, 44, 77, 1);
box-shadow: -1px 3px 5px 0px rgba(52, 44, 77, 1);
}
ul a {
display: block;
padding: 20px;
padding-right: 0 !important;
/* important overrides media queries */
font-size: 13px;
font-weight: bold;
text-align: center;
color: #aaa;
cursor: pointer;
text-decoration: none;
}
ul a:hover {
background: #eee;
}
.nav li {
float: left;
width: 20%;
border-right: 1px solid #ddd;
position: relative;
}
.nav li > ul li{
float: left;
position: relative;
}
.nav li:last-child {
border-right: none;
}
ul.submenu {
display: none;
}
ul.submenu li {
width: 100%;
border-right: none;
background: #eee;
}
ul.submenu li a {
width: 100%;
padding: 6px;
}
ul.menu > li:hover ul.submenu {
float: none;
display: block;
background: #eee;
}
@media only screen and (max-width: 600px) {
.hamburger {
padding: 15px;
display: block;
}
.line {
border-bottom: 4px solid #bbb;
width: 35px;
margin-bottom: 6px;
}
.line:last-child {
margin-bottom: 0;
}
.nav li {
width: 100%;
}
.menu {
height: 0;
overflow: hidden;
transition: height 0.3s linear;
-webkit-box-shadow: -1px 3px 5px 0px rgba(52, 44, 77, 1);
-moz-box-shadow: -1px 3px 5px 0px rgba(52, 44, 77, 1);
box-shadow: -1px 3px 5px 0px rgba(52, 44, 77, 1);
}
.slide-down {
height: 275px;
}
}