Skip to content

Instantly share code, notes, and snippets.

@justinobney
Created June 4, 2014 20:31
Show Gist options
  • Save justinobney/90ab961295ca103ec5c2 to your computer and use it in GitHub Desktop.
Save justinobney/90ab961295ca103ec5c2 to your computer and use it in GitHub Desktop.
jQuery plugin to find overflowing direct children of an element.
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