运行 if 语句只在滚动事件中出现一次

Run if statement only one time inside scroll event

我有这段代码,我试着只执行一次 if 语句。 因此,用户在到达页面中间时只会看到一个警报显示,在跳过警报并继续滚动后,他不应该再次收到警报。 所以我添加了这个 PassedValue 布尔变量,但在显示警报时它没有更改为 true。

$f = $.noConflict();

$f(document).ready(function() {
  $f(window).scroll(function() {
    var scrollAmount = $f(window).scrollTop();
    var documentHeight = $f(document).height();
    var scrollPercent = (scrollAmount / documentHeight) * 100;
    var PassedValue = false;
    //$(".3amal").append(scrollPercent);
    if (scrollAmount > documentHeight * 0.3 && !PassedValue) {
      alert('the user scrolled once!');
      PassedValue = true;
    } else {
      //alert('Nothing done nothing to save');
    }
  });
});
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<div id="container">

  <div id="sidebar">
    <ul>
      <li><a id="aboutlink" href="#" style="position: sticky;top: 0;">auck</a></li>
      <li><a id="projectslink" href="#">Projects</a></li>
      <li><a id="resumelink" href="#">Resume</a></li>
      <li><a id="contactlink" href="#">Contact</a></li>
    </ul>
  </div>
  <br/><br/><br/><br/><br/><br/><br/><br/><br/><br/><br/><br/><br/><br/><br/><br/><br/><br/><br/><br/><br/><br/><br/><br/><br/><br/><br/><br/><br/><br/>
  <div class="3amal">Hellow i'm here</div>
</div><br/><br/><br/>
<br/><br/><br/><br/><br/><br/><br/><br/><br/><br/><br/><br/><br/><br/><br/><br/><br/><br/><br/><br/><br/><br/><br/><br/><br/><br/><br/><br/><br/><br/><br/><br/><br/>
<div id="content">
  <div class="" id="about">
    <p class="header">uck</p>
    <p class="info">Lorem ipsum dolor sit amet, consectetur adipisicing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam, quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo consequat. Duis aute irure
      dolor in reprehenderit in voluptate velit esse cillum dolore eu fugiat nulla pariatur. Excepteur sint occaecat cupidatat non proident, sunt in culpa qui officia deserunt mollit anim id est laborum.</p>
  </div>
  <div class="sections" id="projects">
    <p class="header">Projects</p>
    <p class="info">Projects</p>
  </div>
  <div class="sections" id="resume">
    <p class="header">Resume</p>
    <p class="info">Resume</p>
  </div>
  <div class="sections" id="contact">
    <p class="header">Contact</p>
    <p class="info">Contact</p>
  </div>
</div>



</div>

您可以创建一个外部变量,然后在if语句中检查它:

// somewhere outside your handler/function
let reviewed = false;

// inside
if(scrollAmount > documentHeight*0.3 && !PassedValue && !reviewed) {
  reviewed = true;
  // your logic here
}

PassedValue 应该在滚动事件方法范围之外。每次代码调用滚动事件时,都会在滚动事件内部创建 var passedValue。如果你把它作为全局变量,它应该可以工作。

$f = $.noConflict();

// Moved passedValue to a global variable
var PassedValue=false;

$f(document).ready(function() {
 $f(window).scroll(function() {
 var scrollAmount = $f(window).scrollTop();
 var documentHeight = $f(document).height();
 var scrollPercent = (scrollAmount / documentHeight) * 100;

 //$(".3amal").append(scrollPercent);
  if(scrollAmount > documentHeight*0.3 && !PassedValue){
alert('the user scrolled once!');
PassedValue=true;
  }else{
  //alert('Nothing done nothing to save');
  }
});
});