确定最大可能DIV高度 [英] Determine Maximum Possible DIV Height

查看:152
本文介绍了确定最大可能DIV高度的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

有没有建议的方法来确定DIV可以设置的最大高度,并且每个浏览器保持可见?这似乎并没有被记录在任何地方,并且具有高度的实现特性。

Is there a recommended way to determine the maximum height that a DIV can be set and remain visible per browser? This doesn't appear to be documented anywhere and is highly implementation specific.

例如,请参阅以下测试脚本:

For example, see the following test script:

http://jsfiddle.net/NP5Pa/2/

这是一个简单的测试,以找到最大值,您可以在元素的相应 clientHeight 之前设置DIV样式高度属性为0。您可以通过点击查找最大值,然后将找到的高度增加1并点击设置高度来确认。

This is a simple test to find the maximum value you can set a DIV style height attribute before the corresponding clientHeight of the element becomes 0. You can confirm this by clicking "Find Max" then incrementing the found height by 1 and clicking "Set Height".

一些示例(Win7 / 64):

Some examples (Win7/64):

Chrome (14.0) :    134,217,726 px
Safari (5.1)  :    134,217,726 px
IE (9.0)      :     10,737,418 px
FF (7.0.1)    :     17,895,697 px

WebKit生成相同的并不奇怪结果,我猜 - 更奇怪的是,IE和FF是如此不同。

It's not surprising the WebKit produces the same result, I guess - more surprising that IE and FF are so different.

有更好的方法吗?你在32位系统中得到不同的结果?

Is there a better way? And do you get different results in 32bit systems?

- 编辑:更新了手机,以停止10,000,000,000(并更快地到达Opera)。这是很多像素。

推荐答案

这是你的代码,修改为使用二进制搜索(所以它更快)。

This is your code, modified to use binary search (so it's much quicker).

http://jsfiddle.net/thai/zkuGv/ 4 /

从1像素开始,将其大小加倍,直到它达到最大值(我使用2 53 ,其中是可以存储在JavaScript中的最大的整数,而不会失去精度,并且会使二进制搜索错误),或者将div折叠到零像素。

It begins at 1 pixel and doubling its size until the it hits the maximum (I use 253, which is the biggest integer that can be stored in JavaScript without losing precision and would make the binary search buggy), or the div collapses to zero pixel.

假设我们设置div要大小 h ,它消失,那么最大大小必须在 h / 2和 h 之间。我们从那里二进制搜索一个高度 h ,当设置为高度 h 时,不会使div消失,但设置为 h 时消失1。

Suppose we set the div to size h and it disappears, then the maximum size must be between h/2 and h. We binary search from there for a height h that does not make the div disappear when set to height h, but disappears when set to h+1.

然后我们可以得出结论:Opera:2147483583像素。

Then we can come to a conclusion for Opera: 2147483583 pixels.

这篇关于确定最大可能DIV高度的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆