我一直在为网站寻找不同的菜单样式,这里有一个非常简单的菜单:http://www.w3schools.com/howto/howto_js_topnav.asp
我喜欢菜单的地方在于它反应灵敏,可以与我的菜单一起开箱即用(大多数情况下)。我遇到的唯一问题是子菜单。这个菜单似乎没有任何子菜单的css,但我想它应该不会太难,对吧?我非常接近,但我想我需要稍微调整一下才能让子菜单正常工作。
JavaScript:
<script>
/* Toggle between adding and removing the "responsive" class to topnav when the user clicks on the icon */
function myFunction() {
var x = document.getElementById("myTopnav");
if (x.className === "topnav") {
x.className += " responsive";
} else {
x.className = "topnav";
}
}
</script>
HTML(尽管我删除了一些默认的WordPress类并列出了URL,以便更容易理解):<ul class="topnav" id="myTopnav">
<li><a href="http://url.com/">Home</a></li>
<li>
<a href="http://url.com/">Pictures</a>
<ul class="children">
<li><a href="http://url.com/">Beaver</a></li>
<li>
<a href="http://url.com/">Duck</a>
<ul class="children">
<li><a href="http://url.com/">Fever</a></li>
</ul>
</li>
<li><a href="http://url.com/">Frog</a></li>
</ul>
</li>
<li><a href="http://url.com/">Nominations</a></li>
<li><a href="http://url.com/">Contact</a></li>
<li class="icon">
<a href="javascript:void(0);" onclick="myFunction()">☰</a>
</li>
</ul>
CSS:/* Remove margins and padding from the list, and add a black background color */
ul.topnav {
list-style-type: none;
margin: 0;
padding: 0;
overflow: hidden;
background-color: #333;
}
/* Float the list items side by side */
ul.topnav li {float: left;}
/* Style the links inside the list items */
ul.topnav li a {
display: inline-block;
color: #f2f2f2;
text-align: center;
padding: 14px 16px;
text-decoration: none;
transition: 0.3s;
font-size: 17px;
}
/* Change background color of links on hover */
ul.topnav li a:hover {background-color: #555;}
/* Hide the list item that contains the link that should open and close the topnav on small screens */
ul.topnav li.icon {display: none;}
/* When the screen is less than 680 pixels wide, hide all list items, except for the first one ("Home"). Show the list item that contains the link to open and close the topnav (li.icon) */
@media screen and (max-width:680px) {
ul.topnav li:not(:first-child) {display: none;}
ul.topnav li.icon {
float: right;
display: inline-block;
}
}
/* The "responsive" class is added to the topnav with JavaScript when the user clicks on the icon. This class makes the topnav look good on small screens */
@media screen and (max-width:680px) {
ul.topnav.responsive {position: relative;}
ul.topnav.responsive li.icon {
position: absolute;
right: 0;
top: 0;
}
ul.topnav.responsive li {
float: none;
display: inline;
}
ul.topnav.responsive li a {
display: block;
text-align: left;
}
}
子菜单CSS:/* CSS I added */
ul.topnav .page_item_has_children ul {
display: none;
}
ul.topnav .page_item_has_children:hover ul {
display: block;
}
ul.topnav .page_item_has_children:hover li {
width: 100%;
}
我不确定我做错了什么,但我希望菜单的功能如下:http://blog.teamtreehouse.com/wp-content/uploads/2015/01/css-dropdown.png现在,当我将鼠标悬停在菜单上时,即使我在菜单上设置了最大宽度,二级菜单也会覆盖整个屏幕。
我想我把事情弄得比实际情况复杂得多,有人有什么想法吗?