Overflow happens when there is too much content to fit in a box. CSS provides various tools to manage overflow. As you go further with CSS layout and writing CSS, you will encounter more overflow situations.
Definition and Usage. The overflow property sets or returns what to do with content that renders outside the element box. Tip: If you want to hide the scrollbars of the entire document, use the overflow property on the body or the html element.
You actually don't need any jQuery to check if there is an overflow happening or not. Using element.offsetHeight
, element.offsetWidth
, element.scrollHeight
and element.scrollWidth
you can determine if your element have content bigger than it's size:
if (element.offsetHeight < element.scrollHeight ||
element.offsetWidth < element.scrollWidth) {
// your element have overflow
} else {
// your element doesn't have overflow
}
See example in action: Fiddle
But if you want to know what element inside your element is visible or not then you need to do more calculation. There is three states for a child element in terms of visibility:
If you want to count semi-visible items it would be the script you need:
var invisibleItems = [];
for(var i=0; i<element.childElementCount; i++){
if (element.children[i].offsetTop + element.children[i].offsetHeight >
element.offsetTop + element.offsetHeight ||
element.children[i].offsetLeft + element.children[i].offsetWidth >
element.offsetLeft + element.offsetWidth ){
invisibleItems.push(element.children[i]);
}
}
And if you don't want to count semi-visible you can calculate with a little difference.
I had the same question as the OP, and none of those answers fitted my needs. I needed a simple condition, for a simple need.
Here's my answer:
if ($("#myoverflowingelement").prop('scrollWidth') > $("#myoverflowingelement").width() ) {
alert("this element is overflowing !!");
}
else {
alert("this element is not overflowing!!");
}
Also, you can change scrollWidth
by scrollHeight
if you need to test the either case.
Partially based on Mohsen's answer (the added first condition covers the case where the child is hidden before the parent):
jQuery.fn.isChildOverflowing = function (child) {
var p = jQuery(this).get(0);
var el = jQuery(child).get(0);
return (el.offsetTop < p.offsetTop || el.offsetLeft < p.offsetLeft) ||
(el.offsetTop + el.offsetHeight > p.offsetTop + p.offsetHeight || el.offsetLeft + el.offsetWidth > p.offsetLeft + p.offsetWidth);
};
Then just do:
jQuery('#parent').isChildOverflowing('#child');
One method is to check scrollTop against itself. Give the content a scroll value larger than its size and then check to see if its scrollTop is 0 or not (if it is not 0, it has overflow.)
http://jsfiddle.net/ukvBf/
So I used the overflowing jquery library: https://github.com/kevinmarx/overflowing
After installing the library, if you want to assign the class overflowing
to all overflowing elements, you simply run:
$('.targetElement').overflowing('.parentElement')
This will then give the class overflowing
, as in <div class="targetElement overflowing">
to all elements that are overflowing. You could then add this to some event handler(click, mouseover) or other function that will run the above code so that it updates dynamically.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With