From 6739c439582eb80018fce0528202f65f9525dc7e Mon Sep 17 00:00:00 2001 From: Tomasz Rondio Date: Tue, 28 Apr 2015 13:20:49 +0200 Subject: [PATCH 1/2] jquery 2.x support + checkInView fixes --- jquery.inview.js | 28 +++++++++++++--------------- 1 file changed, 13 insertions(+), 15 deletions(-) diff --git a/jquery.inview.js b/jquery.inview.js index 9e630e0..b337849 100644 --- a/jquery.inview.js +++ b/jquery.inview.js @@ -43,38 +43,36 @@ function checkInView() { var viewportTop = getScrollTop(), - viewportBottom = viewportTop + getViewportHeight(), - elems = []; + viewportBottom = viewportTop + getViewportHeight(); + + - // naughty, but this is how it knows which elements to check for - $.each($.cache, function() { - if (this.events && this.events.inview) { - elems.push(this.handle.elem); - } - }); - $(elems).each(function() { + $('.inview').each(function() { var $el = $(this), elTop = offsetTop(this), elHeight = $el.height(), elBottom = elTop + elHeight, wasInView = $el.data('inview') || false, offset = $el.data('offset') || 0, - inView = elTop > viewportTop && elBottom < viewportBottom, - isBottomVisible = elBottom + offset > viewportTop && elTop < viewportTop, - isTopVisible = elTop - offset < viewportBottom && elBottom > viewportBottom, + inView = elTop >= viewportTop && elBottom <= viewportBottom, + isBottomVisible = elBottom + offset >= viewportTop && elTop <= viewportTop, + isTopVisible = elTop - offset <= viewportBottom && elBottom >= viewportBottom, inViewWithOffset = inView || isBottomVisible || isTopVisible || - (elTop < viewportTop && elBottom > viewportBottom); + (elTop <= viewportTop && elBottom >= viewportBottom); + if (inViewWithOffset) { var visPart = (isTopVisible) ? 'top' : (isBottomVisible) ? 'bottom' : 'both'; if (!wasInView || wasInView !== visPart) { $el.data('inview', visPart); $el.trigger('inview', [true, visPart]); + } } else if (!inView && wasInView) { $el.data('inview', false); $el.trigger('inview', [false]); + } }); } @@ -89,7 +87,7 @@ return; } shouldRun = false; - fn(); + window.requestAnimationFrame(fn); timer = setTimeout(function() { timer = null; if (shouldRun) { @@ -106,4 +104,4 @@ var runner = createFunctionLimitedToOneExecutionPerDelay(checkInView, 100); $(window).on('checkInView.inview click.inview ready.inview scroll.inview resize.inview', runner); -})(jQuery); +})(jQuery); \ No newline at end of file From 068e9a729603b295125615cacf56b75b8020617d Mon Sep 17 00:00:00 2001 From: Tomasz Rondio Date: Tue, 28 Apr 2015 13:26:11 +0200 Subject: [PATCH 2/2] requestAnimationFrame removed --- jquery.inview.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/jquery.inview.js b/jquery.inview.js index b337849..7ab2dda 100644 --- a/jquery.inview.js +++ b/jquery.inview.js @@ -87,7 +87,7 @@ return; } shouldRun = false; - window.requestAnimationFrame(fn); + fn(); timer = setTimeout(function() { timer = null; if (shouldRun) {