简体   繁体   English

用JavaScript检测水平div溢出?

[英]Detecting horizontal div overflow with JavaScript?

I have a DIV that has plenty of child DIVs inside of it. 我有一个DIV,里面有很多儿童DIV。 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. 我想要的是类似于Firefox的标签,当你打开太多标签或主DIV宽度太小时,界面将检测溢出并在右侧显示一个按钮以列出所有隐藏标签。 The problem is that I have no idea where to even start looking for help. 问题是我不知道从哪里开始寻求帮助。

Is you main DIV set to overflow:hidden ? 你主DIV设置为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: 如果是这样,您可以通过递增scrollLeft属性然后查询它以查看它是否已更改来测试其溢出需求:

function containsTooMuch(el) {
    var original = el.scrollLeft++;
    return el.scrollLeft-- > original;
}

Googling turns up this: 谷歌搜索出现了这个:

http://knitinr.blogspot.com/2008/08/javascript-warn-if-overflow.html 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 哦,猜猜哪个流行的编码社区网站搞砸了Googe的结果

javascript detect overflow 

:) :)

My approach would be to work from how new DIVs get added. 我的方法是从新DIV的添加方式开始。 Whatever event causes this to happen, I would add a handler to the document that runs a script which checks the size of the various DIVs to ensure that they meet your requirements. 无论什么事件导致这种情况发生,我都会在运行脚本的文档中添加一个处理程序,该脚本检查各种DIV的大小以确保它们满足您的要求。 If they are too large (or too many), then you hide some of them and add your button with it's display logic. 如果它们太大(或太多),那么你隐藏它们中的一些并用它的显示逻辑添加你的按钮。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM