javascript - 使用 fullpage.js 将类添加到粘性菜单

标签 javascript jquery html css fullpage.js

我正在尝试使用 fullpage.js 将一个类添加到滚动的粘性菜单中.

我想要的是#top-wrapper 在第一部分/页面加载时是正常的,然后当您向下滚动时它会添加一个 is-fixed 类。如果您向上滚动到第一部分,则会删除类 is-fixed。

当前发生的情况是,当用户向下滚动时,类 is-fixed 被添加(这是正确的)。但是,如果您向上滚动到第一部分,则不会删除该类。

这是我的代码:

var fullPageInstance = new fullpage('#page-wrapper', {
    anchors: ['firstPage', 'secondPage', '3rdPage', '4thpage', '5thpage', '6thpage'],
    menu: '#side-menu',
    slidesNavigation: true,
    scrollingSpeed: 1000,
    parallax: true,
    parallaxOptions: {
        type: 'cover',
        percentage: 62,
        property: 'translate'
    },
    onLeave: function(index, nextIndex, direction){
        if (nextIndex != 1){
            $('#top-wrapper').addClass('is-fixed');
        } else {
            $('#top-wrapper').removeClass('is-fixed');
        }
    }
});

最佳答案

试试 next.index 属性

var fullPageInstance = new fullpage('#page-wrapper', {
  anchors: ['firstPage', 'secondPage', '3rdPage', '4thpage', '5thpage', '6thpage'],
  menu: '#side-menu',
  slidesNavigation: true,
  scrollingSpeed: 1000,
  parallax: true,
  parallaxOptions: {
    type: 'cover',
    percentage: 62,
    property: 'translate'
  },
  onLeave: function(index, next, direction) {
    if (next.index != 0) {
      $('#top-wrapper').addClass('is-fixed');
    } else {
      $('#top-wrapper').removeClass('is-fixed');
    }
  }
});
.section:nth-child(odd) {
  background-color: green;
}

.section:nth-child(even) {
  background-color: red;
}

.is-fixed {
  color: #fff;
  position: fixed;
  z-index: 1;
}
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/fullPage.js/3.0.7/fullpage.min.js" integrity="sha256-e13jRNqOX98m6UAwI/yZTpcDseJtA8s86yfFs4Sqrv8=" crossorigin="anonymous"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/fullPage.js/3.0.7/vendors/scrolloverflow.min.js" integrity="sha256-R7CttZ4L0/szai0hbFUlPDiRaEJmqYuvLhyAOr19vQg=" crossorigin="anonymous"></script>

<div id="top-wrapper">top wrapper</div>
<div id="page-wrapper">
  <div class="section">Some section</div>
  <div class="section">Some section</div>
  <div class="section">Some section</div>
  <div class="section">Some section</div>
</div>

注意整页 View 中运行演示

关于javascript - 使用 fullpage.js 将类添加到粘性菜单,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/57737076/

相关文章:

javascript - 晚餐成本计算器无法将字符串转换为整数

javascript - 如何在页面刷新时正确编写 jQuery overscroll 代码以保存 div?

jquery - $(a,this).attr ('href' ) 返回未定义

javascript - 禁用表同一列中的所有复选框,而无需选中一个jquery

javascript - 谁能告诉我是否可以在 wemos d1 mini 上刷新 javascript 代码?

jquery - Bootstrap 3.3.x,折叠时如何获得导航栏菜单项的正确顺序?

javascript - jQuery .val() 测试返回空白

html - Windows 中的静态 html

html - Bootstrap 3 子菜单的 RTL 支持

javascript - pjs 草图和 html Canvas