航点无法与我的Sidenav正常配合

时间:2018-07-31 22:02:31

标签: javascript jquery materialize jquery-waypoints

我正在尝试在我的网站上进行侧面导航,以在其四个按钮中添加“活动”类,但我似乎无法使其正常运行。

我已经成功地将航路点添加到代码中,但是它们似乎总是有些偏离,并且需要用户额外滚动一点以激活航路点。我也尝试遵循文档中的{offset}规则,但是没有任何面纱。他们要么从头开始停止正常工作,要么从头到尾停止工作。

为了使sidenav正常工作,我将页面分为几列,如下面的CSS所示。请随意提供有关代码的见解,因为这是一项学习练习,我知道目前我的代码非常脏(尤其是Javascript)

侧面导航:

<div class="sidebar verticalized" id="sidebar-verticalized">
  <ul id="sidenav" class="side nav-fixed hide-on-med-and-down">
    <li class="side-link">
      <a class="side-link-first link1" onclick="clickOne()" href="#">01</a>
    </li>
    <li class="side-link">
      <a href="#portfolio" onclick="clickTwo()" class="link2">02</a>
    </li>
    <li class="side-link">
      <a href="#what-we-do" onclick="clickThree()" class="link3">03</a>
    </li>
    <li class="side-link">
      <a href="#contact" onclick="clickFour()" class="link4">04</a>
    </li>
  </ul>
</div>

CSS:

html {
overflow: scroll;
overflow-x: hidden;
}
::-webkit-scrollbar {
width: 0px;  /* remove scrollbar space */
background: transparent;  /* optional: just make scrollbar invisible */
}

.page{
display: grid;
grid-template-columns: 300px auto;
}
.sidebar{
position:fixed;
width:300px;
}
.main{
grid-column-start:2;
}
.verticalized {
margin: 0px;
padding:0px;
float: left;
top: 50%;
transform: translateY(-50%) translateX(-50%);
left:9%;
}

我的JS烂摊子(每个部分都在下面声明):

var $section1 = $('.header');
var $section2 = $('.portfolio');
var $section3 = $('.what-we-do');
var $section4 = $('.contact');
var $link1 = $('.link1');
var $link2 = $('.link2');
var $link3 = $('.link3');
var $link4 = $('.link4');

$section1.waypoint(function (){
  $link1.addClass('active');
  $link2.removeClass('active');
  $link3.removeClass('active');
  $link4.removeClass('active');
});

$section2.waypoint(function(){
  $link1.removeClass('active');
  $link2.addClass('active');
  $link3.removeClass('active');
  $link4.removeClass('active');
});

$section3.waypoint(function(){
  $link1.removeClass('active');
  $link2.removeClass('active');
  $link3.addClass('active');
  $link4.removeClass('active');
});

$section4.waypoint(function(){
  $link1.removeClass('active');
  $link2.removeClass('active');
  $link3.removeClass('active');
  $link4.addClass('active');
});

到目前为止,我已经尝试过:

偏移:视图底部(部分有时太大,因此仍保留旧的活动元素)

偏移量:+/- x%(这从一端解决了问题,但从另一端没有解决:我可能在链接上从1变为4,并且可以正常工作,但从4到1断开了,反之亦然)

欢迎任何意见和提示。我正在尝试模仿每个部分的活动项目的引导导航栏行为。

1 个答案:

答案 0 :(得分:0)

<link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/4.3.1/css/bootstrap.min.css">

     <!-- jQuery library -->
     <script src="https://ajax.googleapis.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>

     <!-- Popper JS -->
     <script src="https://cdnjs.cloudflare.com/ajax/libs/popper.js/1.14.7/umd/popper.min.js"></script>

           <!-- Latest compiled JavaScript -->
      <script src="https://maxcdn.bootstrapcdn.com/bootstrap/4.3.1/js/bootstrap.min.js"></script>

      <style>
      @import url('https://fonts.googleapis.com/css?family=Varela+Round');
      html, body {
          overflow-x: hidden;
          height: 100%;
      }
      body {
          background: #fff;
          padding: 0;
          margin: 0;
          font-family: 'Varela Round', sans-serif;
      }
      .header {
          display: block;
          margin: 0 auto;
          width: 100%;
          max-width: 100%;
          box-shadow: none;
          background-color: #FC466B;
          position: fixed;
          height: 60px!important;
          overflow: hidden;
          z-index: 10;
      }
      .main {
          margin: 0 auto;
          display: block;
          height: 100%;
          margin-top: 60px;
      }
      .mainInner{
          display: table;
          height: 100%;
          width: 100%;
          text-align: center;
      }
      .mainInner div{
          display:table-cell;
          vertical-align: middle;
          font-size: 3em;
          font-weight: bold;
          letter-spacing: 1.25px;
      }
      #sidebarMenu {
          height: 100%;
          position: fixed;
          left: 0;
          width: 250px;
          margin-top: 60px;
          transform: translateX(-250px);
          transition: transform 250ms ease-in-out;
          background:#414956;
      }
      .sidebarMenuInner{
          margin:0;
          padding:0;
          border-top: 1px solid rgba(255, 255, 255, 0.10);
      }
      .sidebarMenuInner li{
          list-style: none;
          color: #fff;
          text-transform: uppercase;
          font-weight: bold;
          padding: 20px;
          cursor: pointer;
          border-bottom: 1px solid rgba(255, 255, 255, 0.10);
      }
      .sidebarMenuInner li span{
          display: block;
          font-size: 14px;
          color: rgba(255, 255, 255, 0.50);
      }
      .sidebarMenuInner li a{
          color: #fff;
          text-transform: uppercase;
          font-weight: bold;
          cursor: pointer;
          text-decoration: none;
      }
      input[type="checkbox"]:checked ~ #sidebarMenu {
          transform: translateX(0);
      }

      input[type=checkbox] {
          transition: all 0.3s;
          box-sizing: border-box;
          display: none;
      }
      .sidebarIconToggle {
          transition: all 0.3s;
          box-sizing: border-box;
          cursor: pointer;
          position: absolute;
          z-index: 99;
          height: 100%;
          width: 100%;
          top: 22px;
          left: 15px;
          height: 22px;
          width: 22px;
      }
      .spinner {
          transition: all 0.3s;
          box-sizing: border-box;
          position: absolute;
          height: 3px;
          width: 100%;
          background-color: #fff;
      }
      .horizontal {
          transition: all 0.3s;
          box-sizing: border-box;
          position: relative;
          float: left;
          margin-top: 3px;
      }
      .diagonal.part-1 {
          position: relative;
          transition: all 0.3s;
          box-sizing: border-box;
          float: left;
      }
      .diagonal.part-2 {
          transition: all 0.3s;
          box-sizing: border-box;
          position: relative;
          float: left;
          margin-top: 3px;
      }
      input[type=checkbox]:checked ~ .sidebarIconToggle > .horizontal {
          transition: all 0.3s;
          box-sizing: border-box;
          opacity: 0;
      }
      input[type=checkbox]:checked ~ .sidebarIconToggle > .diagonal.part-1 {
          transition: all 0.3s;
          box-sizing: border-box;
          transform: rotate(135deg);
          margin-top: 8px;
      }
      input[type=checkbox]:checked ~ .sidebarIconToggle > .diagonal.part-2 {
          transition: all 0.3s;
          box-sizing: border-box;
          transform: rotate(-135deg);
          margin-top: -9px;
      }
      </style>
      <body>

      <div class="header"></div>
        <input type="checkbox" class="openSidebarMenu" id="openSidebarMenu">
        <label for="openSidebarMenu" class="sidebarIconToggle">
          <div class="spinner diagonal part-1"></div>
          <div class="spinner horizontal"></div>
          <div class="spinner diagonal part-2"></div>
        </label>
        <div id="sidebarMenu">
          <ul class="sidebarMenuInner">
            <li><a href="#">Jelena Jovanovic</a></li>
            <li><a href="#">Company</a></li>
            <li><a href="">Instagram</a></li>
            <li><a href="">Twitter</a></li>
            <li><a href="">YouTube</a></li>
            <li><a href="">Linkedin</a></li>
          </ul>
        </div>
        <div id='center' class="main center">
          <div class="mainInner">
            <div>PURE CSS SIDEBAR TOGGLE MENU</div>
          </div>
          <div class="mainInner">
            <div>PURE CSS SIDEBAR TOGGLE MENU</div>
          </div>
          <div class="mainInner">
            <div>PURE CSS SIDEBAR TOGGLE MENU</div>
          </div>
        </div>
      </body>`enter code here`