【问题标题】:Overlapping of dropdown menu with another item下拉菜单与另一个项目重叠
【发布时间】:2012-01-31 10:32:30
【问题描述】:

编辑

我的 jsfiddle 条目是 here : http://jsfiddle.net/ehNrE/3/

下面的所有代码(只有那些需要的)都在那里,我根据@Jasper 的要求更新了......虽然有些部分可能会丢失,因为我不得不削减巨大的代码

PS:点击 jsfiddle 中的下拉菜单时,您在本地系统中看不到红色向下箭头作为其图像,但您只需点击箭头应该指向的位置即可来看看效果吧。

原帖

上面的图片解释了我的问题......我的下拉菜单有什么问题?为什么它与我下面的entry div 类重叠.... 任何人都可以提出补救措施吗? ...我正在使用codes from here 来开发这个...我也不知道额外空间来自哪里

我的标记 HTML(带有 jquery 的下拉菜单):

<div id="menu">
    <ul class="topnav">



        <li><a href="#">Live-Radio</a></li>
        <li><a href="#">Blog</a></li>
        <li><a href="#">Profile</a></li>
        <li><a href="#">Home</a></li>
        <li>
            <a href="#">Songs</a>
            <ul class="subnav">
                <li><a href="#">Sub Nav Link</a></li>
                <li><a href="#">Sub Nav Link</a></li>
            </ul>
        </li>

    </ul>

    <script type="text/javascript">
    $(document).ready(function(){

        $("ul.subnav").parent().append("<span></span>"); //Only shows drop down trigger when js is enabled (Adds empty span tag after ul.subnav*)

        $("ul.topnav li span").click(function() { //When trigger is clicked...

            //Following events are applied to the subnav itself (moving subnav up and down)
            $(this).parent().find("ul.subnav").slideDown('fast').show(); //Drop down the subnav on click

            $(this).parent().hover(function() {
            }, function(){
                $(this).parent().find("ul.subnav").slideUp('slow'); //When the mouse hovers out of the subnav, move it back up
            });

            //Following events are applied to the trigger (Hover events for the trigger)
            }).hover(function() {
                $(this).addClass("subhover"); //On hover over, add class "subhover"
            }, function(){  //On Hover Out
                $(this).removeClass("subhover"); //On hover out, remove class "subhover"
        });

    });

    </script>
</div>

我的 CSS:

#menu{
    float:left;
    height:71px;
    padding-top:35px;
    text-align: right;
    width: 400px;
}

ul.topnav {    
    list-style: none;
    margin: 0;
    font-size: 1.2em;
    z-index:50;
}
ul.topnav li {

    height:100px;
    float: right;
    margin: 0;
    padding: 0 15px 15px 0;
    position: relative; /*--Declare X and Y axis base for sub navigation--*/
}
ul.topnav li a{
    padding: 10px 5px;
    color: #222;
    display: block;
        text-decoration: none;
        float: left;
}
ul.topnav li a:hover{
    font-weight:bold;
}
ul.topnav li span { /*--Drop down trigger styles--*/
    width: 17px;
    height: 35px;
    float: left;
    background: url(images/down.png) no-repeat center top;
}
ul.topnav li span.subhover {cursor: pointer;} /*--Hover effect for trigger--*/
ul.topnav li ul.subnav {
    list-style: none;
    position: absolute; /*--Important - Keeps subnav from affecting main navigation flow--*/
    left: 0; top: 35px;
    background: #333;
    margin: 0; padding: 0;
    display: none;
    float: left;
    width: 170px;
    border: 1px solid #111;
}
ul.topnav li ul.subnav li{
    margin: 0; padding: 0;
    border-top: 1px solid #252525; /*--Create bevel effect--*/
    border-bottom: 1px solid #444; /*--Create bevel effect--*/
    clear: both;
    width: 170px;
}
html ul.topnav li ul.subnav li a {
    float: left;
    width: 145px;
    background: #333 url(dropdown_linkbg.gif) no-repeat 10px center;
    padding-left: 20px;
}
html ul.topnav li ul.subnav li a:hover { /*--Hover effect for subnav links--*/
    background: #222 url(dropdown_linkbg.gif) no-repeat 10px center;
}



.entry{
    position:relative;
    margin:0 0 20px 0;
    border:2px solid #fff;
    background:#eee url(images/entrybg.png) repeat-x;
    color:#333;
    padding:10px 10px 0 10px;
}

下面的&lt;div class="entry"&gt; ... &lt;/div&gt; 会创建您看到的标题为What's happening at Bedupako.Com 的框

【问题讨论】:

  • PS:我尝试将 100 的 z-index 设置为 #menu ID,但它不起作用:(
  • 你能设置你的代码的jsfiddle吗?这种格式有很多内容可以接受:jsfiddle.net
  • @Jasper 我已经更新并提供了 JSFiddle 条目的链接...请看一下。
  • 你似乎从JS Fiddle 中省略了 JavaScript/jQuery。
  • @DavidThomas ...我现在更新了代码....这里也更新了帖子...JS嵌入在HTML中,现在我只是把需要的一个分开。

标签: jquery html css drop-down-menu


【解决方案1】:

首先,您的菜单与您的内容区域重叠,因为它不知道谁先进入信息流,因为您的子菜单是绝对定位的。要解决此问题,只需向您的 #menu 容器声明一个位置,然后添加一个类似于 9999z-index 以将其置于其他所有位置之上。

#menu {
    position:relative;
    z-index:9999;
}

至于第二个问题,子菜单项继承了主菜单项的高度,即 100 像素,只需在子菜单 li 项中声明一个 height:auto 即可解决此问题。

ul.topnav li ul.subnav li {
    height: auto;
}

编辑::

小提琴

http://jsfiddle.net/andresilich/ehNrE/6/

【讨论】:

  • 谢谢 :D 我在前端有点弱,没有注意到这些...再次感谢!...在您第一次发布时接受了您的!
  • 你好,还有1个疑问,不管我给什么z-index,如果页面中嵌入了FLASH,总是排在最前面,如何防止?
  • 您必须为您的&lt;object&gt;&lt;embed&gt; 标签定义一个wmode="transparent" 属性,以将其置于您的html 内容后面。
【解决方案2】:

您的菜单出现在您的内容后面的原因是因为ul.topnav 中的z-index 不适用于static 定位。要解决此问题,您只需应用 relative 定位:

ul.topnav {
    position: relative;    /* add this */   
    list-style: none;
    margin: 0;
    font-size: 1.2em;
    z-index:50;
}

子导航中有额外间距的原因是因为.subnav 中的li 元素从ul.topnav li 中的CSS 样式继承height: 100px。要解决这个问题,您应该从 ul.topnav li 中删除 height: 100px 并将其添加到 #menu

#menu{
    height:100px;    /* add this */
    float:left;
    height:71px;
    padding-top:35px;
    text-align: right;
    width: 400px;
}

ul.topnav li {
    /* height:100px;   remove this */
    float: right;
    margin: 0;
    padding: 0 15px 15px 0;
    position: relative; /*--Declare X and Y axis base for sub navigation--*/
}

JSfiddle:http://jsfiddle.net/ehNrE/5/

【讨论】:

  • 谢谢 :D 我的前端有点弱,没注意到这些……再次感谢!...
  • 没问题,很高兴我们能帮上忙 :)
猜你喜欢
  • 2011-11-11
  • 2016-04-23
  • 2017-05-08
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2020-11-13
相关资源
最近更新 更多