如何仅使用 HTML 和 CSS(也许很小的 JavaScript)制作多层下拉菜单
CSS 新手,只能找到单级菜单。这是菜单和列表项:
<ul>
<li>
<a href="#">Home</a>
</li>
<li>
<a href="#">Forums</a>
<ul>
<a href="#">Basketball</a>
<ul>
<li>
<a href="#">Trading</a>
</li>
<li>
<a href="#">Personal Collections</a>
</li>
<li>
<a href="#">Box Breaks</a>
</li>
</ul>
</ul>
</li>
</ul>
正如您所看到的,它将是多层的。现在,使用我拥有的 CSS,首先仅显示主页和论坛,当我将鼠标悬停在论坛上时,会显示篮球......但后续菜单项也是如此。我希望它们保持隐藏状态,直到我将鼠标悬停在篮球上。有人知道如何仅使用 CSS 或尽可能少的 JavaScript 来做到这一点吗?谢谢。这是我的 CSS 代码:
ul
{
margin: 0;
padding: 0;
list-style: none;
width: 150px;
border-bottom: 1px solid #ccc;
}
ul li
{
position: relative;
}
li ul
{
position: absolute;
left: 149px;
top: 0;
display: none;
}
ul li a
{
display: block;
text-decoration: none;
color: #777;
background: #bad8f8;
padding: 2px 0 2px 10px;
border: 1px solid #ccc;
border-bottom: 0;
}
li:hover ul
{
display: block;
}
/* Styles for validation helpers
-----------------------------------------------------------*/
.field-validation-error
{
color: #ff0000;
}
.field-validation-valid
{
display: none;
}
.input-validation-error
{
border: 1px solid #ff0000;
background-color: #ffeeee;
}
.validation-summary-errors
{
font-weight: bold;
color: #ff0000;
}
.validation-summary-valid
{
display: none;
}
Newbie to CSS and can only find single level menus. Here's the Menu and the list items:
<ul>
<li>
<a href="#">Home</a>
</li>
<li>
<a href="#">Forums</a>
<ul>
<a href="#">Basketball</a>
<ul>
<li>
<a href="#">Trading</a>
</li>
<li>
<a href="#">Personal Collections</a>
</li>
<li>
<a href="#">Box Breaks</a>
</li>
</ul>
</ul>
</li>
</ul>
As you can see, it would be multi-tiered. Now, with the CSS I have, only Home and Forums are displayed first, and when I hover over Forums, Basketball is displayed...but so are the subsequent menu items. I want those to stay hidden until I hover over basketball. Anyone know how to do this with just CSS or as little JavaScript as possible? Thanks. Here's the CSS code I have:
ul
{
margin: 0;
padding: 0;
list-style: none;
width: 150px;
border-bottom: 1px solid #ccc;
}
ul li
{
position: relative;
}
li ul
{
position: absolute;
left: 149px;
top: 0;
display: none;
}
ul li a
{
display: block;
text-decoration: none;
color: #777;
background: #bad8f8;
padding: 2px 0 2px 10px;
border: 1px solid #ccc;
border-bottom: 0;
}
li:hover ul
{
display: block;
}
/* Styles for validation helpers
-----------------------------------------------------------*/
.field-validation-error
{
color: #ff0000;
}
.field-validation-valid
{
display: none;
}
.input-validation-error
{
border: 1px solid #ff0000;
background-color: #ffeeee;
}
.validation-summary-errors
{
font-weight: bold;
color: #ff0000;
}
.validation-summary-valid
{
display: none;
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
这是一个工作演示:
http://jsfiddle.net/rcravens/aqz8q/
我做了两件事。
对 ul/li 列表进行了一些重组。有些元素不在 li 中。
对
使用 'li:hover > ul' 仅选择直接子级。
希望有帮助。
鲍勃
Here is a working demo:
http://jsfiddle.net/rcravens/aqz8q/
Two things I did.
A bit of restructuring the ul/li list. There were some elements not in the li.
Used 'li:hover > ul' to select the direct children only.
Hope that helps.
Bob
尝试添加
并从那里开始:)
Try to add
And go from there :)
您构建 css 的方式会导致“篮球”的所有后代继承其 css。您应该使用 child(">") 或 :first-child 选择器。请参阅第 5.5 节和第 5.6 节,了解我在说什么:http://www.w3.org/TR/CSS2/selector.html w3.org/TR/CSS2/selector.html
如果您正在寻找动态菜单,我强烈建议您使用 javascript 而不是纯粹依赖 css,除非您确定很多人会访问您的网站他们的 javascript 已关闭。
The way you have structured your css causes all of "Basketball"'s descendants to inherit its css. You should be using a child(">") or :first-child selector instead. Look at sections 5.5 and 5.6 here to know what I am talking about: http://www.w3.org/TR/CSS2/selector.html
If you are looking to do dynamic menus I'd strongly recommend using javascript over relying purely on css, unless you are certain a lot of people viewing your website are going to have their javascript turned off.