Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Detecting horizontal div overflow with JavaScript?

I have a DIV that has plenty of child DIVs inside of it. What I want is similar to Firefox's tabs, when you have too many tabs open or the main DIV width is too small, the interface will detect overflow and show a button on the right side to list all hidden tabs. The problem is that I have no idea where to even start looking for help.

like image 848
Tower Avatar asked Dec 23 '22 07:12

Tower


2 Answers

Is you main DIV set to overflow:hidden?

If so, you can test its need to overflow by incrementing the scrollLeft property and then querying it to see if it's changed:

function containsTooMuch(el) {
    var original = el.scrollLeft++;
    return el.scrollLeft-- > original;
}
like image 60
James Avatar answered Jan 06 '23 05:01

James


Googling turns up this:

http://knitinr.blogspot.com/2008/08/javascript-warn-if-overflow.html

looks nice and framework independent.

But maybe somebody comes up with a solution that works with less code.

Oh and guess which popular coding community site screws up the Googe results for

javascript detect overflow 

:)

like image 29
Pekka Avatar answered Jan 06 '23 06:01

Pekka