仅在滚动时触发一次函数

时间:2018-05-09 14:27:19

标签: javascript jquery

所以,我想在滚动时只触发一次函数。

问题是我不能只触发一次该功能。我尝试过不同的解决方案(.on(),设置一个计数器,在window.scrollstop函数外部/内部设置它)但没有任何效果。

它确实有效...但每当我滚动它都有效! :(

我不认为这很困难,但是......到目前为止,我还没有让它发挥作用。

这是我的代码:

$(window).scroll(function(){
    $('.sr-icons').toggleClass('scrolled', $(this).scrollTop() > 950);  
});

CSS:

.sr-icons.scrolled {
  opacity: 0.2;
}

和HTML:

<div class="container">
        <div class="row">
          <div class="col-lg-3 col-md-6 text-center">
            <div class="mt-5 mx-auto">
              <i class="fa fa-4x fa-superpowers text-primary mb-3 sr-icons"></i>
              <h3 class="mb-3 text-dark">Sturdy Templates</h3>
              <p class="text-muted mb-0">Our templates are updated regularly so they don't break.</p>
            </div>
          </div>
          <div class="col-lg-3 col-md-6 text-center">
            <div class="mt-5 mx-auto">
              <i class="fa fa-4x fa-paper-plane text-primary mb-3 sr-icons"></i>
              <h3 class="mb-3 text-dark">Ready to Ship</h3>
              <p class="text-muted mb-0">You can use this theme as is, or you can make changes!</p>
            </div>
          </div>
          <div class="col-lg-3 col-md-6 text-center">
            <div class="mt-5 mx-auto">
              <i class="fa fa-4x fa-newspaper-o text-primary mb-3 sr-icons"></i>
              <h3 class="mb-3 text-dark">Up to Date</h3>
              <p class="text-muted mb-0">We update dependencies to keep things fresh.</p>
            </div>
          </div>
          <div class="col-lg-3 col-md-6 text-center">
            <div class="mt-5 mx-auto">
              <i class="fa fa-4x fa-cog fa-spin text-primary mb-3 sr-icons"></i>
              <h3 class="mb-3 text-dark">I Work For You</h3>
              <p class="text-muted mb-0">Just think about any website you want to have, I will make that a reality.</p>
            </div>
          </div>
        </div>
      </div>

2 个答案:

答案 0 :(得分:1)

如果我理解你,你可以这样做:

telephone_number NOT IN 
         (SELECT telephone_number 
          FROM tps_database WHERE telephone_number = l.telephone_number) 
           AND telephone_number NOT IN 
             (SELECT telephone_number 
                FROM dialler_dnc 
               WHERE telephone_number = l.telephone_number) 
                AND l.id NOT IN 
                     (SELECT number_id 
                        FROM dialler_callback 
                       WHERE number_id = l.id)  
                         AND (SELECT COUNT(*) 
                                FROM dialler_history 
                               WHERE telephone_number = l.telephone_number 
                                 AND (last_called >= ' . $start . ' 
                                 AND last_called <= ' . $end . ')) <= 2 
                                 AND (SELECT COUNT(*) 
                                        FROM dialler_history 
                                       WHERE telephone_number = l.telephone_number) < 14

如其他评论所述,您应该检查您想要的$(window).on('scroll', function(){ if($(this).scrollTop() > 950) { $('.sr-icons').toggleClass('scrolled', true); $(window).off('scroll'); } }); $(this).scrollTop() > 950

编辑:更合适的解决方案是仅删除该特定处理程序,以便您可以在滚动时附加其他事件:

$(this).scrollTop() < 950

答案 1 :(得分:0)

您可以使用标志变量,最初为0,然后在滚动时递增。 然后您可以添加检查

var flag=0;
$(window).scroll(function(){  
if(flag==0){
$('.sr-icons').toggleClass('scrolled', $(this).scrollTop() > 950); 
flag++;
}
});