简体   繁体   English

滚动调用函数仅一次

[英]Call function on scroll only once

I got a question regarding a function that will be called if the object is within my screen.我有一个关于如果对象在我的屏幕内将被调用的函数的问题。 But when the object is within my screen the function is been called and a alert is been fired.但是当对象在我的屏幕内时,该函数被调用并发出警报。 But if I close the alert and scroll further down the event is called again.但是,如果我关闭警报并进一步向下滚动,则会再次调用该事件。 I do not want that.我不要那个。 How can I solve that?我该如何解决?

Working example工作示例

My code so far:到目前为止我的代码:

<div id="wrapper">
    scroll down to see the div
</div>
<div id="tester"></div>

JS JS

$(window).on('scroll',function() {
    if (checkVisible($('#tester'))) {
        alert("Visible!!!")        
    } else {
        // do nothing 
    }
});

function checkVisible( elm, eval ) {
    eval = eval || "object visible";
    var viewportHeight = $(window).height(), // Viewport Height
        scrolltop = $(window).scrollTop(), // Scroll Top
        y = $(elm).offset().top,
        elementHeight = $(elm).height();   

    if (eval == "object visible") return ((y < (viewportHeight + scrolltop)) && (y > (scrolltop - elementHeight)));
    if (eval == "above") return ((y < (viewportHeight + scrolltop)));
}

What I want is that the If function only will be called 1 time and not on every scroll if the object is visible.我想要的是 If 函数只会被调用 1 次,如果对象可见,则不会在每次滚动时调用。

Try using .one() :尝试使用.one()

$(window).one('scroll',function() {
   // Stuff
});

Or, unlink the event inside:或者,取消链接里面的事件:

$(window).on('scroll',function() {
   // After Stuff
   $(window).off('scroll');
});

Guess you might need this code :猜你可能需要这个代码

$(window).on('scroll',function() {
    if (checkVisible($('#tester'))) {
        alert("Visible!!!");
        $(window).off('scroll');
    } else {
        // do nothing
    }
});

Fiddle: http://jsfiddle.net/c68nz3q6/小提琴: http : //jsfiddle.net/c68nz3q6/

Let's try to solve your problem in javascript as all of the answers here are in jquery.让我们尝试在 javascript 中解决您的问题,因为这里的所有答案都在 jquery 中。 You can use global variable as browser retains its value as long as the page is not refreshed.您可以使用全局变量,因为只要页面不刷新,浏览器就会保留其值。 All those variables declared outside the function are called global variables and can be accessed by any function.所有在函数外声明的变量都称为全局变量,可以被任何函数访问。

 window.onscroll = myScroll; var counter = 0; // Global Variable function myScroll(){ var val = document.getElementById("value"); val.innerHTML = 'pageYOffset = ' + window.pageYOffset; if(counter == 0){ // if counter is 1, it will not execute if(window.pageYOffset > 300){ alert('You have scrolled to second div'); counter++; // increment the counter by 1, new value = 1 } } }
 #wrapper,#tester { width: 300px; height: 300px; border: 1px solid black; padding: 10px; } #wrapper p { text-align: center; } #tester { border: 1px solid crimson; } #value { position: fixed; left: auto; right: 40px; top: 10px; }
 <p id = "value"></p> <div id="wrapper"> <p>scroll down to div to see the alert</p> </div> <div id="tester"></div>

$(window).on('scroll', function() {
  if ($(document).scrollTop() >= $(document).height() / 100) {
    $("#spopup").show("slow");
    $(window).off('scroll');
  } else {
    $("#spopup").hide("slow");
  }
});

function closeSPopup() {
  $('#spopup').hide('slow');
}

worked for me为我工作

Caution: All answers here are using $(window).off('scroll');注意:这里的所有答案都使用$(window).off('scroll'); which could effect your other scripts parts, like in my case my sticky nav stopped working.这可能会影响您的其他脚本部分,就像在我的情况下,我的粘性导航停止工作。 So I used a simple flag var to solve my issue.所以我使用了一个简单的标志 var 来解决我的问题。

var scrolled = false;
$(window).on('scroll', function() {
  if (!scrolled) {
    if (checkVisible($('#tester'))) {
      alert("Visible!!!");
      $(window).off('scroll');
    } else {
      // do nothing
    }
    scrolled = true;
  }
});

when the $test is within in you screen, remove the event listener.当 $test 在您的屏幕中时,删除事件侦听器。

$(window).on('scroll',function() {
    if (checkVisible($('#tester'))) {
       alert("Visible!!!")   
       $(window).off('scroll');
    } else {
    // do nothing
    }
});

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

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