Created
January 13, 2015 19:52
-
-
Save anonymous/f09b5cd487b5ad55c46e to your computer and use it in GitHub Desktop.
Enhanced isScrolledIntoView
This file contains hidden or 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
$.fn.isInViewport = function(containerSelector, partial) { | |
var $element = this; | |
if($element.length) { | |
var el = $element[0]; | |
var rect = el.getBoundingClientRect(); | |
if(containerSelector && $(containerSelector).length) { | |
var parentRect = $(containerSelector)[0].getBoundingClientRect(); | |
} else { | |
var parentRect = {top: 0, left: 0, bottom: $(window).height(), right: $(window).width()}; | |
} | |
if(partial) { | |
return ( | |
rect.left >= parentRect.left && | |
rect.right <= parentRect.right && | |
( | |
(rect.top >= parentRect.top && rect.top <= parentRect.bottom) || | |
(rect.bottom >= parentRect.top && rect.bottom <= parentRect.bottom) | |
) | |
); | |
} else { | |
return ( | |
rect.top >= parentRect.top && | |
rect.left >= parentRect.left && | |
rect.bottom <= parentRect.bottom && | |
rect.right <= parentRect.right | |
); | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment