Created
June 4, 2014 20:31
-
-
Save justinobney/90ab961295ca103ec5c2 to your computer and use it in GitHub Desktop.
jQuery plugin to find overflowing direct children of an element.
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
jQuery.fn.withOverflowingChildren = function (cb) { | |
var parent = jQuery(this); | |
var p = parent.get(0); | |
var children = parent.find('> *'); | |
var len = children.length | |
var parentCoords = p.getBoundingClientRect(); | |
var called = false | |
while(len){ | |
if(!isInParent(children.get(len -1))){ | |
cb && cb.call(children.get(len -1)) | |
called = true; | |
} | |
len--; | |
} | |
return called; | |
function isInParent(el){ | |
var childCoords = el.getBoundingClientRect() | |
return ( | |
parentCoords.top <= childCoords.top && | |
parentCoords.right >= childCoords.right && | |
parentCoords.bottom >= childCoords.bottom && | |
parentCoords.left <= childCoords.left | |
) | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment