Triggering jquery event when an element appears on screen

前端 未结 7 1672
难免孤独
难免孤独 2020-12-05 02:41

I want to show a fade effect as soon as the element appears on screen. There is a lot of content before this element so if I trigger the efect on document.ready, at certain

7条回答
  •  伪装坚强ぢ
    2020-12-05 03:04

    If you want a simple and working sollution:

    function elementInView(elem){
      return $(window).scrollTop() < $(elem).offset().top + $(elem).height() ;
    };
    
    $(window).scroll(function(){
      if (elementInView($('#your-element')))
      //fire at will!
        console.log('there it is, wooooohooooo!');
    });
    

提交回复
热议问题