jQuery:如何在div进入视图时触发事件?

这个问题在这里已经有了答案:

  • 滚动38个答案后,检查元素是否可见

  • 我想你必须勾选滚动事件并手动检查。 这是一个类似的问题:

    滚动后检查元素是否可见

    希望有所帮助。

    短发


    航点插件涵盖了这个和更多。 http://imakewebthings.com/jquery-waypoints/

    文档:http://imakewebthings.com/jquery-waypoints/#docs

    例如:

    $('.someSelector').waypoint(function(direction){
    //do stuff 
    },{
     //bottom-in-view will ensure event is thrown when the element's bottom crosses
     //bottom of viewport.
     offset: 'bottom-in-view'
    });
    

    如果你只需要射击一次:

    $(window).scroll(function(){
      // This is then function used to detect if the element is scrolled into view
      function elementScrolled(elem)
      {
        var docViewTop = $(window).scrollTop();
        var docViewBottom = docViewTop + $(window).height();
        var elemTop = $(elem).offset().top;
        return ((elemTop <= docViewBottom) && (elemTop >= docViewTop));
      }
    
      // This is where we use the function to detect if ".box2" is scrolled into view, and when it is add the class ".animated" to the <p> child element
      if(elementScrolled('. box2')) {
    
    
      // Your function here
    
      }
    });
    

    完整答案:https://www.thewebtaylor.com/articles/how-to-detect-if-an-element-is-scrolled-into-view-using-jquery

    链接地址: http://www.djcxy.com/p/83333.html

    上一篇: jQuery: How can I trigger an event when a div comes into view?

    下一篇: Jquery check if element is visible in viewport