简体   繁体   English

将右侧的垂直菜单展开为div

[英]Expand vertical menu to the right into a div

First time user here. 第一次使用这里的用户。

I have created a vertical menu and i want it to expand (on hover) to the right into a DIV to show an image. 我已经创建了一个垂直菜单,我希望它在悬停时向右扩展为DIV以显示图像。 The problem is that when the menu expands, the top of the DIV is aligned with the top of the menu element taht is on hover and i want the top of the DIV to align to the top of the first menu element no matter wich element is on hover. 问题是,当菜单展开时,DIV的顶部与菜单元素的顶部对齐taht悬停了,并且我希望DIV的顶部与第一个菜单元素的顶部对齐,而无论哪个元素悬停。

Here's the code i have so far... 这是我到目前为止的代码...

<nav>
    <ul>
        <li id="menu-1"><a href="menu-1.html">menu-1</a>
            <div class="nav-expand">
                <a href="menu-1"><img src="img/1.png"></a>
            </div>
        </li>
        <li id="menu-2"><a href="menu-2.html">menu-2</a>
            <div class="nav-expand">
                <a href="menu-2"><img src="img/2.png"></a>
            </div>
        </li>
        <li id="menu-3"><a href="menu-3.html">menu-3</a>
            <div class="nav-expand">
                <a href="menu-3"><img src="img/3.png"></a>
            </div>
        </li>
    </ul>
</nav>
</body>

and the CSS 和CSS

nav {
float:left;
width:192px;
}
nav li {
position:relative;
display:block;
width:176px;
height:29px;
margin-bottom:1px;
background:#EEE;
}
nav li:last-child {
height:30px;
}
nav li:hover {
background:#09B2B3;
width:192px;
-webkit-transition:all .3s ease;
   -moz-transition:all .3s ease;
     -o-transition:all .3s ease;
        transition:all .2s ease;
}
.nav-expand {
display:none;
background:#FFF;
border:2px solid #09B2B3;
position:absolute;
left:192px;
top:0;
z-index:1001;
width:556px;
height:356px;
overflow:hidden;
}
#navigation .nav-expand a {
margin-left:0;
display:block;
width:560px;
height:360px;
}
.nav-expand {
width:556px;
height:356px;
}
nav li:hover > .nav-expand {
display:block;
}

I realized that i could use a different class for every element of the menu and use an absolute position, but was wondering if there's a better way to do it. 我意识到我可以为菜单的每个元素使用不同的类并使用绝对位置,但是我想知道是否有更好的方法可以做到这一点。

I think you can remove the 'position: relative' from your .nav li and add it to .nav ul. 我认为您可以从.nav li中删除“ position:relative”并将其添加到.nav ul中。

.nav > ul{
position: relative;
}

There are better ways of doing this then the way you have approached. 有比您所采用的方法更好的方法。

Actually, menu-1, 2, 3 are all positioned relative and the nav-expand is positioned absolute. 实际上,菜单1、2、3都是相对放置的,导航展开是绝对放置的。

Here's the working code 这是工作代码

CSS: CSS:

nav {
float:left;
width:192px;
}
nav li {
display:block;
width:176px;
height:29px;
margin-bottom:1px;
background:#EEE;
}
nav li:last-child {
height:30px;
}
nav li:hover {
background:#09B2B3;
width:192px;
-webkit-transition:all .3s ease;
   -moz-transition:all .3s ease;
     -o-transition:all .3s ease;
        transition:all .2s ease;
}
.nav-expand {
background:#FFF;
border:2px solid #09B2B3;
position:absolute;
left:240px;
top:24px;
z-index:1001;
width:556px;
height:356px;
overflow:hidden;
display:none;    
}
#navigation .nav-expand a {
margin-left:0;
display:block;
width:560px;
height:360px;
}
.nav-expand {
width:556px;
height:356px;
}
nav li:hover > .nav-expand {
display:block;
}

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

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