Last active
December 12, 2017 17:21
-
-
Save pedrohugorm/6e204afac417874b88060806d49a1816 to your computer and use it in GitHub Desktop.
Infinite Scroll Performance Problem
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
function isElementInViewport (rect) { | |
return ( | |
rect.top >= 0 && | |
rect.left >= 0 && | |
rect.bottom <= (window.innerHeight || document.documentElement.clientHeight) && /*or $(window).height() */ | |
rect.right <= (window.innerWidth || document.documentElement.clientWidth) /*or $(window).width() */ | |
) | |
} | |
function hideIfNotVisible(o){ | |
if(o.visible === false) $(o.el).children().remove(); | |
return o | |
} | |
function createFakeSpace(o){ | |
$(o.el).css('height', o.rect.height) | |
return o | |
} | |
$('.lead-table .lead-row') | |
.map((i, el) => {el, rect: el.getBoundingClientRect()}) | |
.map((i, o) => { | |
o.visible = isElementInViewport(o.rect) | |
return o | |
}) | |
.map((i, o) => { | |
hideIfNotVisible(o) | |
createFakeSpace(o) | |
return o | |
}) | |
; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment