将下拉菜单背景高度设置为绝对位置子菜单高度

时间:2015-12-15 05:07:19

标签: jquery html css3

我在页面上有一个多级导航。导航有三到四个级别。我想在下拉菜单中包含所有子菜单的全宽背景。目前,我已经完成了一个级别,但是当点击第一级菜单时,出现第二级菜单,然后背景没有动态地获取高度,并且在第三级菜单的情况下也是如此。由于子菜单处于绝对位置,因此背景的高度不会动态增加。有人可以帮忙吗?我有一个参考网站。 http://www.qsc.com/ ...但是从这里我无法获得代码。我正在准备自己的代码。



 $(document).ready(function(){
	/* 
        // Show hide popover
        $(".dropdown").click(function(){
            $(this).find(".dropdown-menu").slideToggle("fast");
			
        });*/
		$('.dropdown-menu').hide();	
		
		$('.nav ul li > a').click(function() {
	$(this).parent().siblings().find('ul').slideUp(300);
	$(this).next('ul').stop(true, false, true).slideToggle(300);
	return false;
	
		});
	
	
});
	
	

/* not very relevant styling */
h1         { font-size: 20px; padding-top: 20px; }
.container { position: relative; margin: 20px auto 0 auto; width: 75%; }
.header    { background: #eee; }
.nav       { background: #ccc; }


/* relevant styling */

body { overflow-x: hidden; } /* trick from css-tricks comments */

/* FIRST LEVEL */

.nav > ul > li { 
    display: inline-block; 
    position: relative; 
    padding: 3px 10px 3px 0;
    z-index: 100;
	list-style:none;
}

.nav > ul > li > ul{
	width:100px}

.nav > ul > li > a{
	color:#939598;
	text-decoration:none;
}

/* Level 1 Drop Down */
.dropdown-menu > li {
	display: inline-block;
	vertical-align: top;
	 /* solve the 4 pixels spacing between list-items */
}
.dropdown-menu > li:first-child {
	margin-left: 0;
}

.dropdown-menu li {
	position: relative;
	list-style:none;
}

/* SECOND LEVEL ( background -color) of full width bg*/
.nav > ul > li > ul {
    position: absolute;
    left: 0;
    top: 100%;
    padding: 0 1000em; /* trick from css-tricks comments */
    margin: 0 -1000em; /* trick from css-tricks comments */
    z-index: 101;   
    background:#f2f2f2;
}





/*second level menu*/
.nav li ul li ul li:before {
    content: "";
    display: block;
    width: 1px;
    height: 100%;
    background-color: #7f7f7f;
    position: absolute;
    left: 0;
}


.nav > ul > li > ul > li {
    padding: 3px 0;
	color:#939598;
	text-decoration:none;


}

.nav > ul > li > ul > li > a{color:#939598;
	text-decoration:none;}
	
	.dropdown-menu li a{
	color:#939598;
	text-decoration:none;		
}

	.dropdown-menu li ul li{
		color:#939598;
	text-decoration:none;
		padding-left:5px;}
	
	
	
.dropdown-menu  > li > ul {
	text-align: left;
	display: none;
	color:#939598;
	text-decoration:none;
	position: absolute;
	top: 5px;
	left: 100%;	
	z-index: 999999; /* if you have YouTube iframes, is good to have a bigger z-index so the video can appear above the video */
}


.nav .drop { 
 
    width:100%!important; /* or: width:1200px!important;*/
    box-sizing:border-box;
}

/*Third level menu*/

.dropdown-menu  > li > ul > li > ul {
	text-align: left;
display: none;	
	position: absolute;
	left: 100%;
	top: 0;
	z-index: 9999999;
	color:#939598;
	text-decoration:none;
}

<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.9.1/jquery.min.js"></script>
<div class="header">
    <!-- NAVIGATION -->    
    <div class="nav">
        <ul class="container">
            <li class="dropdown">
                <a class="drop" href="#">Products</a>
                <ul class="dropdown-menu">
                    <li><a href="#">Live Sound</a>
                    
                    <ul>
                    <li>Power AMplifiers</li>
                    <li>Loud Speakers</li>
                     <li>Power AMplifiers</li>
                    <li>Loud Speakers</li>
                     <li>Power AMplifiers</li>
                    <li>Loud Speakers</li>
                     <li>Power AMplifiers</li>
                    <li>Loud Speakers</li>
                    <li><a href="#">Touch Mix Mixers</a>
                    <ul class="dropdown-menu">
                    <li>GX Series</li>
                    <li>GXD Series</li>
                    <li>RMX Series</li>
                    <li>PLX2 Series</li>
                    <li>Powerlight three Series</li>
                    </ul>
                    
                    </li>
                    
                    </ul>
                    
                    </li>
                    <li><a href="#">System</a></li>
                    <li><a href="#">Cinema</a></li>
                   
                </ul>
            </li>
            <li class="dropdown">
                <a class="drop" href="#">Locations</a>
                <ul class="dropdown-menu">
                    <li><a href="#">Location A</a></li>
                    <li><a href="#">Location B</a></li>
                    <li><a href="#">Location C</a></li>
                </ul>
            </li>
            
            
            <li class="dropdown">
                <a class="drop" href="#">Locations</a>
                <ul class="dropdown-menu">
                    <li><a href="#">Location A</a></li>
                    <li><a href="#">Location B</a></li>
                    <li><a href="#">Location C</a></li>
                </ul>
            </li>
        
        </ul>
    </div>
    
</div>

<div class="content container">
    <p>All sorts of content</p>
    <p>All sorts of content</p>
    <p>All sorts of content</p>
    <p>All sorts of content</p>
    <p>All sorts of content</p>
    <p>All sorts of content</p>
</div>
&#13;
&#13;
&#13;

1 个答案:

答案 0 :(得分:0)

您应该将background: #f2f2f2;提交给ul

这里我将菜单类添加到ul并将css添加到菜单类

.menu{
 background: #f2f2f2;
}

检查Working Fiddle

希望它有所帮助。

修改

像给定链接一样做。打开时,您应该将子ul设为position:relative。如下:

JQuery的:

$(this).next('.sub').toggleClass('menu');

CSS:

.menu{
 background: #f2f2f2;
 position: relative !important;
}

HTML:

<ul class="sub" >

您可以通过这种方式切换类menu的类sub,这样您就可以获得预期的结果。

Updated Fiddle

您可以根据自己的要求进行更改。