【问题标题】:CSS Vertical Menu with submenu callouts overlapping子菜单标注重叠的 CSS 垂直菜单
【发布时间】:2017-08-03 13:47:32
【问题描述】:

已经有一段时间了 - 但我发现自己对 Web 开发有了新的兴趣,我希望你能帮助我克服以下问题:

我正在开发个人网站。内容等都是初步的。我的主要关注点是左侧的垂直菜单。虽然我对目前的外观很满意,但仍有一些问题:

1) 我必须将过渡设置为缓出到 >0s,以便在主菜单悬停和悬停在子菜单上之间有足够的时间。因此,如果我将鼠标悬停在主菜单项之间,它们将由于逐步淘汰而短暂重叠。有没有办法解决这个问题? (焦点选择器可能是一个选项,但这需要单击菜单项,我更喜欢悬停)

2) 子菜单与主菜单的 li 顶部对齐。因此,最后一项的子(测试)超出了页面的边界,在页面下方给出了空白。如果子菜单压在整体的边缘,有没有办法让子菜单上移? 我可能可以给它一个不同的标签并进行不同的设计,我希望有一个更通用的解决方案。

3) 小问题:从主菜单悬停切换到子菜单悬停时,主图标“闪烁”非常短暂。这是正常的行为吗?当然不漂亮。

4) 最后但并非最不重要的一点:由于这是我第一次做网站,我将非常感谢您对代码效率低下的一般反馈,这样我将来可以更精简。

(请参阅下面更新的小提琴 - 删除完整代码以节省空间。)

感谢您的帮助!


编辑 感谢您的回答 - 问题 #1 已使用您概述的方法解决!

但是,最后一个子菜单还是让我有点头疼。

如果我使用“last-child”选择器,以及下面添加的代码,它不会改变任何东西。然而,如果我给最后一个“气泡”一个不同的标签,使用完全相同的代码,气泡将与最后一个主菜单项的底部对齐,但包含的 UL 仍然是挑衅的。

从原始 CSS 中可以看出,最后一个“相对”是主菜单 IL,所以子菜单和包含的 UL 不应该都对齐到它们各自父/祖父的底部吗?

  • 为最后一个孩子“bubble_last”使用专用的不同标签
  • 使用最后一个子选择器

以及随附的新代码(完整代码的更新小提琴:继承人Fiddle ...使用大屏幕查看结果以查看对齐问题):

.navigation ul li:last-child {
    border: solid orange;
}

.navigation .bubble_last{
    position: absolute;
    margin-top: 0%;
    padding: 0px;
    left: 60px;
    bottom: 0px;
    width: 400px;
    height: 200px;
    border: solid red;
}

.navigation .bubble_last ul{
    position: absolute;
    left: 0;
    bottom: : 0px;
    margin-top: 0px;
    padding: 0px;
    width: 20vw;
    height: 100px;
    font-size: 15px;
    font-size: 1.2vw;
    font-weight: 400;
    border: solid blue;
    margin-left: 28%;
    display: block;

}

.navigation .bubble_last ul li {
    list-style: none;
    background-color: none;
    border-radius: 3px;
    height: 2vw;
    line-height: 1.2vw;
    width: 20vw;
    position: relative; 
    padding-top: 0px;
    margin: 0;
    padding: 0;

}

【问题讨论】:

    标签: html css drop-down-menu menu


    【解决方案1】:

    无需在.bubble 上添加转换延迟,您只需使.bubble 类足够宽(并在主li 触发器后面使用left: 60px 开始),以便悬停将是连续的:

    .navigation .bubble {
      position: absolute;
      margin-top: 0%;
      padding: 0px;
      left: 60px;
      top: 0px;
      width: 200px;
      height: 100%;
      background-color: none;
    }
    

    然后只需在子菜单中添加margin-left

    .navigation ul li ul {
      margin-left: 20px;
    }
    

    要解决您的第二个问题,只需添加以下 CSS 以使最后一个子菜单相对于导航项的底部定位:

    ul li ul.subm:last-child {
      top: auto;
      bottom:0px;
    }
    

    更新的演示:JSFiddle

    【讨论】:

    • 看起来足够简单和优雅!您对第二个问题有任何线索吗?关于页面底部过度扩展的最后一个子菜单的问题?有没有解决方案,或者我有单独的样式?
    • @chimp 我已经用第二个问题的解决方案更新了我上面的答案。 Fiddle
    • 太棒了!非常感谢。
    • 我尝试使用您的方法,但是虽然气泡 div 与最后一个主菜单项的底部很好地对齐,但包含的 UL 仍然顽固。我已经更新了原始问题以反映包括屏幕截图在内的问题。如果你能回顾一下并让我知道我一直在撞哪面砖墙,那就太好了。
    • Holy Smokes - 今天早上我几乎检查了所有的行,修改和删除,看看是否有什么会改变......这一切都是因为这个额外的小冒号。嗯,谢谢你解决了这个问题!在通过反复试验学习了 last-child 选择器的应用之后,我的首页现在按照我在脑海中描绘的那样运行。谢谢!
    【解决方案2】:

    这个怎么样?调整您的导航以使用 nav 元素,将其设置为使用弹性框,调整它以适合您的页面高度,然后调整过渡效果,使其正确过渡进出。

    我希望这会有所帮助。

    @import url("http://fonts.googleapis.com/css?family=Roboto");
    * {
      border: none;
      margin: 0;
      padding: 0;
    }
    
    html,
    body {
      margin: 0;
      padding: 0;
      height: 100%;
      width: 100%;
      font-family: Roboto Condensed;
      box-sizing: border-box;
    }
    
    .landing {
      display: table;
      margin: auto;
      width: 100%;
      height: 100%;
      background: url("https://imageshack.com/i/poXkyeIYj") 50% 50% no-repeat;
      background-size: cover;
      top: 0;
      position: relative;
      z-index: 1;
    }
    
    .landing .welcome {
      margin: 10px;
      width: 89%;
      color: #FFF;
      text-align: center;
      display: table-cell;
      vertical-align: middle;
      position: absolute;
      left: 10%;
      top: 30%;
    }
    
    #mug {
      background: url("https://imageshack.com/i/pmJaAuFkj") 50% 50% no-repeat;
      background-size: cover;
      border: solid 3px #FFF;
      /*max-width: 20%;
        max-height: 20%; */
      width: 150px;
      height: 150px;
      margin: auto;
      border-radius: 100%;
    }
    
    nav {
      width: 50px;
      height: 100%;
      flex: 1;
      display: flex;
      justify-content: center;
      flex-direction: column;
    }
    
    nav img {
      max-width: 50px;
      max-height: 50px;
    }
    
    .nav ul {
      *zoom: 1;
      list-style: none;
      margin: 0;
      padding: 0;
      -ms-flex: 0 100px;
      flex: 1;
      display: flex;
      flex-direction: column;
      justify-content: center;
      justify-content: space-between;
      margin-top: 0px;
      padding: 0px;
      width: 11vw;
      height: auto;
      font-size: 15px;
      font-size: 1.2vw;
      font-weight: 400;
      border-color: none;
    }
    
    nav ul li {
      background-color: rgba(222, 225, 229, 0.8);
      border-radius: 3px;
      width: 100%;
      font-size: 15px;
      font-size: 1.2vw;
      font-weight: 900;
    }
    
    .nav ul:before,
    .nav ul:after {
      content: "";
      display: block;
    }
    
    .nav ul:after {
      clear: both;
    }
    
    .nav ul>li {
      position: relative;
    }
    
    .nav a {
      display: block;
      padding: 10px 20px;
      line-height: 1.2em;
      color: #fff;
      border-left: 1px solid #595959;
      text-decoration: none;
      color: #FFF;
    }
    
    .nav a:hover {
      text-decoration: none;
      background-color: rgba(242, 93, 38, 0.8);
    }
    
    .nav li ul {
      background: #273754;
    }
    
    .nav li ul li {
      width: 200px;
    }
    
    .nav li ul a {
      border: none;
    }
    
    .nav li ul a:hover {
      background: rgba(0, 0, 0, 0.2);
    }
    
    .nav li ul {
      position: absolute;
      left: 11vw;
      top: 0;
      z-index: 1;
      visibility: hidden;
      opacity: 0;
      filter: alpha(opacity=0);
      -webkit-transition: 200ms ease;
      -moz-transition: 200ms ease;
      -o-transition: 200ms ease;
      transition: 200ms ease;
    }
    
    .nav ul>li:hover ul {
      visibility: visible;
      opacity: 1;
      filter: alpha(opacity=100);
    }
    
    span.caption {
      display: none;
    }
    <body>
      <div class="landing">
        <div class="welcome">
          <div id="mug">
          </div>
    
          <h1>Welcome to my personal Website!</h1>
          <h2>On this site you will find a information about the person, the profession and other stuff about me. Enjoy!</h2>
    
        </div>
    
    
    
    
    
        <!-- cleaned up the navigation, nested it within the rest of the page, and adjusted the css -->
        <nav class="nav">
          <ul>
            <li>
              <a href="https://google.de" class="item"><img src="https://imageshack.com/i/potj2pVwp"> <span class="caption">Home</span></a>
            </li>
            <li>
              <a href="#" class="item"><img src="https://imageshack.com/i/pntQ9nVMp"><span class="caption">About Me</span></a>
              <ul>
                <li><a href="#">What I do</a></li>
                <li><a href="#">Motorcycling</a></li>
                <li><a href="#">Music</a></li>
              </ul>
            </li>
            <li>
              <a href="#" class="item"><img src="https://imageshack.com/i/po4WFq6Yp"><span class="caption">Professional</span></a>
              <ul>
                <li><a href="#">Current Employment</a></li>
                <li><a href="#">Working Experience</a></li>
                <li><a href="#">Education</a></li>
              </ul>
            </li>
            <li>
              <a href="#" class="item"><img src="https://imageshack.com/i/pmcfm7Kbp"> <span class="caption">Projects</span></a>
              <ul>
                <li><a href="#">Subnav Item</a></li>
                <li><a href="#">Subnav Item</a></li>
                <li><a href="#">Subnav Item</a></li>
              </ul>
            </li>
            <li>
              <a href="#" class="item"><img src="https://imageshack.com/i/pnM0Fmgrp"> <span class="caption">Misc</span></a>
              <ul>
                <li><a href="#">Subnav Item</a></li>
                <li><a href="#">Subnav Item</a></li>
                <li><a href="#">Subnav Item</a></li>
              </ul>
            </li>
            <li>
              <a href="#" class="item"><img src="https://imageshack.com/i/poqRnk6ap"> <span class="caption">Testing</span></a>
              <ul>
                <li><a href="#">Subnav Item</a></li>
                <li><a href="#">Subnav Item</a></li>
                <li><a href="#">Subnav Item</a></li>
              </ul>
            </li>
            <li>
              <a href="#" class="item"><img src="https://imageshack.com/i/pmc8tts9p"> <span class="caption">Contact</span></a>
            </li>
          </ul>
        </nav>
      </div>

    【讨论】:

    • 谢谢,这看起来是一个有趣的方法!请问导航标签的好处是什么? w3schools 只列出了浏览器可能会跳过渲染,但没有更深入。最后,您是否知道如何处理第二个问题 - 最后一个子菜单过度扩展页面的事实?
    • 对于初学者来说,语义标签的使用非常适合 SEO。当一切都井井有条且合乎逻辑时,它还可以提供更好的用户体验。 HTML5 只是有一个更流畅的方法。虽然仍有一些浏览器不支持 HTML5 中的某些标签,但在站点范围内实现它仍然是更好的选择。 --------- 最后一个子菜单超出了页面,因为它的选项超出了页面。您可以将子菜单设置为bottom: 0,而不是top: 0。 -------- 我会发布一些链接以供进一步阅读。
    • 延伸阅读:Dzone:10 大好处 link Hubpages 5 好处 link GoMoLearning 为什么 HTML5 很重要 link Comlabindia 6 用 HTML5 开发内容的好处 link
    • 感谢您提供阅读材料 - 非常有帮助!所以最后一个子菜单还是很头疼的。绝对 position-bottom: 0 对我的生活不起作用。我将使用更新的代码更新原始问题。如果你能看一下就太好了!
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-05-15
    • 1970-01-01
    • 1970-01-01
    • 2015-01-27
    • 2015-03-18
    • 1970-01-01
    相关资源
    最近更新 更多