如何在Bootstrap 4中隐藏和替换navbar-toggler-icon?

时间:2017-01-26 13:21:38

标签: html css html5 bootstrap-4 twitter-bootstrap-4

如何在Bootstrap 4中隐藏和替换navbar-toggler-icon?现在,下面的代码只是将'X'放在汉堡菜单下面。

<nav class="navbar navbar-toggleable-md">
  <button class="navbar-toggler navbar-toggler-right" type="button" data-toggle="collapse" 
  data-target="#navbarsExampleDefault" aria-controls="navbarsExampleDefault" aria-expanded="false" 
  aria-label="Toggle navigation">
    <span class="navbar-toggler-icon"></span>
  </button>

  <div class="collapse navbar-collapse" id="navbarsExampleDefault">

     <button class="navbar-toggler navbar-toggler-right" type="button" data-toggle="collapse" 
  data-target="#navbarsExampleDefault" aria-controls="navbarsExampleDefault" aria-expanded="false" 
  aria-label="Toggle navigation">
    <span>X</span>
  </button>
</nav>

2 个答案:

答案 0 :(得分:7)

您现在拥有的代码有2个切换器,但我不确定原因。您只需要用普通的navbar-toggler-icon替换X ..

<nav class="navbar navbar-faded navbar-toggleable-md">
    <button class="navbar-toggler navbar-toggler-right py-2" type="button" data-toggle="collapse" data-target="#navbarsExampleDefault" aria-controls="navbarsExampleDefault" aria-expanded="false" aria-label="Toggle navigation">
       <span>X</span>
    </button>
    <a href="" class="navbar-brand">Brand</a>
    <div class="collapse navbar-collapse" id="navbarsExampleDefault">
        <ul class="navbar-nav">
            <li><a href="">Link</a></li>
        </ul>
    </div>
</nav>

编辑要在汉堡包切换器图标和关闭X之间切换,您需要在navbar-toggler内同时拥有X和汉堡包图标。添加一些CSS来处理导航栏的collapsed状态。

.navbar-toggler>.close {
    display:inline;
}
.navbar-toggler.collapsed>.close, .navbar-toggler:not(.collapsed)>.navbar-toggler-icon {
    display:none;
}

http://www.codeply.com/go/fgHv33Q6UM

更新Bootstrap 4.2

最初的问题/答案是针对Bootstrap 4 alpha。一旦Bootstrap 4处于测试阶段,navbar-toggleable-*类将更改为navbar-expand-*,这将影响导航栏断点。这是Bootstrap 4.2的更新示例......

https://www.codeply.com/go/9AQ4AsYUph

答案 1 :(得分:0)

如果开始出现关闭按钮,请尝试此操作

HTML

<nav class="navbar navbar-light bg-faded navbar-toggleable-md">
    <button class="navbar-toggler navbar-toggler-right collapsed" type="button" data-toggle="collapse" data-target="#navbarsExampleDefault" aria-controls="navbarsExampleDefault" aria-expanded="false" aria-label="Toggle navigation">
       <span class="my-1 mx-2 close">X</span>
       <span class="navbar-toggler-icon"></span>
    </button>
    <a href="" class="navbar-brand">Brand</a>
    <div class="collapse navbar-collapse" id="navbarsExampleDefault">
        <ul class="navbar-nav">
            <li class="nav-item"><a href="" class="nav-link">Link</a></li>
            <li class="nav-item"><a href="" class="nav-link">Link</a></li>
            <li class="nav-item"><a href="" class="nav-link">Link</a></li>
        </ul>
    </div>
</nav>

CSS

.navbar{
  button[aria-expanded="true"] > span.close {
    display: inline;
    color: #fff;
    opacity: 1;
  }

  button[aria-expanded="true"] > span.navbar-toggler-icon {
    display: none;
  }

  button[aria-expanded="false"] > span.close {
    display: none;
  }
}