仅 CSS 下拉菜单

发布于 2024-09-11 09:15:40 字数 1700 浏览 6 评论 0原文

我正在尝试制作 CSS 下拉菜单(不涉及 JavaScript)。根据 http://pixelspread.com/blog/289/css-drop-down- menu 我只需要添加

#menuBar #test2 a:hover .subMenu{display:block;}   

即可显示子菜单。但是,在我的代码中,它不起作用。有人可以帮我解决这个问题吗?多谢!

我的 html

<ul id="menuBar">
   <li id="test1">test1</li>
   <li id="test2"><a href="#">Pro1</a>
     <div class="subMenu">
        <ul>
           <li><a href="#">sub1</a></li>  
           <li><a href="#">sub2</a></li>
           <li><a href="#">sub3</a></li>
         </ul>
         <ul>
            <li><a href="#">Volleyball</a></li>
            <li><a href="#">Walking</a></li>
            <li><a href="#">Water Shoes</a></li>
         </ul>
       </div> <!--end of submenu-->
     </li>
  </ul>

我的 CSS

 #menuBar #test2 a{
background:url("../images/btTest.jpg") no-repeat bottom;
display:block;
border-right:1px solid #ffffff;
width:112px;
height:37px;
}

#menuBar #test2 a:hover{
background:url("../images/btTest.jpg") no-repeat top;
}

#menuBar #test2 a:hover .subMenu{  
// I add .subMenu after a:hover and have two a:hover for #test2 a
// I know it won't work but not sure what to do now.
//thanks for the help.
display:block;
}


.subMenu{  // the hidden menu
position:absolute;
top:35px;
left:0px;
z-index: 99999;
width:550px;
background-color:black;
display:none;
}

I am trying to make CSS drop down menu (no javascript involved). According to
http://pixelspread.com/blog/289/css-drop-down-menu I only need to add

#menuBar #test2 a:hover .subMenu{display:block;}   

to make the sub menu show up. However, in my code, it doesn't work. Could someone help me about this issue? Thanks a lot!

My html

<ul id="menuBar">
   <li id="test1">test1</li>
   <li id="test2"><a href="#">Pro1</a>
     <div class="subMenu">
        <ul>
           <li><a href="#">sub1</a></li>  
           <li><a href="#">sub2</a></li>
           <li><a href="#">sub3</a></li>
         </ul>
         <ul>
            <li><a href="#">Volleyball</a></li>
            <li><a href="#">Walking</a></li>
            <li><a href="#">Water Shoes</a></li>
         </ul>
       </div> <!--end of submenu-->
     </li>
  </ul>

My Css

 #menuBar #test2 a{
background:url("../images/btTest.jpg") no-repeat bottom;
display:block;
border-right:1px solid #ffffff;
width:112px;
height:37px;
}

#menuBar #test2 a:hover{
background:url("../images/btTest.jpg") no-repeat top;
}

#menuBar #test2 a:hover .subMenu{  
// I add .subMenu after a:hover and have two a:hover for #test2 a
// I know it won't work but not sure what to do now.
//thanks for the help.
display:block;
}


.subMenu{  // the hidden menu
position:absolute;
top:35px;
left:0px;
z-index: 99999;
width:550px;
background-color:black;
display:none;
}

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(5

骄傲 2024-09-18 09:15:40

您的 HTML 结构未设置为允许使用单个 css 语句使用多个子菜单。如果您查看 Mcinerney 的 HTML:

<div id="menu">
  <ul id="item1">
    <li class="top">menu item</li> 
    <li class="item"><a href="#">menu item 1</a></li> 
    <li class="item"><a href="#">menu item 2</a></li> 
    <li class="item"><a href="#">menu item 3</a></li> 
  </ul> 
</div>

和他的 css:,

#menu ul:hover .item{display:block;}

它会翻译为“如果您将鼠标悬停在 id 为“menu”的元素的后代“ul”上,则查找属于该“ul”后代的所有元素类“item”并将其显示设置为“block”

您可以执行类似的操作,但您需要根据 LI 元素的 id 为每个子菜单添加一行 css:

#test2:hover div.subMenu { display: block; }

“#test2”指的是id 为“test2”的任何元素。

“div.subMenu”引用类名称为“subMenu”的任何元素(在本例中为 div),因为它位于“#test2”之后,因此 div 元素必须是。 “#test2”的后代。

为了使背景图像保持悬停状态,您需要对 css 和 html 进行一些更改,首先在“A”上指定一个类(因为我们不希望这样)。引用 #test2 子级的所有“A”元素,仅指定指定的元素):

<li id="test2"><a href="#" class="top">Pro1</a> ...

然后修改 css,以便在将鼠标悬停在 #test2 上时设置背景(而不是 #test2 a):

#test2:hover a.top {
  background:url("../images/btTest.jpg") no-repeat top;
}

Your HTML structure isn't set up to allow multiple sub-menus with a single css statement. If you look at Mcinerney's HTML:

<div id="menu">
  <ul id="item1">
    <li class="top">menu item</li> 
    <li class="item"><a href="#">menu item 1</a></li> 
    <li class="item"><a href="#">menu item 2</a></li> 
    <li class="item"><a href="#">menu item 3</a></li> 
  </ul> 
</div>

and his css:

#menu ul:hover .item{display:block;}

it translates to "If you hover over a "ul" that is a descendant of an element with id, "menu", then find all elements that are descendants of said "ul" with the class, "item" and set their display to "block".

You can do something similar, but you will need to add a line of css for each sub-menu based on the id of the LI element:

#test2:hover div.subMenu { display: block; }

"#test2" refers to any element with an id of "test2".

"div.subMenu" refers to any element (in this case a div) with a class designation of "subMenu". Because it comes after "#test2", the div element must be a descendant of "#test2".

In order to keep your background-image on hover, you'd need to make some changes to the css and html. First, designate a class on the "A" (because we don't want to reference all "A" elements that are children of #test2, just the designated one):

<li id="test2"><a href="#" class="top">Pro1</a> ...

Then modify your css so that the background is set upon the hover over #test2 (not #test2 a):

#test2:hover a.top {
  background:url("../images/btTest.jpg") no-repeat top;
}
水染的天色ゝ 2024-09-18 09:15:40

div.subMenu 不是“a”标签的后代。尝试:

#menuBar #test2 a:hover + .subMenu{  
    display:block;
}

“+”意味着“直接跟随的兄弟”

您可能还需要

.submenu:hover {
    display:block;
}

或者只是将它们组合起来:

#menuBar > li > a:hover + .subMenu, .submenu:hover {  
    display:block;
}

div.subMenu is not a descendant of 'a' tag. Try:

#menuBar #test2 a:hover + .subMenu{  
    display:block;
}

The '+' means 'direct following sibling of'

You will probably need also

.submenu:hover {
    display:block;
}

Or just combine them:

#menuBar > li > a:hover + .subMenu, .submenu:hover {  
    display:block;
}
殊姿 2024-09-18 09:15:40

这是一个跨浏览器 CSS 下拉菜单,也适用于 IE6。它使用 CSS hacks 和条件 HTML 标记,但它有效!

http://www.cssplay.co.uk/menus/final_drop.html

Here is a cross-browser CSS only drop down menu, that works in IE6 too. It uses CSS hacks AND conditional HTML markup, but it works!

http://www.cssplay.co.uk/menus/final_drop.html

相思故 2024-09-18 09:15:40

Steve Gibson 的网站上有一个纯 CSS 菜单的很好的例子 - http://www.grc.com

There's a good example of CSS-only menus in action at Steve Gibson's site - http://www.grc.com.

離殇 2024-09-18 09:15:40

前往我的 http://collins.class401.com/nav 文件夹,下载所有 css以及 gif 和 js(不用担心,它也可以在没有 javascript 的情况下工作)并查看自述文件并查看演示,

这正是您想要的

head on over to my http://collins.class401.com/nav folder, download all the css and gifs and the js's (don't worry, it works without javascript also) and veiw the readme and check out the demo

this is exactly what your looking for

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文