当下拉菜单被附加时,会创建微小的空间

Posted

技术标签:

【中文标题】当下拉菜单被附加时,会创建微小的空间【英文标题】:tiny space is created when dropdown gets affix 【发布时间】:2015-11-30 12:57:16 【问题描述】:

当用户滚动网页时,当巨型下拉菜单获得词缀(即粘性)时,会创建微小的空间如何修复它

<!DOCTYPE html>
      <html lang="en">
      <head>
      <meta charset="UTF-8">
      <meta name="viewport" content="width=device-width, initial-scale=1">
      <title>ADC</title>
      <link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.5/css/bootstrap.min.css">
      <link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.5/css/bootstrap-theme.min.css">
      <script src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.3/jquery.min.js"></script>
      <script src="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.5/js/bootstrap.min.js"></script>

这是附加代码: . 附加 顶部:0; 宽度:100%; .导航栏 边框宽度:0px; -webkit-box-shadow:0px 0px; 盒子阴影:0px 0px; 背景颜色:rgba(0,0,0,0.0); 背景图像:-webkit-渐变(线性,50.00% 0.00%,50.00% 100.00%,颜色停止(0%,rgba(0,0,0,0.00)),颜色停止(100%,rgba(0 ,0,0,0.00))); 背景图像:-webkit-linear-gradient(270deg,rgba(0,0,0,0.00) 0%,rgba(0,0,0,0.00) 100%); 背景图像:线性渐变(180度,rgba(0,0,0,0.00)0%,rgba(0,0,0,0.00)100%); .navbar-default .navbar-nav > li > a 颜色:黑色; .navbar-default .navbar-nav > .dropdown > a .caret 边框顶部颜色:#fff; 边框底部颜色:#fff; .navbar-default .navbar-brand 颜色:黑色; .menu-大 位置:静态!重要; .megamenu 填充:20px 0px; 宽度:100%; 填充:0; 边距:0; .megamenu>李>ul 填充:0; 边距:0; .megamenu>李>ul>李 列表样式:无; .megamenu> li > ul > li > a 显示:块; 填充:3px 20px; 明确:两者; 字体粗细:正常; 行高:1.428571429; 颜色:#333333; 空白:正常; .megamenu> li ul > li > a, .megamenu> li ul > li > a:focus 文字装饰:无; 颜色:#262626; 背景颜色:#f5f5f5;

      .megamenu.disabled > a,
      .megamenu.disabled > a,
      .megamenu.disabled > a:focus 
        color: #999999;
      
      .megamenu.disabled > a,
      .megamenu.disabled > a:focus 
        text-decoration: none;
        background-color: transparent;
        background-image: none;
        filter: progid:DXImageTransform.Microsoft.gradient(enabled = false);
        cursor: not-allowed;
      
      .megamenu.dropdown-header 
        color: #428bca;
        font-size: 18px;
      

      @media (max-width: 768px) 
        .megamenu
          margin-left: 0 ;
          margin-right: 0 ;
        
        .megamenu> li 
          margin-bottom: 30px;
        
        .megamenu> li:last-child 
          margin-bottom: 0;
        
        .megamenu.dropdown-header 
          padding: 3px 15px !important;

        
        .navbar-nav .open .dropdown-menu .dropdown-header
        color:#fff;
        
      
      </style>
      <script>

      $(document).ready(function()
          $("#flip").click(function()
              $("#panel").slideToggle("slow");
          );

           $("#flip1").click(function()
              $("#panel1").slideToggle("slow");
          );
      );

      </script>
      </head>
      <body>
      <div class="navbar navbar-default">
            <div class="container-fluid">
              <div class="navbar-header">
                <button type="button" class="navbar-toggle" data-toggle="collapse" data-target=".navbar-collapse">
                  <span class="icon-bar"></span>
                  <span class="icon-bar"></span>
                  <span class="icon-bar"></span>
                </button>
                <a class="navbar-brand" href="#">Logo</a>
              </div>
              <div class="navbar-collapse collapse">
                <ul class="nav navbar-nav">
                <li><a href="#">Home</a></li>
                <li class="dropdown menu-large">
                        <a href="#" class="dropdown-toggle" data-toggle="dropdown" id="flip1"> Product Listing <b class="caret"></b> </a>
                        <ul class="dropdown-menu megamenu row" data-spy="affix" data-offset-top="197" id="panel1">
                          <li>
              <div class="col-sm-6 col-md-3">
                <a href="#" class="thumbnail">
                <img src="http://placehold.it/150x120" />
                </a>
              </div>
              <div class="col-sm-6 col-md-3">
            <a href="#" class="thumbnail">
                <img src="http://placehold.it/150x120" />
                </a>
              </div>
              <div class="col-sm-6 col-md-3">
            <a href="#" class="thumbnail">
                <img src="http://placehold.it/150x120" />
                </a>
              </div>
              <div class="col-sm-6 col-md-3">
               <a href="#" class="thumbnail">
                <img src="http://placehold.it/150x120" />
                </a>
            </div>
                        </li>
                        </ul>
                  </li>

                <li class="dropdown menu-large">
                    <a href="#" class="dropdown-toggle" data-toggle="dropdown" id="flip">Categories <b class="caret"></b></a>               
                    <ul class="dropdown-menu megamenu row" data-spy="affix" data-offset-top="197" id="panel">
                        <li class="col-sm-3">
                            <ul>
                                <li class="dropdown-header">Glyphicons</li>
                                <li><a href="#">Available glyphs</a></li>
                                <li class="disabled"><a href="#">How to use</a></li>
                                <li><a href="#">Examples</a></li>
                                <li class="divider"></li>
                                <li class="dropdown-header">Dropdowns</li>
                                <li><a href="#">Example</a></li>
                                <li><a href="#">Aligninment options</a></li>
                                <li><a href="#">Headers</a></li>
                                <li><a href="#">Disabled menu items</a></li>
                            </ul>
                        </li>
                        <li class="col-sm-3">
                            <ul>
                                <li class="dropdown-header">Button groups</li>
                                <li><a href="#">Basic example</a></li>
                                <li><a href="#">Button toolbar</a></li>
                                <li><a href="#">Sizing</a></li>
                                <li><a href="#">Nesting</a></li>
                                <li><a href="#">Vertical variation</a></li>
                                <li class="divider"></li>
                                <li class="dropdown-header">Button dropdowns</li>
                                <li><a href="#">Single button dropdowns</a></li>
                            </ul>
                        </li>
                        <li class="col-sm-3">
                            <ul>
                                <li class="dropdown-header">Input groups</li>
                                <li><a href="#">Basic example</a></li>
                                <li><a href="#">Sizing</a></li>
                                <li><a href="#">Checkboxes and radio addons</a></li>
                                <li class="divider"></li>
                                <li class="dropdown-header">Navs</li>
                                <li><a href="#">Tabs</a></li>
                                <li><a href="#">Pills</a></li>
                                <li><a href="#">Justified</a></li>
                            </ul>
                        </li>
                        <li class="col-sm-3">
                            <ul>
                                <li class="dropdown-header">Navbar</li>
                                <li><a href="#">Default navbar</a></li>
                                <li><a href="#">Buttons</a></li>
                                <li><a href="#">Text</a></li>
                                <li><a href="#">Non-nav links</a></li>
                                <li><a href="#">Component alignment</a></li>
                                <li><a href="#">Fixed to top</a></li>
                                <li><a href="#">Fixed to bottom</a></li>
                                <li><a href="#">Static top</a></li>
                                <li><a href="#">Inverted navbar</a></li>
                            </ul>
                        </li>
                    </ul>

                </li>
            </ul>
            </div>
            </div>
          </div>
            <h1>Responsive Mega Dropdown</h1>
            <h2>Responsive Mega Dropdown</h2>
            <br>
            <h1>Responsive Mega Dropdown</h1>
            <br>
            <br>
            <h1>Responsive Mega Dropdown</h1>
            <br>
            <h4>Responsive Mega Dropdown</h4>
            <br>
            <h5>Responsive Mega Dropdown</h5>
            <h1>Responsive Mega Dropdown</h1>
            <h2>Responsive Mega Dropdown</h2>
            <br>
            <h1>Responsive Mega Dropdown</h1>
            <br>
            <br>
            <h1>Responsive Mega Dropdown</h1>
            <br>
            <h4>Responsive Mega Dropdown</h4>
            <br>
            <h5>Responsive Mega Dropdown</h5>

            <h1>Responsive Mega Dropdown</h1>
            <h2>Responsive Mega Dropdown</h2>
            <br>
            <h1>Responsive Mega Dropdown</h1>
            <br>
            <br>
            <h1>Responsive Mega Dropdown</h1>
            <br>
            <h4>Responsive Mega Dropdown</h4>
            <br>
            <h5>Responsive Mega Dropdown</h5>

            <h1>Responsive Mega Dropdown</h1>
            <h2>Responsive Mega Dropdown</h2>
            <br>
            <h1>Responsive Mega Dropdown</h1>
            <br>
            <br>
            <h1>Responsive Mega Dropdown</h1>
            <br>
            <h4>Responsive Mega Dropdown</h4>
            <br>
            <h5>Responsive Mega Dropdown</h5>
        </body>
        </html>

【问题讨论】:

【参考方案1】:

根据我在将您的代码加载到浏览器时看到的情况,我发现修复它的最简单方法是在您的词缀类中设置top: -1px,给您.affix top: -1px; width: 100%; 。这会告诉浏览器将元素放置在超出视口顶部一个像素的位置,确保不存在间隙。

我希望我有一个关于为什么 0px 不起作用的明确答案,我坚信应该这样做,但是使用 -1px 似乎可以解决这个问题,而您实际上不会丢失菜单中的任何空间。

【讨论】:

以上是关于当下拉菜单被附加时,会创建微小的空间的主要内容,如果未能解决你的问题,请参考以下文章

崩溃中的引导下拉菜单,

下拉菜单--JavaScript触发方法

相对位置不会发生响应

按下后退按钮时,自动折叠移动设备上的下拉导航栏菜单

EasyAdmin Bundle 创建的下拉菜单未在选择时关闭

导航下拉菜单无法正常工作