Running function on window scroll once in jQuery

前端 未结 1 595
爱一瞬间的悲伤
爱一瞬间的悲伤 2021-01-27 16:44

I\'m creating my portfolio and I\'m trying to make my skill bars load when I go to \"My skills\" section. I want them to do it only once, either when someone scroll to this sect

相关标签:
1条回答
  • 2021-01-27 17:23

    You can set a namespace at .on() for resize, scroll events, use .off() within if statement to remove namespaced events.

    var skills = $('#mySkills');
    var skillsPositionTop = skills.position().top;
    $(window).on("resize.once scroll.once", function (){
      if (pageYOffset<skillsPositionTop-20 && pageYOffset>skillsPositionTop-80) {
        $(this).off("resize.once").off("scroll.once");
        console.log ("here is my loading script");
      }
    });
    
    0 讨论(0)
提交回复
热议问题