实现固定侧边栏关闭

时间:2019-02-08 15:32:51

标签: javascript jquery materialize sidenav

我有一个Materialize固定的sidenav,它始终可见。我想添加一个按钮,它将打开和关闭它。

如果我在按钮上添加$('.sidenav').sidenav('close'),则只能关闭侧边栏;与$('.sidenav').sidenav('open')类似。

我不想打两个按钮。我该怎么解决?

此功能可初始化侧边栏:

$(document).ready(function () {
  $('.sidenav').sidenav();
});

这是我的HTML:

<ul id="slide-out" class="sidenav sidenav-fixed">
  <li>
    <div class="user-view">
      <div class="background">
        <img src="img/flag.png">
      </div>
      <a href="#user"><img class="circle" src="img/yuna.jpg"></a>
      <a href="#name"><span class="white-text name">John Doe</span></a>
      <a href="#email"><span class="white-text email">jdandturk@gmail.com</span></a>
    </div>
  </li>
  <div class="nav-wrapper">
    <form>
      <div class="input-field">
        <input id="search" type="search" required>
        <label class="label-icon" for="search"><i class="material-icons">search</i></label>
        <i class="material-icons">close</i>
      </div>
    </form>
  </div>
  <li><a href="#!"><i class="material-icons">account_balance</i>Departments</a></li>
  <li><a href="#!"><i class="material-icons">store</i>Regions</a></li>
  <li><a href="#!"><i class="material-icons">description</i>Districts</a></li>
  <li><a href="#!"><i class="material-icons">panorama</i>Cities</a></li>
  <li><a href="#!"><i class="material-icons">time_to_leave</i>Vehicles</a></li>
  <li><div class="divider"></div></li>
  <li><a class="subheader">Subheader</a></li>
  <li><a class="waves-effect" href="#!">Third Link With Waves</a></li>
  <li><a class="sidenav-close" href="#!">Clicking this will close Sidenav</a></li>
</ul>

我有一个类别为.sidenav-close的按钮,该按钮不起作用(默认)。我认为这是因为我的侧边栏是固定的。

是否有默认方法可以打开和关闭它?

这是我的按钮,需要打开/关闭它:

<a href="#!" data-target="slide-out" class="sidenav-trigger rb_nav_btn">
  <i class="material-icons">menu</i>
</a>

我考虑过编写一个函数,该函数将检查边栏是否打开,然后在click上将其关闭,反之亦然。

1 个答案:

答案 0 :(得分:0)

固定导航栏不是不关闭...。它的唯一含义是提供一种实现始终可见的导航栏的快速方法。因此,您要问的基本上是在初始化后立即打开一个简单的导航栏...简单导航栏和固定导航栏之间的唯一区别是后者开始打开。

顺便说一句,您以为您可以使用Docs中所述的属性instance.isOpen来检查(如果sidenav已打开)。如果是,则将其关闭,否则将其打开。我使用了浮动按钮,并注释了sidenav的默认触发器,因为它的功能已由浮动按钮取代。

注意:要查看其运行效果,您需要运行代码段并单击 FULL PAGE (完整页面)链接,否则您将不会看到导航栏,并且必须手动将其打开。这是因为如果窗口的宽度为<992,查看代码似乎Sidenav会自行关闭:

来源:materialize.js (第5834行)

value: function _handleWindowResize() {
  // Only handle horizontal resizes
  if (this.lastWindowWidth !== window.innerWidth) {
    if (window.innerWidth > 992) {
      this.open();
    } else {
      this.close();
    }
  }

  this.lastWindowWidth = window.innerWidth;
  this.lastWindowHeight = window.innerHeight;
}

这是您的脚本:

document.addEventListener("DOMContentLoaded", function() {
  var elems = document.querySelectorAll(".sidenav");
  var options = {};
  var instances = M.Sidenav.init(elems, options);

  document
    .querySelector("#toggle_sidenav")
    .addEventListener("click", function() {
      var elem = document.querySelector(".sidenav");
      var instance = M.Sidenav.getInstance(elem);

      if (instance.isOpen) {
        console.log("Is open: I need to close it");
        instance.close();
      } else {
        console.log("Is closed: I need to open it");
        instance.open();
      }
    });
});
header, main, footer {
  padding-left: 300px;
}

@media only screen and (max-width: 992px) {
  header,  main, footer {
    padding-left: 0;
  }
}
<!-- Compiled and minified CSS -->
<link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/materialize/1.0.0/css/materialize.min.css">

<!-- Compiled and minified JavaScript -->
<script src="https://cdnjs.cloudflare.com/ajax/libs/materialize/1.0.0/js/materialize.min.js"></script>

<!--Import Google Icon Font-->
<link href="https://fonts.googleapis.com/icon?family=Material+Icons" rel="stylesheet">

<ul id="slide-out" class="sidenav sidenav-fixed">
  <li><a href="#!">First Sidebar Link</a></li>
  <li><a href="#!">Second Sidebar Link</a></li>
</ul>
<!-- <a href="#" data-target="slide-out" class="sidenav-trigger"><i class="material-icons">menu</i></a> -->

<div style='position: absolute; bottom:10px; right:10px'>
  <a id="toggle_sidenav" class="btn-floating btn-large waves-effect waves-light red"><i class="material-icons">add</i></a>
</div>