【问题标题】:Adding Submenu to Existing Nav将子菜单添加到现有导航
【发布时间】:2021-04-06 12:46:19
【问题描述】:

我一直在研究导航栏。我已经按照我喜欢的方式设置了它的样式,但由于某种原因,我无法让子菜单正常工作。

代码如下:

<div id="head">
    <input id="toggle" type="checkbox">

    <label for="toggle" class="icon">
        <div><!-- --></div>
        <div><!-- --></div>
        <div><!-- --></div>
    </label>

    <div id="nav">
        <div>
            <ul>
                 <li class="page_item page-item-8"><a href="http://url.com/home/">Home</a></li>
                 <li class="page_item page-item-10 page_item_has_children"><a href="http://url.com/about/">About</a>
                 <ul class="children">
                     <li class="page_item page-item-22 page_item_has_children"><a href="http://url.com/about/once-more/">Once More</a>
                         <ul class="children">
                             <li class="page_item page-item-23 page_item_has_children"><a href="http://url.com/about/once-more/another-sub-page/">Another Sub Page</a>
                                 <ul class="children">
                                     <li class="page_item page-item-25"><a href="http://url.com/about/once-more/another-sub-page/final-link/">Final Link</a></li>
                                     <li class="page_item page-item-24"><a href="http://url.com/about/once-more/another-sub-page/something-else/">Something Else</a></li>
                                 </ul>
                             </li>
                         </ul>
                     </li>
                     <li class="page_item page-item-21"><a href="http://url.com/about/another-page/">Another Page</a></li>
                     <li class="page_item page-item-20"><a href="http://url.com/about/subpage/">Subpage</a></li>
                 </ul>
                 </li>
                 <li class="page_item page-item-13 current_page_item"><a href="http://url.com/shop/" aria-current="page">Shop</a></li>
                 <li class="page_item page-item-9"><a href="http://url.com/faq/">FAQ</a></li>
                 <li class="page_item page-item-11"><a href="http://url.com/contact/">Contact</a></li>
            </ul>
        </div>
    </div>
</div>

CSS 看起来像:

/* Toggle */
#toggle {
    display: none;
}

/* Nav */
#nav li.current_page_item a {
    background: #d2b48c;
    border-radius: 40px;
    color: #260f03;
}

#nav li a {
    border-bottom: 1px solid transparent;
    border-top: 1px solid transparent;
    color: #fff;
    display: inline-block;
    font-family: 'Lato', sans-serif;
    font-size: 14px;
    letter-spacing: 4px;
    margin: 20px;
    padding: 10px 10px 10px 15px;
    text-decoration: none;
    text-transform: uppercase;
}

/* Media */
@media screen and (max-width: 600px) {
    /* Icon */
    .icon {
        background: #260f03;
        margin: 0 auto;
        padding: 20px 20px 30px;
        position: absolute;
        width: 100%;
        z-index: 500;
    }
    .icon div {
        background: #d2b48c;
        border-radius: 3px;
        height: 2px;
        margin: 10px auto 0;
        position: relative;
        transition: all 0.3s ease-in-out;
        width: 3em;
    }
    /* Toggle */
    #toggle:checked + .icon div:nth-child(1) {
        margin-top: 25px;
        transform: rotate(-45deg);
    }
    #toggle:checked + .icon div:nth-child(2) {
        margin-top: -2px;
        transform: rotate(45deg);
    }
    #toggle:checked + .icon div:nth-child(3) {
        opacity: 0;
        transform: rotate(45deg);
    }
    #toggle:checked + .icon + #nav {
        top: 0;
        transform: scale(1);
    }
    /* Nav */
    #nav {
        background: rgba(38, 15, 3, 0.95);
        bottom: 0;
        height: 100%;
        left: 0;
        overflow: hidden;
        position: fixed;
        right: 0;
        top: -100%;
        transform: scale(0);
        transition: all 0.3s ease-in-out;
        z-index: 200;
    }
    #nav div {
        height: 100%;
        overflow: hidden;
        overflow-y: auto;
        position: relative;
    }
    #nav ul {
        padding-top: 90px;
        text-align: center;
    }
    #nav li a:before {
        background: #fff;
        content: '';
        height: 0;
        left: -0.5em;
        position: absolute;
        transition: all 0.2s ease-in-out;
        width: 0.25em;
    }
    #nav li a:hover:before {
        height: 100%;
    }
}

@media screen and (min-width: 601px) {
    /* HIDE the dropdown */
    #nav li ul { display: none; }

    /* DISPLAY the dropdown */
    #nav li:hover > ul {
        display: block;
        position: absolute;
    }

    /* Nav */
    #nav ul {
        background: #260f03;
        font-size: 0;
        text-align: center;
    }
    #nav li {
        display: inline;
    }
    #nav li a:hover {
        border-bottom: 1px solid #d2b48c;
        border-top: 1px solid #d2b48c;
    }
}

JS Fiddle(带有子菜单):https://jsfiddle.net/2v8zhL3e/1/
JS Fiddle(无子菜单):https://jsfiddle.net/b0mj7gp4/

我找到了一堆关于如何做到这一点的教程,但他们都在 li 上使用浮点数。

我确实尝试过......

我隐藏了下拉菜单:#nav li ul { display: none; }

然后我在悬停时显示它:

#nav li:hover > ul {
    display: block;
    position: absolute;
}

哪种有效...但是链接显示在主导航项的左侧,我想调整一下,使链接堆叠并在相应标题下居中显示。

我觉得我很接近了,谁能指出我正确的方向?

谢谢,
乔什

【问题讨论】:

    标签: css navigation position dropdown submenu


    【解决方案1】:

    您可以将display: flex 添加到li:hover ul,然后创建flex-direction: column 并创建非悬停事件position: absoluteleft: 0。这会造成 ul 子元素浮动到最左侧的问题。因此,要解决此问题,您需要将其父级设置为具有相对位置。以下应该可以解决问题。

    #nav li {
      position: relative;
    }
    
    #nav li ul {  
      left: 0;
      position: absolute;
    }
    
    #nav li:hover > ul {
      display: flex;
      flex-direction: column;
    }
    

    【讨论】:

    • 所以,我使用了这个,但我仍然需要进行一些调整才能让多个关卡正常工作......这对于单个关卡来说非常有用,这让我走上了正确的轨道:-)
    【解决方案2】:

    你没有 position: relative in li。 而且,默认情况下最好使用 position: absolute 不悬停。

    #nav li {
      position: relative;
    }
    #nav li > ul {
      position: absolute;
      display: none;
    }
    #nav li:hover > ul {
      display: block;
    }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2012-02-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2020-09-06
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多