Call function on scroll only once

前端 未结 4 1841
悲&欢浪女
悲&欢浪女 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:08

    when the $test is within in you screen, remove the event listener.

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

提交回复
热议问题