【问题标题】:Run if statement only one time inside scroll event在滚动事件中只运行一次 if 语句
【发布时间】:2020-11-07 13:47:06
【问题描述】:

我有这段代码,我尝试只执行一次 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>

【问题讨论】:

  • 在检查之前,您在每个滚动事件上将 PassedValue 设置为 false。将其声明移出事件处理程序!
  • 感谢 FZ 的帮助,这对我来说很完美

标签: javascript jquery if-statement boolean alert


【解决方案1】:

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

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

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

【讨论】:

  • 我认为这就是为什么 OP 有 PassedValue 变量...
  • 谢谢 Sergey 真的很感激,我刚刚将 PassedValue 从事件处理程序中移出,正如 FZ 和 Thamidu de Harshitha 所说,解决了我的问题
【解决方案2】:

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

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

【讨论】:

  • 感谢 Thamidu de Harshitha 的宝贵时间和完美的建议
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2015-08-23
  • 1970-01-01
  • 2015-01-18
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多