【问题标题】:Setting bootstrap dropdown width equal to container设置引导下拉菜单宽度等于容器
【发布时间】:2014-07-14 00:24:43
【问题描述】:

我的左边是260px 宽,右边是260px 宽。我的中心 div 是流动的 100% 宽度。

点击右箭头/中心 div,我想要一个与中心 div 容器宽度相同的dropdown。下面是我的代码和fiddle

<div class="navbar navbar-default" role="navigation">
  <div class="container-fluid">    
   <div class="row">
    <div class="leftBox pull-left" href="#">
      <img src="//placehold.it/258x58">
    </div>
    <div class="rightBox pull-right" href="#">
      Right Div
    </div>
     <div class="centerBox">
       <ul class="nav pull-right">
          <li class="dropdown" id="menuLogin">
            <a class="dropdown-toggle" href="#" data-toggle="dropdown" id="navLogin">
              <b class="glyphicon glyphicon-chevron-down"></b></a>
              <div class="dropdown-menu">
                <form class="form" id="formLogin"> 
                  <input name="username" id="username" placeholder="Username" type="text"> 
                  <input name="password" id="password" placeholder="Password" type="password"><br>
                  <button type="button" id="btnLogin" class="btn">Login</button>
                </form>
            </div>
          </li>
        </ul>
    </div>
  </div>
</div>
</div> 

http://www.bootply.com/82O0SgmP8p

【问题讨论】:

  • 您想要只使用 CSS 还是可以使用 Javascript/jQuery?既然你已经标记了 jQuery,我可以假设你对 jQuery 解决方案没问题吗?
  • @Harry:我只是在思考如何使下拉菜单的宽度等于中心 div 容器的宽度。我可以接受任何解决方案。

标签: jquery css twitter-bootstrap twitter-bootstrap-3


【解决方案1】:

这是一个基于 jQuery 的解决方案,它计算中心框的 width(在 load 和窗口大小调整期间),然后将 width 分配给登录框并根据需要定位它。

注意:请参阅 cmets inline 以了解这些步骤。

$(document).ready(function(){
  var cntrboxWidth = $('#centerBox').width()-520; // Width of center box minus width of the two side boxez
  $('#navLogin + .dropdown-menu').width(cntrboxWidth); // Setting the width of Login box
  var extraWidth = $('#navLogin').outerWidth(); // Getting the width of the arrow icon box to adjust position
  $('#navLogin + .dropdown-menu').css('left',-(cntrboxWidth)+extraWidth); // Positioning the login box

  $(window).resize(function(){ // Adjusting position and size upon resizing window.
    cntrboxWidth = $('#centerBox').width()-520;
    $('#navLogin + .dropdown-menu').width(cntrboxWidth);
    extraWidth = $('#navLogin').outerWidth();
    $('#navLogin + .dropdown-menu').css('left',-(cntrboxWidth)+extraWidth); 
  });

});

Demo

编辑:要将登录框准确定位在菜单下方,请使用以下 CSS。它不必是动态的,因为根据 CSS,您的菜单栏有一个固定的高度(60 像素)。

.dropdown-menu{
    top: 60px; // Equal to the height of the menu
}

【讨论】:

  • 再次感谢Harry,您能否让下拉菜单与菜单末尾完全对齐。它有点重叠......如图所示。
  • 再次感谢Harry,最后一个问题...您认为我要进行的布局是正确的还是应该继续使用基于网格的系统进行更改。顺便说一句,它是一个导航栏
  • 如果可能的话,我们能否在不使用 jQuery 的情况下使用 css 实现相同的目标?
  • 考虑到宽度计算和动态定位,可能是可能的,但很难。
  • @theJava:我假设您指的是箭头按钮。它下降了一行,因为第一行是一个块。为了使两者在同一行中,您需要将其设置为内联块。检查this。我已将最后两行添加到 CSS 中以进行定位。但我认为也许是时候从任何使用过引导程序的人那里获得一些专家建议,看看是否有任何其他内置设计来解决这些问题。 (注意:我已经删除了一些过去的 cmets 以使 conv 更短)。
【解决方案2】:

为什么不使用grid system 而不是左右浮动列?

使用网格你可以做这样的事情

<div class="container">
    <div class="row">
        <div class="col-sm-3 width-260">
            <!-- left box goes here -->
        </div>

        <div class="col-sm-6">
            <!-- centre box goes here -->
        </div>

        <div class="col-sm-3 width-260">
            <!-- right box goes here -->
        </div>
    </div>
</div>

然后将您的下拉菜单放在中心栏中。以这种方式设置样式会容易得多(即,将其设为包含元素的width: 100% 等)。

您所要做的就是添加一些自定义样式,以确保左右列固定在260px

更新:要使左右两列的宽度为 260px,请将其添加到您的样式表中:

.width-260 {
    width: 260px;
}

【讨论】:

  • 谢谢亨利,你能帮我在网格系统到位的情况下将左右列设置为 260 像素,并以 100% 的宽度居中。再次感谢。
  • 查看我的更新。另请注意,我已为每一列(左和右)添加了 width-260 类。默认情况下,中心列应该已经是流动的了。
猜你喜欢
  • 1970-01-01
  • 2018-03-08
  • 2014-05-27
  • 2016-08-27
  • 2015-06-02
  • 2019-02-13
  • 2021-11-17
  • 1970-01-01
相关资源
最近更新 更多