我试图让子菜单项在菜单项列表中显示得更高或更低。目前我在菜单右侧有子菜单项,但始终位于固定位置。菜单左边是175px。它可以根据悬停的菜单项在列表中上下移动吗?
HTML
<nav id="main_menu">
<div class="nav-collapse collapse">
<ul class="nav nav-pills">
<li class="dropdown active"><a href="javascript:{}">Home</a>
</li>
<li class="dropdown"><a href="javascript:{}">Services</a>
<ul class="dropdown-menu">
<li><a href="portraits.html">Portraits</a></li>
<li><a href="javascript:{}">Social - Event</a>
<ul class="dropdown-menu sub-menu">
<li><a href="wedding-photographers.html">Weddings</a></li>
<li><a href="bar-bat-mitzvah.html">Bar/Bat Mitzvahs</a></li>
<li><a href="404.html">Birthdays</a></li>
<li><a href="404.html">Balls</a></li>
</ul>
</li>
<li><a href="javascript:{}">Commercial</a>
<ul class="dropdown-menu sub-menu">
<li><a href="architecture-photographers.html">Architecture</a></li>
<li><a href="jewelry-photographers.html">On Figure</a></li>
<li><a href="product-photographers.html">Product</a></li>
<li><a href="404.html">Food</a></li>
</ul>
</li>
</ul>
</li>
<li class="dropdown"><a href="javascript:{}">INFO/EXTRA</a>
<ul class="dropdown-menu">
<li><a href="javascript:{}">Client Galleries</a>
<ul class="dropdown-menu sub-menu">
<li><a href="http://photo.kochfoto.com/BarBatMitzvah">Bar & Bat mitzvah</a></li>
<li><a href="http://photo.kochfoto.com/Commercial">Commercial</a></li>
<li><a href="http://photo.kochfoto.com/portraits">Portrait</a></li>
<li><a href="http://photo.kochfoto.com/weddings">Wedding</a></li>
</ul>
</li>
<li><a href="albums.html">Albums</a></li>
<li><a href="testimonials.html">Testimonials</a></li>
<li><a href="camera-bag.html">Camera Bag</a></li>
<li><a href="about.html">About</a></li>
</ul>
</li>
<li><a href="http://www.kochfoto.com/blog" target="_blank">BLOG</a></li>
<li><a href="contact.html">Contact</a></li>
</ul>
</div>
</nav>
CSS
body {
margin:0; padding:0;
font:13px/22px 'Lato', sans-serif;
color:#555;
background: #f0f0f0;
-webkit-backface-visibility:hidden;
overflow-x:hidden;
text-rendering:optimizeLegibility;
}
#main_menu {float:right; margin:22px 0 0 0;}
.nav{ padding:0; margin:0; }
.nav-pills > li{
list-style: none;
float: left;
margin: 0;
position: relative;
padding: 0 0;
display: inline-block;
}
.nav-pills > li > a {
font-weight: 400;
color: #bbb;
letter-spacing: 2px;
font-size: 13px;
display: block;
padding:24px 22px 22px 25px;
text-align: center!important;
text-transform: uppercase;
}
.nav-pills > li.active{
background: #C0392B;
color:#fff;
webkit-border-radius: 4px 4px 0px 0px;
-moz-border-radius: 4px 4px 0px 0px;
border-radius: 4px 4px 0px 0px;
}
.nav-pills > .active > a, .nav-pills > .active > a:hover, .nav-pills > .active > a:focus { background: transparent; }
.nav-pills > li > a:hover { background: transparent; color: #fff; }
.dropdown-menu {
background: #fff;
display: none;
left:-25px;
padding:0;
position:absolute;
top:80px;
width:170px;
z-index:300;
-webkit-border-radius: 4px;
-moz-border-radius: 4px;
border-radius: 4px;
background:#333;
box-shadow:none !important;
border:none;
}
.dropdown-menu > li > a {
border: 0;
color: #bbb;
display: block;
font-size: 13px;
padding: 8px 20px !important;
text-align: left;
text-transform: none;
}
.dropdown-menu > li > a:hover, .dropdown-menu > li > a:focus, .dropdown-submenu:hover > a, .dropdown-submenu:focus > a {text-decoration:none; color: #fff; background:transparent!important; }
.dropdown-menu > li:first-child { margin-top:4px;}
.dropdown-menu > li:last-child { margin-bottom:6px;}
.nav-tabs .open .dropdown-toggle, .nav-pills .open .dropdown-toggle, .nav > li.dropdown.open.active > a:hover, .nav > li.dropdown.open.active > a:focus{ background- color:transparent; border:0px; }
.dropdown-menu .sub-menu { visibility: hidden; left:175px; }
.dropdown-menu li:hover .sub-menu { visibility: visible; }
.dropdown-menu .sub-menu:hover a {
-o-transition-property: none !important;
-moz-transition-property: none !important;
-ms-transition-property: none !important;
-webkit-transition-property: none !important;
transition-property: none !important;
}
答案 0 :(得分:0)
这有点像一个肮脏的黑客,但它完成了工作。
jQuery代码:
jQuery('ul.nav li.dropdown').hover(function (){
jQuery(this).find('.dropdown-menu').stop(true, true).delay(200).fadeIn();
jQuery('a').hover(function() {
var height = jQuery(this).offset();
jQuery(this).parent().find('.sub-menu').css('top', height.top - +106);
});
}, function (){
jQuery(this).find('.dropdown-menu').stop(true, true).delay(200).fadeOut();
});
jQuery('.btn-navbar').on('click',function(){
jQuery('nav#main_menu > .nav-collapse > ul.nav-pills').slideDown();
});
jsFiddle:http://jsfiddle.net/zfmuf/9/
它获取div中高度的偏移量,然后将其添加到子菜单类,同时删除父偏移量。它是一个非常脏的小黑客,但它的工作原理。