I've seen this question but feel like there has to be a "cleaner" jQuery method of doing this. I'm not even sure if this really works in all scenarios. Is there a way for jQuery to determine if a container has overflow without comparing dimensions?
For clarification, is there a method to test whether the CSS attribute overflow: hidden
has kicked in and is hiding content?
overflowing is a lightweight yet useful jQuery plugin that checks if an element's content overflows its parent container and executes a callback function when the content is overflowed. 1. Download and place the JavaScript file jquery.overflowing.js after jQuery library (slim build). 2.
More in this category... overflowing is a lightweight yet useful jQuery plugin that checks if an element's content overflows its parent container and executes a callback function when the content is overflowed. 1. Download and place the JavaScript file jquery.overflowing.js after jQuery library (slim build).
Given an HTML element and the task is to determine whether its content is overflow or not using JavaScript. Select the element to check form overflow. Check its style.overflow property, if it is ‘visible’ then the element is hidden.
detect elements overflow using jquery - Stack Overflow CSS: .blue { width:200px; height:200px; background-color:blue; color:#000000; overflow:auto; } JavaScript: function addChar() { $('.blue').append('some text... Stack Overflow About Products For Teams
$.fn.hasOverflow = function() {
var $this = $(this);
var $children = $this.find('*');
var len = $children.length;
if (len) {
var maxWidth = 0;
var maxHeight = 0
$children.map(function(){
maxWidth = Math.max(maxWidth, $(this).outerWidth(true));
maxHeight = Math.max(maxHeight, $(this).outerHeight(true));
});
return maxWidth > $this.width() || maxHeight > $this.height();
}
return false;
};
Example:
var $content = $('#content').children().wrapAll('<div>');
while($content.hasOverflow()){
var size = parseFloat($content.css('font-size'), 10);
size -= 1;
$content.css('font-size', size + 'px');
}
You may change the css attributes to fit your needs.
$.fn.hasOverflow = function() {
$(this).css({ overflow: "auto", display: "table" });
var h1 = $(this).outerHeight();
$(this).css({ overflow: "hidden", display: "block" });
var h2 = $(this).outerHeight();
return (h1 > h2) ? true : false;
};
There is no clean method. You could make it two wrappers, the outer wrapper having overflow: hidden
, and comparing the two wrappers' dimensions, but anything you could possibly do would end up being hacky. If the functionality really is necessary, then you'll have to live with the hacks.
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