Call function on scroll only once

前端 未结 4 1827
悲&欢浪女
悲&欢浪女 2021-01-01 23:48

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

4条回答
  •  春和景丽
    2021-01-02 00:16

    Try using .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/

提交回复
热议问题