代码之家  ›  专栏  ›  技术社区  ›  Sean Ad

带有固定高度自定义指针的导航栏

  •  0
  • Sean Ad  · 技术社区  · 3 年前

    我创建了一个测试页面,其中一个菜单栏在悬停时有一个三角形指针。除了蓝色菜单栏在底部添加指针时会增加高度外,其余部分看起来都很好。我可以想到一种方法,给菜单栏固定高度,并在媒体查询过程中删除这种样式。

    在不改变高度的情况下添加指针是否有更好/简单的解决方案?

    (三角形应该与菜单栏最底部的边界接触,没有间隙。我将在解决高度问题后进行调整)

    TIA

    截图: enter image description here 代码:

    <link href="https://cdn.jsdelivr.net/npm/bootstrap@4.6.0/dist/css/bootstrap.min.css" rel="stylesheet" />
    <link href="https://cdnjs.cloudflare.com/ajax/libs/font-awesome/5.15.3/css/all.min.css" rel="stylesheet" />
    <script src="https://code.jquery.com/jquery-3.5.1.slim.min.js"></script>
    <script src="https://cdn.jsdelivr.net/npm/bootstrap@4.6.0/dist/js/bootstrap.bundle.min.js"></script>
    <style>
        .bg-banner {
            background-image: linear-gradient(#17517f, #46769d);
        }
    
        .bg-headbar {
            background-color: #005db9;
        }
    
        .bg-banner .nav-link {
            color: white !important;
        }
    
        #menubar .nav-link {
            font-size: 16px;
            padding-right: 50px;
            color: white;
        }
            #menubar .nav-link:focus,
            #menubar .nav-link:hover,
            #menubar .nav-link:visited {
                color: white;
            }
        @media (min-width:768px) {
            nav.navbar .navbar-nav li.nav-item.active:after {
                content: "";
                position: relative;
                margin-left: -31px;
                left: 50%;
                bottom: 15px;
                border-left: 6px solid transparent;
                border-right: 6px solid transparent;
                border-bottom: 6px solid white;
            }
        }
    
        @media (max-width:767px) {
            .dropdown-menu > a {
                display: block !important;
            }
        }
    
        #menubar li.dropdown.show {
            position: static;
        }
    
            #menubar li.dropdown.show .dropdown-menu {
                display: table;
                width: 100%;
                text-align: center;
                left: 0;
                right: 0;
                margin: 0;
            }
    
        .dropdown-menu > a {
            display: table-cell;
        }
    
        .dropdown-menu > a {
            font-weight: 600;
        }
    </style>
    
    <nav class="navbar navbar-expand-md navbar-dark text-white bg-banner">
        <a class="navbar-brand" href="#">Test Site</a>
        <button class="navbar-toggler" type="button" data-toggle="collapse" data-target=".navbar-toggle">
            <span class="navbar-toggler-icon"></span>
        </button>
        <div class="collapse navbar-collapse navbar-toggle">
            <form class="form-inline my-2 .m-md-0 ml-auto">
                <div class="input-group">
                    <input type="text" class="form-control form-control-sm" placeholder="Seach" size="40">
                    <div class="input-group-append">
                        <button class="btn btn-outline-light btn-sm" type="button"><i class="fa fa-search"></i></button>
                    </div>
                </div>
            </form>
        </div>
    
        <div class="banner-navbar collapse navbar-collapse flex-grow-0 navbar-toggle">
            <nav class="navbar navbar-expand-sm navbar-light">
                <ul class="navbar-nav">
                    <li class="nav-item dropdown">
                        <a class="nav-link" data-toggle="dropdown" role="button" href="#">
                            <i class="fa fa-user"></i>
                        </a>
                        <div class="dropdown-menu">
                            <a class="dropdown-item" href="#">Sign Out</a>
                        </div>
                    </li>
                    <li class="nav-item">
                        <a href="#" class="nav-link"><i class="fa fa-cog"></i></a>
                    </li>
                    <li class="nav-item dropdown">
                        <a class="nav-link" data-toggle="dropdown" role="button" href="#">
                            <i class="fa fa-question-circle"></i>
                        </a>
                        <div class="dropdown-menu">
                            <a class="dropdown-item" href="#">Help</a>
    
                        </div>
                    </li>
                </ul>
            </nav>
        </div>
    
    </nav>
    <nav class="navbar navbar-expand-md navbar-dark text-white bg-headbar py-0 py-8-md">
        <div class="collapse navbar-collapse navbar-toggle" id="menubar">
            <ul class="navbar-nav mr-auto">
                <li class="nav-item dropdown">
                    <a class="nav-link" href="#" id="navbarDropdown1" role="button" data-toggle="dropdown" aria-haspopup="true" aria-expanded="false">
                        Menu1
                    </a>
                    <div class="dropdown-menu" aria-labelledby="navbarDropdown1">
                        <a class="dropdown-item" href="#">Home<span class="sr-only"> (current)</span></a>
                        <a class="dropdown-item" href="#">Submenu 1B</a>
                    </div>
                </li>
                <li class="nav-item dropdown">
                    <a class="nav-link" href="#" id="navbarDropdown2" role="button" data-toggle="dropdown" aria-haspopup="true" aria-expanded="false">
                        Menu2
                    </a>
                    <div class="dropdown-menu" aria-labelledby="navbarDropdown2">
                        <a class="dropdown-item" href="#">Submenu 2A</a>
                        <a class="dropdown-item" href="#">Submenu 2B</a>
                    </div>
                </li>
                <li class="nav-item dropdown">
                    <a class="nav-link" href="#" id="navbarDropdown3" role="button" data-toggle="dropdown" aria-haspopup="true" aria-expanded="false">
                        Menu3
                    </a>
                    <div class="dropdown-menu" aria-labelledby="navbarDropdown3">
                        <a class="dropdown-item" href="#">Submenu 3A</a>
                        <a class="dropdown-item" href="#">Submenu 3B</a>
                    </div>
                </li>
            </ul>
        </div>
    </nav>
    
    <div class="container">
         Lorem ipsum dolor sit amet consectetur adipisicing elit. Beatae, blanditiis eaque? Vero consequuntur tempora vitae at maxime, repellendus excepturi quia quibusdam odit sed ullam praesentium natus rerum molestiae cupiditate possimus?
    </div>
    
    <script type="text/javascript">
        $("#menubar li.dropdown").hover(function () {
            $(this).addClass("active show");
        },
            function () {
                $(this).removeClass("active show");
            });
        $("#menubar ul.dropdown-menu>li>a").on("click", function (e) {
            $("#menubar li.dropdown").removeClass("active show");
        });
    </script>

    jsfiddle

    0 回复  |  直到 3 年前
        1
  •  1
  •   Rich DeBourke    3 年前

    我喜欢@joshmoto的解决方案,因为它将子菜单直接对齐在菜单项下,但我认为如果你应用它,你可以让子菜单完全展开 position: static 进入菜单1等列表项。

    如果你的子菜单是全宽的,那么你需要将子菜单向右放置,以使内容对齐(你需要根据菜单文本调整值)。

    $("#menubar li.dropdown").hover(function() {
            $(this).addClass("active show");
        },
        function() {
            $(this).removeClass("active show");
        });
    $("#menubar ul.dropdown-menu>li>a").on("click", function(e) {
        $("#menubar li.dropdown").removeClass("active show");
    });
    <link href="https://cdn.jsdelivr.net/npm/bootstrap@4.6.0/dist/css/bootstrap.min.css" rel="stylesheet"/>
    <link href="https://cdnjs.cloudflare.com/ajax/libs/font-awesome/5.15.3/css/all.min.css" rel="stylesheet"/>
    <script src="https://code.jquery.com/jquery-3.5.1.slim.min.js"></script>
    <script src="https://cdn.jsdelivr.net/npm/bootstrap@4.6.0/dist/js/bootstrap.bundle.min.js"></script>
    
    <style>
        .bg-banner {
            background-image: linear-gradient(#17517f, #46769d);
        }
    
        .bg-headbar {
            background-color: #005db9;
        }
    
        .bg-banner .nav-link {
            color: white !important;
        }
    
        #menubar .nav-link {
            font-size: 16px;
            padding-right: 50px;
            color: white;
        }
    
        #menubar .nav-link:focus,
        #menubar .nav-link:hover,
        #menubar .nav-link:visited {
            color: white;
        }
    
        @media (min-width:768px) {
            .navbar {
                height: 2.5rem;
            }
    
            nav.navbar .navbar-nav li.nav-item.active:after {
                content: "";
                position: relative;
                display: inline-block;
                margin-left: -1.6875rem;
                left: 50%;
                bottom: 1.1875rem;
                border-left: 0.375rem solid transparent;
                border-right: 0.375rem solid transparent;
                border-bottom: 0.375rem solid white;
            }
    
            .navbar-nav li:nth-child(2) .dropdown-menu {
                padding-left: 6.75rem;
            }
    
            .navbar-nav li:nth-child(3) .dropdown-menu {
                padding-left: 13.4375rem;
            }
    
    
            .dropdown-menu>a {
                font-weight: 600;
                float: left;
                width: auto;
                clear: none;
            }
        }
    
        @media (max-width:767px) {
            .dropdown-menu>a {
                display: block !important;
                font-weight: 600;
            }
        }
    
        #menubar li.dropdown.show {
            position: static;
        }
    
        #menubar li.dropdown.show .dropdown-menu {
            display: table;
            width: 100%;
            text-align: center;
            left: 0;
            right: 0;
            margin: 0;
        }
    </style>
    
    <nav class="navbar navbar-expand-md navbar-dark text-white bg-banner">
        <a class="navbar-brand" href="#">Test Site</a>
        <button class="navbar-toggler" type="button" data-toggle="collapse" data-target=".navbar-toggle">
            <span class="navbar-toggler-icon"></span>
        </button>
        <div class="collapse navbar-collapse navbar-toggle">
            <form class="form-inline my-2 .m-md-0 ml-auto">
                <div class="input-group">
                    <input type="text" class="form-control form-control-sm" placeholder="Seach" size="40">
                    <div class="input-group-append">
                        <button class="btn btn-outline-light btn-sm" type="button"><i class="fa fa-search"></i></button>
                    </div>
                </div>
            </form>
        </div>
    
        <div class="banner-navbar collapse navbar-collapse flex-grow-0 navbar-toggle">
            <nav class="navbar navbar-expand navbar-light">
                <ul class="navbar-nav">
                    <li class="nav-item dropdown">
                        <a class="nav-link" data-toggle="dropdown" role="button" href="#">
                            <i class="fa fa-user"></i>
                        </a>
                        <div class="dropdown-menu">
                            <a class="dropdown-item" href="#">Sign Out</a>
                        </div>
                    </li>
                    <li class="nav-item">
                        <a href="#" class="nav-link"><i class="fa fa-cog"></i></a>
                    </li>
                    <li class="nav-item dropdown">
                        <a class="nav-link" data-toggle="dropdown" role="button" href="#">
                            <i class="fa fa-question-circle"></i>
                        </a>
                        <div class="dropdown-menu">
                            <a class="dropdown-item" href="#">Help</a>
    
                        </div>
                    </li>
                </ul>
            </nav>
        </div>
    
    </nav>
    <nav class="navbar navbar-expand-md navbar-dark text-white bg-headbar py-0 py-8-md">
        <div class="collapse navbar-collapse navbar-toggle align-self-start" id="menubar">
            <ul class="navbar-nav mr-auto">
                <li class="nav-item dropdown">
                    <a class="nav-link" href="#" id="navbarDropdown1" role="button" data-toggle="dropdown" aria-haspopup="true" aria-expanded="false">
                        Menu1
                    </a>
                    <div class="dropdown-menu" aria-labelledby="navbarDropdown1">
                        <a class="dropdown-item" href="#">Home<span class="sr-only"> (current)</span></a>
                        <a class="dropdown-item" href="#">Submenu 1B</a>
                    </div>
                </li>
                <li class="nav-item dropdown">
                    <a class="nav-link" href="#" id="navbarDropdown2" role="button" data-toggle="dropdown" aria-haspopup="true" aria-expanded="false">
                        Menu2
                    </a>
                    <div class="dropdown-menu" aria-labelledby="navbarDropdown2">
                        <a class="dropdown-item" href="#">Submenu 2A</a>
                        <a class="dropdown-item" href="#">Submenu 2B</a>
                    </div>
                </li>
                <li class="nav-item dropdown">
                    <a class="nav-link" href="#" id="navbarDropdown3" role="button" data-toggle="dropdown" aria-haspopup="true" aria-expanded="false">
                        Menu3
                    </a>
                    <div class="dropdown-menu" aria-labelledby="navbarDropdown3">
                        <a class="dropdown-item" href="#">Submenu 3A</a>
                        <a class="dropdown-item" href="#">Submenu 3B</a>
                    </div>
                </li>
            </ul>
        </div>
    </nav>
    
    <div class="container">
        Lorem ipsum dolor sit amet consectetur adipisicing elit. Beatae, blanditiis eaque? Vero consequuntur tempora vitae at maxime, repellendus excepturi quia quibusdam odit sed ullam praesentium natus rerum molestiae cupiditate possimus?
    </div>

    因为你没有 dropdown-toggle 如果用户使用tab键协商您的网站,并且子菜单在触摸设备上无法正常工作,则菜单1上的类等项目将无法打开。如果你不需要标签或触摸支持,那么应该没问题。

        2
  •  1
  •   joshmoto    3 年前

    我看到你正试图为每个可悬浮导航项目添加一个三角形指针,我没有给你一个完美的解决方案,我只使用了你的css,并添加了一些tweets,将伪“after”emel作为相对而非静态的位置定位到其父级。

    我的更改撤消了全宽活动导航下拉菜单,但这可能会让你走上正轨

    <link href="https://cdn.jsdelivr.net/npm/bootstrap@4.6.0/dist/css/bootstrap.min.css" rel="stylesheet" />
    <link href="https://cdnjs.cloudflare.com/ajax/libs/font-awesome/5.15.3/css/all.min.css" rel="stylesheet" />
    <script src="https://code.jquery.com/jquery-3.5.1.slim.min.js"></script>
    <script src="https://cdn.jsdelivr.net/npm/bootstrap@4.6.0/dist/js/bootstrap.bundle.min.js"></script>
    <style>
      .bg-banner {
        background-image: linear-gradient(#17517f, #46769d);
      }
      
      .bg-headbar {
        background-color: #005db9;
      }
      
      .bg-banner .nav-link {
        color: white !important;
      }
      
      nav.navbar .navbar-nav li.nav-item {
        position: relative;
      }
      
      #menubar .nav-link {
        font-size: 16px;
        padding-right: 50px;
        color: white;
      }
      
      #menubar .nav-link:focus,
      #menubar .nav-link:hover,
      #menubar .nav-link:visited {
        color: white;
      }
      
      @media (min-width:768px) {
        nav.navbar .navbar-nav li.nav-item.active:after {
          content: "";
          position: absolute;
          margin-left: -31px;
          left: 50%;
          bottom: -1px;
          border-left: 6px solid transparent;
          border-right: 6px solid transparent;
          border-bottom: 6px solid white;
          z-index: 1001;
        }
      }
      
      @media (max-width:767px) {
        .dropdown-menu>a {
          display: block !important;
        }
      }
      
      #menubar li.dropdown.show {
        position: relative;
      }
      
      #menubar li.dropdown.show .dropdown-menu {
        display: table;
        width: 100%;
        text-align: center;
        left: 0;
        right: 0;
        margin: 0;
      }
      
      .dropdown-menu>a {
        display: table-cell;
      }
      
      .dropdown-menu>a {
        font-weight: 600;
      }
    </style>
    
    <nav class="navbar navbar-expand-md navbar-dark text-white bg-banner">
      <a class="navbar-brand" href="#">Test Site</a>
      <button class="navbar-toggler" type="button" data-toggle="collapse" data-target=".navbar-toggle">
            <span class="navbar-toggler-icon"></span>
        </button>
      <div class="collapse navbar-collapse navbar-toggle">
        <form class="form-inline my-2 .m-md-0 ml-auto">
          <div class="input-group">
            <input type="text" class="form-control form-control-sm" placeholder="Seach" size="40">
            <div class="input-group-append">
              <button class="btn btn-outline-light btn-sm" type="button"><i class="fa fa-search"></i></button>
            </div>
          </div>
        </form>
      </div>
    
      <div class="banner-navbar collapse navbar-collapse flex-grow-0 navbar-toggle">
        <nav class="navbar navbar-expand-sm navbar-light">
          <ul class="navbar-nav">
            <li class="nav-item dropdown">
              <a class="nav-link" data-toggle="dropdown" role="button" href="#">
                <i class="fa fa-user"></i>
              </a>
              <div class="dropdown-menu">
                <a class="dropdown-item" href="#">Sign Out</a>
              </div>
            </li>
            <li class="nav-item">
              <a href="#" class="nav-link"><i class="fa fa-cog"></i></a>
            </li>
            <li class="nav-item dropdown">
              <a class="nav-link" data-toggle="dropdown" role="button" href="#">
                <i class="fa fa-question-circle"></i>
              </a>
              <div class="dropdown-menu">
                <a class="dropdown-item" href="#">Help</a>
    
              </div>
            </li>
          </ul>
        </nav>
      </div>
    
    </nav>
    <nav class="navbar navbar-expand-md navbar-dark text-white bg-headbar py-0 py-8-md">
      <div class="collapse navbar-collapse navbar-toggle" id="menubar">
        <ul class="navbar-nav mr-auto">
          <li class="nav-item dropdown">
            <a class="nav-link" href="#" id="navbarDropdown1" role="button" data-toggle="dropdown" aria-haspopup="true" aria-expanded="false">
                        Menu1
                    </a>
            <div class="dropdown-menu" aria-labelledby="navbarDropdown1">
              <a class="dropdown-item" href="#">Home<span class="sr-only"> (current)</span></a>
              <a class="dropdown-item" href="#">Submenu 1B</a>
            </div>
          </li>
          <li class="nav-item dropdown">
            <a class="nav-link" href="#" id="navbarDropdown2" role="button" data-toggle="dropdown" aria-haspopup="true" aria-expanded="false">
                        Menu2
                    </a>
            <div class="dropdown-menu" aria-labelledby="navbarDropdown2">
              <a class="dropdown-item" href="#">Submenu 2A</a>
              <a class="dropdown-item" href="#">Submenu 2B</a>
            </div>
          </li>
          <li class="nav-item dropdown">
            <a class="nav-link" href="#" id="navbarDropdown3" role="button" data-toggle="dropdown" aria-haspopup="true" aria-expanded="false">
                        Menu3
                    </a>
            <div class="dropdown-menu" aria-labelledby="navbarDropdown3">
              <a class="dropdown-item" href="#">Submenu 3A</a>
              <a class="dropdown-item" href="#">Submenu 3B</a>
            </div>
          </li>
        </ul>
      </div>
    </nav>
    
    <div class="container">
      Lorem ipsum dolor sit amet consectetur adipisicing elit. Beatae, blanditiis eaque? Vero consequuntur tempora vitae at maxime, repellendus excepturi quia quibusdam odit sed ullam praesentium natus rerum molestiae cupiditate possimus?
    </div>
    
    <script type="text/javascript">
      $("#menubar li.dropdown").hover(function() {
          $(this).addClass("active show");
        },
        function() {
          $(this).removeClass("active show");
        });
      $("#menubar ul.dropdown-menu>li>a").on("click", function(e) {
        $("#menubar li.dropdown").removeClass("active show");
      });
    </script>
        3
  •  0
  •   Rich DeBourke    3 年前

    为了进行比较,这里有一个使用标准Bootstrap配置的导航栏设置。您不会得到悬停效果,但它应该适用于tab键和触摸设备。

    <link href="https://cdn.jsdelivr.net/npm/bootstrap@4.6.0/dist/css/bootstrap.min.css" rel="stylesheet"/>
    <link href="https://cdnjs.cloudflare.com/ajax/libs/font-awesome/5.15.3/css/all.min.css" rel="stylesheet"/>
    <script src="https://code.jquery.com/jquery-3.5.1.slim.min.js"></script>
    <script src="https://cdn.jsdelivr.net/npm/bootstrap@4.6.0/dist/js/bootstrap.bundle.min.js"></script>
    
    <style>
        .bg-banner {
            background-image: linear-gradient(#17517f, #46769d);
        }
    
        .bg-headbar {
            background-color: #005db9;
        }
    </style>
    
    <nav class="navbar navbar-expand-md navbar-dark text-white bg-banner">
        <a class="navbar-brand mr-auto" href="#">Test Site</a>
        <button class="navbar-toggler" type="button" data-toggle="collapse" data-target=".navbar-toggle">
            <span class="navbar-toggler-icon"></span>
        </button>
        <div class="collapse navbar-collapse flex-grow-0 navbar-toggle">
            <form class="form-inline my-2 my-md-0 mr-md-3">
                <div class="input-group">
                    <input type="text" class="form-control form-control-sm" placeholder="Seach" size="40">
                    <div class="input-group-append">
                        <button class="btn btn-outline-light btn-sm" type="button"><i class="fa fa-search"></i></button>
                    </div>
                </div>
            </form>
        </div>
    
        <!--<div class="banner-navbar collapse navbar-collapse flex-grow-0 navbar-toggle">
            <nav class="navbar navbar-expand navbar-light">-->
            <div class="collapse navbar-collapse flex-grow-0 navbar-toggle">
                <ul class="navbar-nav">
                    <li class="nav-item dropdown">
                        <a class="nav-link" data-toggle="dropdown" role="button" href="#" title="Sign Out">
                            <i class="fa fa-user text-white"></i>
                        </a>
                        <div class="dropdown-menu">
                            <a class="dropdown-item" href="#">Sign Out</a>
                        </div>
                    </li>
                    <li class="nav-item">
                        <a href="#" class="nav-link" title="Setup"><i class="fa fa-cog text-white"></i></a>
                    </li>
                    <li class="nav-item dropdown">
                        <a class="nav-link" title="Help" data-toggle="dropdown" role="button" href="#">
                            <i class="fa fa-question-circle text-white"></i>
                        </a>
                        <div class="dropdown-menu">
                            <a class="dropdown-item" href="#">Help</a>
    
                        </div>
                    </li>
                </ul>
        </div>
    
    </nav>
    <nav class="navbar navbar-expand-md navbar-dark text-white bg-headbar py-0">
        <div class="collapse navbar-collapse navbar-toggle align-self-start" id="menubar">
            <ul class="navbar-nav mr-auto">
                <li class="nav-item dropdown active">
                    <a class="nav-link dropdown-toggle" href="#" id="navbarDropdown1" role="button" data-toggle="dropdown" aria-haspopup="true" aria-expanded="false">
                        Menu1
                    </a>
                    <div class="dropdown-menu" aria-labelledby="navbarDropdown1">
                        <a class="dropdown-item active" href="#">Home<span class="sr-only"> (current)</span></a>
                        <a class="dropdown-item" href="#">Submenu 1B</a>
                    </div>
                </li>
                <li class="nav-item dropdown">
                    <a class="nav-link dropdown-toggle" href="#" id="navbarDropdown2" role="button" data-toggle="dropdown" aria-haspopup="true" aria-expanded="false">
                        Menu2
                    </a>
                    <div class="dropdown-menu" aria-labelledby="navbarDropdown2">
                        <a class="dropdown-item" href="#">Submenu 2A</a>
                        <a class="dropdown-item" href="#">Submenu 2B</a>
                    </div>
                </li>
                <li class="nav-item dropdown">
                    <a class="nav-link dropdown-toggle" href="#" id="navbarDropdown3" role="button" data-toggle="dropdown" aria-haspopup="true" aria-expanded="false">
                        Menu3
                    </a>
                    <div class="dropdown-menu" aria-labelledby="navbarDropdown3">
                        <a class="dropdown-item" href="#">Submenu 3A</a>
                        <a class="dropdown-item" href="#">Submenu 3B</a>
                    </div>
                </li>
            </ul>
        </div>
    </nav>
    
    <div class="container">
        Lorem ipsum dolor sit amet consectetur adipisicing elit. Beatae, blanditiis eaque? Vero consequuntur tempora vitae at maxime, repellendus excepturi quia quibusdam odit sed ullam praesentium natus rerum molestiae cupiditate possimus?
    </div>