使用纯JS& amp;动画导航栏折叠CSS

时间:2017-04-14 18:49:20

标签: javascript css twitter-bootstrap-3

我想从我的bootstrap模板中删除jquery,因为我不使用JS Components。所以我删除了它并在纯JS中添加了navbar-toggle的回退代码(检查attatched JSFiddle)。 现在我想设置导航栏切换的动画(使用CSS或纯JS)。

我尝试使用max-height过渡属性。但我不知道最终的高度,因为里面有一个下拉。因此,如果我使用一些很高的高度,那么崩溃就会延迟。

我使用getbootstrap中的示例模板创建了一个JSFiddle:https://jsfiddle.net/c5f82stw/

// Navbar and dropdowns
var toggle = document.getElementsByClassName('navbar-toggle')[0],
  collapse = document.getElementsByClassName('navbar-collapse')[0],
  dropdowns = document.getElementsByClassName('dropdown');;

// Toggle if navbar menu is open or closed
function toggleMenu() {
  collapse.classList.toggle('collapse');
  collapse.classList.toggle('in');
}

// Close all dropdown menus
function closeMenus() {
  for (var j = 0; j < dropdowns.length; j++) {
    dropdowns[j].getElementsByClassName('dropdown-toggle')[0].classList.remove('dropdown-open');
    dropdowns[j].classList.remove('open');
  }
}

// Add click handling to dropdowns
for (var i = 0; i < dropdowns.length; i++) {
  dropdowns[i].addEventListener('click', function() {
    if (document.body.clientWidth < 768) {
      var open = this.classList.contains('open');
      closeMenus();
      if (!open) {
        this.getElementsByClassName('dropdown-toggle')[0].classList.toggle('dropdown-open');
        this.classList.toggle('open');
      }
    }
  });
}

// Close dropdowns when screen becomes big enough to switch to open by hover
function closeMenusOnResize() {
  if (document.body.clientWidth >= 768) {
    closeMenus();
    collapse.classList.add('collapse');
    collapse.classList.remove('in');
  }
}

// Event listeners
window.addEventListener('resize', closeMenusOnResize, false);
toggle.addEventListener('click', toggleMenu, false);
<link href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/css/bootstrap.min.css" rel="stylesheet" />
<div class="container">

  <!-- Static navbar -->
  <nav class="navbar navbar-default">
    <div class="container-fluid">
      <div class="navbar-header">
        <button type="button" class="navbar-toggle collapsed" data-toggle="collapse" data-target="#navbar" aria-expanded="false" aria-controls="navbar">
              <span class="sr-only">Toggle navigation</span>
              <span class="icon-bar"></span>
              <span class="icon-bar"></span>
              <span class="icon-bar"></span>
            </button>
        <a class="navbar-brand" href="#">Project name</a>
      </div>
      <div id="navbar" class="navbar-collapse collapse">
        <ul class="nav navbar-nav">
          <li class="active"><a href="#">Home</a></li>
          <li><a href="#">About</a></li>
          <li><a href="#">Contact</a></li>
          <li class="dropdown">
            <a href="#" class="dropdown-toggle" data-toggle="dropdown" role="button" aria-haspopup="true" aria-expanded="false">Dropdown <span class="caret"></span></a>
            <ul class="dropdown-menu">
              <li><a href="#">Action</a></li>
              <li><a href="#">Another action</a></li>
              <li><a href="#">Something else here</a></li>
              <li role="separator" class="divider"></li>
              <li class="dropdown-header">Nav header</li>
              <li><a href="#">Separated link</a></li>
              <li><a href="#">One more separated link</a></li>
            </ul>
          </li>
        </ul>
        <ul class="nav navbar-nav navbar-right">
          <li class="active"><a href="./">Default <span class="sr-only">(current)</span></a></li>
          <li><a href="../navbar-static-top/">Static top</a></li>
          <li><a href="../navbar-fixed-top/">Fixed top</a></li>
        </ul>
      </div>
      <!--/.nav-collapse -->
    </div>
    <!--/.container-fluid -->
  </nav>

  <!-- Main component for a primary marketing message or call to action -->
  <div class="jumbotron">
    <h1>Navbar example</h1>
    <p>This example is a quick exercise to illustrate how the default, static navbar and fixed to top navbar work. It includes the responsive CSS and HTML, so it also adapts to your viewport and device.</p>
    <p>
      <a class="btn btn-lg btn-primary" href="../../components/#navbar" role="button">View navbar docs &raquo;</a>
    </p>
  </div>

</div>
<!-- /container -->

1 个答案:

答案 0 :(得分:1)

我知道这不是我想要的。我在寻找JS。

根据您的需要调整最大高度。下拉列表需要额外的高度才能工作。记住这一点。

.navbar-collapse.in 

    max-height: 65em;

JSFiddle:https://jsfiddle.net/g3hwf2yo/2/

.navbar-collapse{
    max-height: 0px;
transition: max-height 0.5s cubic-bezier(0, 1, 0, 1);
background: #d5d5d5;
overflow: hidden;
display: block;
    margin-top: 0.5em;
}
.navbar-collapse.in {

    max-height: 65em;
    transition: max-height 1s ease-in-out;
}

这使用立方贝塞尔来微调动画。

    <link href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/css/bootstrap.min.css" rel="stylesheet" />
<div class="container">

  <!-- Static navbar -->
  <nav class="navbar navbar-default">
    <div class="container-fluid">
      <div class="navbar-header">
        <button type="button" class="navbar-toggle collapsed" data-toggle="collapse" data-target="#navbar" aria-expanded="false" aria-controls="navbar">
              <span class="sr-only">Toggle navigation</span>
              <span class="icon-bar"></span>
              <span class="icon-bar"></span>
              <span class="icon-bar"></span>
            </button>
        <a class="navbar-brand" href="#">Project name</a>
      </div>
      <div id="navbar" class="navbar-collapse collapse">
        <ul class="nav navbar-nav">
          <li class="active"><a href="#">Home</a></li>
          <li><a href="#">About</a></li>
          <li><a href="#">Contact</a></li>
          <li class="dropdown">
            <a href="#" class="dropdown-toggle" data-toggle="dropdown" role="button" aria-haspopup="true" aria-expanded="false">Dropdown <span class="caret"></span></a>
            <ul class="dropdown-menu">
              <li><a href="#">Action</a></li>
              <li><a href="#">Another action</a></li>
              <li><a href="#">Something else here</a></li>
              <li role="separator" class="divider"></li>
              <li class="dropdown-header">Nav header</li>
              <li><a href="#">Separated link</a></li>
              <li><a href="#">One more separated link</a></li>
            </ul>
          </li>
        </ul>
        <ul class="nav navbar-nav navbar-right">
          <li class="active"><a href="./">Default <span class="sr-only">(current)</span></a></li>
          <li><a href="../navbar-static-top/">Static top</a></li>
          <li><a href="../navbar-fixed-top/">Fixed top</a></li>
        </ul>
      </div>
      <!--/.nav-collapse -->
    </div>
    <!--/.container-fluid -->
  </nav>

  <!-- Main component for a primary marketing message or call to action -->
  <div class="jumbotron">
    <h1>Navbar example</h1>
    <p>This example is a quick exercise to illustrate how the default, static navbar and fixed to top navbar work. It includes the responsive CSS and HTML, so it also adapts to your viewport and device.</p>
    <p>
      <a class="btn btn-lg btn-primary" href="../../components/#navbar" role="button">View navbar docs &raquo;</a>
    </p>
  </div>

</div>
<!-- /container -->

最后JS

// Navbar and dropdowns
var toggle = document.getElementsByClassName('navbar-toggle')[0],
  collapse = document.getElementsByClassName('navbar-collapse')[0],
  dropdowns = document.getElementsByClassName('dropdown');;

// Toggle if navbar menu is open or closed
function toggleMenu() {
  collapse.classList.toggle('collapse');
  collapse.classList.toggle('in');
}

// Close all dropdown menus
function closeMenus() {
  for (var j = 0; j < dropdowns.length; j++) {
    dropdowns[j].getElementsByClassName('dropdown-toggle')[0].classList.remove('dropdown-open');
    dropdowns[j].classList.remove('open');
  }
}

// Add click handling to dropdowns
for (var i = 0; i < dropdowns.length; i++) {
  dropdowns[i].addEventListener('click', function() {
    if (document.body.clientWidth < 768) {
      var open = this.classList.contains('open');
      closeMenus();
      if (!open) {
        this.getElementsByClassName('dropdown-toggle')[0].classList.toggle('dropdown-open');
        this.classList.toggle('open');
      }
    }
  });
}

// Close dropdowns when screen becomes big enough to switch to open by hover
function closeMenusOnResize() {
  if (document.body.clientWidth >= 768) {
    closeMenus();
    collapse.classList.add('collapse');
    collapse.classList.remove('in');
  }
}

// Event listeners
window.addEventListener('resize', closeMenusOnResize, false);
toggle.addEventListener('click', toggleMenu, false);

度过美好的一天!