简体   繁体   English

条件语句中的Fullpage.js调用函数

[英]Fullpage.js call function in a conditional statement

I have code here : 我在这里有代码:

 $('#fullpage').fullpage({ afterLoad: function(anchorLink, index){ if(index == 1){ var $squares = $('.grid-container div'); function imgFade() { $squares.removeClass('active') //Choose 2 random divs var square1 = $squares.eq([Math.floor(Math.random()*$squares.length)]) var square2 = $squares.eq([Math.floor(Math.random()*$squares.length)]) //Assign active class $([square1, square2]).each( function(){ $(this).addClass('active'); }); setTimeout(imgFade, 2000); } imgFade(); } else { return false; } } }); //end fullpage 
 body {margin:0} section { height:100vh; width:100%; } .grid-container {width:100%;} .grid-container div { width:20vw; height:33.33vh; float:left; background: purple; opacity: 1; border:1px solid white; transition: opacity 3s ease; } div.active {opacity: 0.5;} 
 <script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script> <link href="https://cdnjs.cloudflare.com/ajax/libs/fullPage.js/2.9.6/jquery.fullpage.min.css" rel="stylesheet"/> <script src="https://cdnjs.cloudflare.com/ajax/libs/fullPage.js/2.9.6/jquery.fullpage.min.js"></script> <div id="fullpage"> <section id="section0" class="section"> <div class="grid-container"> <div></div> <div></div> <div></div> <div></div> <div></div> <div></div> </div> </section> <section id="section1" class="section"> Some stuff </section> </div> 

I am trying to execute a function using Fullpage.js when the index (section) is equal to 1. I want the function to stop or pause when the user scrolls to the next section. 当索引(部分)等于1时,我试图使用Fullpage.js执行函数。我希望函数在用户滚动到下一部分时停止或暂停。 When I inspect element, I can see the function is still running when I'm no longer on index 1 (the boxes are still changing color), even though I have the function wrapped in a conditional statement. 当我检查元素时,即使我将函数包装在条件语句中,也可以看到不再使用索引1(框仍在更改颜色)时该函数仍在运行。

So I'd like to know how I can only run the function on the first section (index 1), and stop/pause on the other section. 所以我想知道我如何只能在第一部分(索引1)上运行该函数,而在另一部分上停止/暂停。

That's because you are using a timeout. 那是因为您正在使用超时。 You'll have to clear it later on. 您稍后必须清除它。

Follow this schema: 请遵循以下架构:

var timeoutId;

$('#fullpage').fullpage({
  afterLoad: function(anchorLink, index){   
      if(index === 1){
         timeoutId = setTimeout(imgFade, 2000); // <-- timeoutId assignation
      }
      else{
         clearTimeout(timeoutId);   // <-- this will remove the timeout
      }
   }
});

You can even clear it in the onLeave function. 您甚至可以在onLeave函数中清除它。 That will be more precise. 那会更精确。 If the nextIndex is different from 1, then clear it. 如果nextIndex不同于1,则将其清除。

var timeoutId; var timeoutId;

$('#fullpage').fullpage({
  afterLoad: function(anchorLink, index){   
      if(index === 1){
         timeoutId = setTimeout(imgFade, 2000); // <-- timeoutId assignation
      }
   },

   onLeave: function(index, nextIndex, direction){
      if(nextIndex !== 1){
         clearTimeout(timeoutId);   // <-- this will remove the timeout
      }
   }
});

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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