简体   繁体   中英

add class to sticky menu with fullpage.js

I'm trying to add a class to a sticky menu on scroll using fullpage.js .

What I would like is the #top-wrapper is normal on the first section/page load then as you scroll down it adds a class of is-fixed. If you scroll back up to the first section then the class is-fixed is removed.

What currently happens is that as the user scrolls down the class is-fixed is added (which is correct). However if you scroll back up to the first section the class isn't removed.

Here is my code:

 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'); } } }); 

Try next.index property

 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> 

Note: run demo in Full page view

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM