检测视口

时间:2017-09-29 08:52:14

标签: javascript jquery html css

我需要检测某个div的底部边框何时触及视口底部然后触发某些功能。我试过这个

jQuery(function ($) { 

$.fn.isOnScreen = function(){

    var win = $(window);

    var viewport = {
        top : win.scrollTop(),
        left : win.scrollLeft()
    };
    viewport.right = viewport.left + win.width();
    viewport.bottom = viewport.top + win.height();

    var bounds = this.offset();
    bounds.right = bounds.left + this.outerWidth();
    bounds.bottom = bounds.top + this.outerHeight();

    return (!(viewport.right < bounds.left || viewport.left > bounds.right || viewport.bottom < bounds.top || viewport.top > bounds.bottom));

};

$(window).scroll(function() {
   if ($('.gallery full-width-container').isOnScreen() == true) {
     $('.portfolio-description-showinfo').addClass('testing');   
   }
});

});

但是此函数检测div何时进入视口,而不是当div的底部位于视口底部时。有什么提示,有帮助吗?感谢。

1 个答案:

答案 0 :(得分:1)

试试这个:

$(window).scroll(function() {
   if ($('.gallery full-width-container').isOnScreen() == true) {
      if ($(".portfolio-description-showinfo").offset().top + $(".portfolio-description-showinfo").height()) >= $(window).height() {
         $('.portfolio-description-showinfo').addClass('testing');  
      } 
   }
});