Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Use jQuery to Detect Container Overflow?

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?

like image 946
Jason Avatar asked Jan 21 '10 19:01

Jason


People also ask

How to check if content overflows its parent container using jQuery?

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.

What is jQuery overflowing?

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).

How to determine whether content is overflow or not using JavaScript?

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.

How to detect elements overflow using jQuery - Stack Overflow?

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


3 Answers

$.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');
}
like image 109
Luka Zakrajšek Avatar answered Sep 19 '22 05:09

Luka Zakrajšek


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;
};
like image 23
user1058982 Avatar answered Sep 19 '22 05:09

user1058982


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.

like image 20
Matchu Avatar answered Sep 21 '22 05:09

Matchu