简体   繁体   English

当<A>其他div内的标签悬停</a>时,如何显示<A>div?</a>

[英]How to make a div display when <A> tag inside other div is hovered?

I'm trying to built a vertical menu inside a submenu container. 我试图在子菜单容器中建立一个垂直菜单。

I have a horizontal menu with full width submenu container (this I have done, for the submenu content I need help) - in which I would need to create a vertical drop-down menu, but the vertical-submenus need to appear in a div (or whatever will make it look like in the example) with the same height as the vertical menu, full width and appear in the same position no matter the parrent button's position. 我有一个具有全宽度子菜单容器的水平菜单(我已经完成了此操作,对于子菜单内容我需要帮助)-在其中我需要创建一个垂直下拉菜单,但是垂直子菜单需要出现在div中(或使示例中显示的样子)具有与垂直菜单相同的高度,全宽并且无论父按钮的位置如何都显示在相同位置。 Hope you understood, if no just see the jsFiddle . 希望您能理解,如果没有的话,请看jsFiddle

The problem is that the vertical menu container need to have a width of 20% and a specific color background and the container for its submenus should have the rest of 80% and a different color. 问题在于,垂直菜单容器需要具有20%的宽度和特定的颜色背景,而其子菜单容器应具有80%的其余部分和不同的颜色。 On hover over one button (Button1) should appear that 80% width div (div.BC) with its content (let's say a video). 将鼠标悬停在一个按钮(Button1)上时,应显示其内容的80%宽度div(div.BC)(例如,视频)。

<div class="AB">
<li><a class="1">Button 1</a></li>
<li><a class="2">Button 2</a></li>
<li><a class="3">Button 3</a></li>
<li><a class="4">Button 4</a></li>
<li><a class="5">Button 5</a></li>
</div>
<div class="B">
   <div class="C"></div>
   <div class="D"></div>
</div>

Any solution (CSS or jQuery - though I would prefer a CSS one) will be highly appreciated. 任何解决方案(CSS或jQuery-尽管我更喜欢CSS)都将受到高度赞赏。

And again an outline of my problem: jsFIDDLE 再次概述我的问题: jsFIDDLE

First what you want to do is nest your divs you want to display on hover inside of the list item elements so they become part of the hover field and are target-able by the css traversing. 首先,您要做的是将要显示的div嵌套在列表项元素内,以便它们成为悬浮字段的一部分,并且可以通过CSS遍历成为目标。 You set the initial display on your nested divs to display: none. 您将嵌套div上的初始显示设置为显示:无。 On hover, you just set the display on the nested div to inline-block (or any visible display property). 悬停时,只需将嵌套div上的显示设置为inline-block(或任何可见的display属性)。 I mocked up your code quick to show you. 我快速模拟了您的代码以向您展示。

HTML 的HTML

<div class="AB">
<li><a class="1">Button 1</a>
    <div class="sub">Some stuff</div>
</li>
<li><a class="2">Button 2</a>
    <div class="sub">Some other stuff</div>
</li>
<li><a class="3">Button 3</a></li>
<li><a class="4">Button 4</a></li>
<li><a class="5">Button 5</a></li>
</div>

CSS 的CSS

.AB {
float:left;
width: 100px;
height:100px;
background: #d9d9d9;
}
.AB li {
width: 100px;
height: 20px;
line-height: 20px;
font-size: 15px;
text-align: center;
list-style-type: none;
}
li a{
width: 100px !important;
cursor: pointer;
}
.sub {
display:none;
background: #333;
margin-left: 100px;
width: 400px;
}
.AB li:hover{
background: #555;
}
.AB li:hover > .sub {
display:block;
}
.AB .B {
display: inline-block;
}

.example{
height: 100px;
width: 100%;
background: #00ff00;
}

.AB .example {display: inline-block;}

JsFiddle JsFiddle

I this what you try to achieve? 我这你想达到什么? You can run the snippet to see the result. 您可以运行代码段以查看结果。

EDIT: I have added a class active to the first <li> element (Button 1), this way it is by default active. 编辑:我已经向第一个<li>元素(按钮1)添加了一个活动类,这种方式默认情况下是活动的。 You can then add the class active to other buttons in other pages. 然后,您可以将活动类别添加到其他页面中的其他按钮。

 .container { position: relative; width: 100%; height: 100px; margin: 0; padding: 0; background: #efefef; } ul { position: relative; top: 0; left: 0; width: 100%; height:100px; margin: 0; padding: 0; list-style-type: none; } ul li { width: 100px; height: 20px; line-height: 20px; font-size: 15px; text-align: center; background: #d9d9d9; } ul li a{ width: 100%; cursor: pointer; } ul li:hover{ background: #555; } ul .options { display: none; position: absolute; top: 0; bottom: 0; left: 100px; right: 0; text-align: left; background: lightblue; z-index: 0; } ul li:hover .options{ display: block; z-index: 1; } ul li:nth-child(even) .options { background: lightblue; } ul li:nth-child(odd) .options { background: lightgreen; } ul .active { background: #555; } ul .active .options { display: block; } 
 <div class="container"> <ul class="AB"> <li class="active"> <a class="1">Button 1</a> <div class="options">Button 1 sub-menu</div> </li> <li> <a class="2">Button 2</a> <div class="options">Button 2 sub-menu</div> </li> <li> <a class="3">Button 3</a> <div class="options">Button 3 sub-menu</div> </li> <li> <a class="4">Button 4</a> <div class="options">Button 4 sub-menu</div> </li> <li> <a class="5">Button 5</a> <div class="options">Button 5 sub-menu</div> </li> </ul> </div> 

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM