Skip to content

Instantly share code, notes, and snippets.

@lidlanca
Last active August 29, 2015 14:07
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save lidlanca/faf526d95f8ba67d54cc to your computer and use it in GitHub Desktop.
Save lidlanca/faf526d95f8ba67d54cc to your computer and use it in GitHub Desktop.
element overflow detection
//Lidlanca 10/03/2014
function identify_overflow_elements($main_container,remove){
var main_container = $main_container;
var potential_overflow_elements = [];
main_container.each (function (a,b){
var max_w = $(b).width()+ ($(b).offset().left) ;
var min_w = ($(b).offset().left);
console.log ("Container max_w, min_w" , max_w,min_w);
$(b).find("*:not(script)").each( function (c,d){ //rough element query
var el_left = $(d).offset().left;
var el_right = $(d).width() + el_left;
if ( el_right > max_w || el_left < min_w ){
console.warn("Out of container bounds: ",d , el_right , el_left );
potential_overflow_elements.push ($(d));
$(d).css("background-color","rgba(255, 250, 83,0.9)");
}
});
});
//generate a visual indicator next to the offending element
$(potential_overflow_elements).each (function (e,f){
var el = $("<div>").css({
"position":"absolute",
"z-index":"100",
'border':'1px solid gray',
"display":"block",
"left": $(f).offset().left + "px",
"top": $(f).offset().top-20 + "px",
"background":"red",
"color":"#fff",
'font-size':'12px'
});
el.html("&#9660;");
$("body").append(el);
});
//run with the remove flag on to remove all offending elements
if (remove){
$(potential_overflow_elements).each (function (a,b){
b.remove();
});
}
}
//container provided is used as a reference to boundary check for all of its children
identify_overflow_elements($("div.container.posts"),false);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment