简体   繁体   English

确定最大可能的 DIV 高度

[英]Determine Maximum Possible DIV Height

Is there a recommended way to determine the maximum height that a DIV can be set and remain visible per browser?是否有推荐的方法来确定 DIV 可以设置的最大高度并在每个浏览器中保持可见? 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/ http://jsfiddle.net/NP5Pa/2/

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".这是一个简单的测试,用于在元素的相应clientHeight变为 0 之前找到您可以设置 DIV 样式高度属性的最大值。您可以通过单击“查找最大值”然后将找到的高度增加 1 并单击“设置高度”来确认这一点”。

Some examples (Win7/64):一些例子(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

It's not surprising the WebKit produces the same result, I guess - more surprising that IE and FF are so different. WebKit 产生相同的结果并不奇怪,我想 - 更令人惊讶的是 IE 和 FF 如此不同。

Is there a better way?有没有更好的办法? And do you get different results in 32bit systems?你在 32 位系统中得到不同的结果吗?

--EDIT: Updated the fiddle to stop at 10,000,000,000 (and get there quicker) for Opera. --编辑:更新了 Opera 的小提琴,使其停在 10,000,000,000(并更快地到达那里)。 That's a lot of pixels.这是很多像素。

This is your code, modified to use binary search (so it's much quicker).这是您的代码,已修改为使用二进制搜索(因此速度更快)。

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

It begins at 1 pixel and doubling its size until the it hits the maximum (I use 2 53 , 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.它从 1 个像素开始,然后将其大小加倍,直到达到最大值(我使用 2 53 ,这是可以存储在 JavaScript 中而不会丢失精度的最大整数,并且会使二进制搜索出错),或者 div 折叠为零像素。

Suppose we set the div to size h and it disappears, then the maximum size must be between h /2 and h .假设我们将 div 设置为大小h并且它消失了,那么最大大小必须介于h /2 和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.我们从那里二分搜索高度h ,当设置为高度h时不会使 div 消失,但当设置为h +1 时消失。

Then we can come to a conclusion for Opera: 2147483583 pixels.那么我们可以得出Opera的结论:2147483583像素。

It should be noted, while you can make elements very tall, they won't be usable after a certain height, depending on what you're doing.应该注意的是,虽然您可以将元素设置得非常高,但在达到一定高度后它们将无法使用,具体取决于您在做什么。

Modifying Thai jsfiddle to have text at the bottom:修改Thai jsfiddle 以在底部显示文本:

<div id="inner" style="position: relative;">
  <div style="position:absolute; bottom:0;">
    === BOTTOM ===
  </div>
</div>

http://jsfiddle.net/8kjstu4x/ http://jsfiddle.net/8kjstu4x/

Now:现在:

  • Make the height 30000000 (30 million), scroll down, text is still there.将高度设为30000000(3000万),向下滚动,文字还在。
  • Make the height 40000000 (40 million), scroll down, text is gone.将高度设为 40000000(4000 万),向下滚动,文字不见了。

I've also noticed, that after a height of around 16 million pixels, things start going wrong.我还注意到,在大约 1600 万像素的高度之后,事情开始出错。 Missing borders, alignment issues, etc.缺少边框、对齐问题等。

Here try this: http://jsfiddle.net/serkanyersen/yNfuX/在这里试试这个: http : //jsfiddle.net/serkanyersen/yNfuX/

It will respect the parent's padding and margin properties.它将尊重父级的填充和边距属性。 When you click MAX button, DIV will expand to most available space.当您单击 MAX 按钮时,DIV 将扩展到大部分可用空间。 I'm sure you can convert this according to your needs.我相信您可以根据自己的需要进行转换。

It actually needs to be recursive, starting from itself to the very top, but I didn't have time to implement that.它实际上需要递归,从自身开始到最顶端,但我没有时间实现它。

NOTE: Try resizing the page, and uncomment the parent DIV to see it fully work.注意:尝试调整页面大小,并取消对父 DIV 的注释以查看它是否完全正常工作。

让高度为<p>确定高度</p><div></div><div id="text_translate"><p>我在&lt;div&gt;中有一段文本,它随着长度和高度而变化。 我希望 div 的最小高度为100px ,但随着&lt;p&gt;的高度增加到80px以上而增长。 我为&lt;div&gt;设置了 css 属性height: 100px但是当&lt;p&gt;超过80px时,文本刚刚溢出&lt;div&gt;并且&lt;div&gt; div&gt; 的高度保持不变。 我应该怎么办?</p><p> <strong>代码:</strong></p><pre> &lt;div id="outer_box"&gt; &lt;div id="box"&gt; &lt;p class="content"&gt; Some long paragraph of content here &lt;/p&gt; &lt;/div&gt; &lt;/div&gt;</pre><p> <strong>CSS:</strong></p><pre> #outer_box { width: 300px; min-height: 100px; } #box{ width: 300px; min-height: 90px; }</pre><p> <em><strong>id="box"的 div 确实改变了它的高度以完全包含&lt;p&gt; ,但是id="outer_box"的 div 没有改变它的高度!</strong></em></p><p> <em><strong>如果可能的话,我对 CSS 解决方案而不是 jQuery 解决方案感兴趣,我将使用任何可行的方法。</strong></em> <em><strong>选择纯 CSS 解决方案的原因是因为div在用户进行鼠标悬停之前不存在。</strong></em> <em><strong>想知道如何使用尚未存在的 css 定位div</strong></em></p></div> - Let the height of <p> determine height of <div>

暂无
暂无

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

相关问题 让高度为<p>确定高度</p><div></div><div id="text_translate"><p>我在&lt;div&gt;中有一段文本,它随着长度和高度而变化。 我希望 div 的最小高度为100px ,但随着&lt;p&gt;的高度增加到80px以上而增长。 我为&lt;div&gt;设置了 css 属性height: 100px但是当&lt;p&gt;超过80px时,文本刚刚溢出&lt;div&gt;并且&lt;div&gt; div&gt; 的高度保持不变。 我应该怎么办?</p><p> <strong>代码:</strong></p><pre> &lt;div id="outer_box"&gt; &lt;div id="box"&gt; &lt;p class="content"&gt; Some long paragraph of content here &lt;/p&gt; &lt;/div&gt; &lt;/div&gt;</pre><p> <strong>CSS:</strong></p><pre> #outer_box { width: 300px; min-height: 100px; } #box{ width: 300px; min-height: 90px; }</pre><p> <em><strong>id="box"的 div 确实改变了它的高度以完全包含&lt;p&gt; ,但是id="outer_box"的 div 没有改变它的高度!</strong></em></p><p> <em><strong>如果可能的话,我对 CSS 解决方案而不是 jQuery 解决方案感兴趣,我将使用任何可行的方法。</strong></em> <em><strong>选择纯 CSS 解决方案的原因是因为div在用户进行鼠标悬停之前不存在。</strong></em> <em><strong>想知道如何使用尚未存在的 css 定位div</strong></em></p></div> - Let the height of <p> determine height of <div> 是否可以确定IFrame中视频的高度? - Is it possible to determine the height of a video in an IFrame? 计算DIV元素的最大/最小高度 - Calculating the maximum/minimum height of a DIV element div中的可滚动表格,div的最大高度有限? - Scrollable table inside div with div having limited maximum height? 如何获得div的最大可能宽度? - How to get the maximum possible width of a div? 是否可以在div内获取当前的高度偏移量? - Is it possible to get the current height offset inside the div? 如何确定尚未显示在页面上的div的高度 - How do I determine the height of a div that has yet to be displayed on the page jquery - 如何确定一个 div 是否改变了它的高度或任何 css 属性? - jquery - How to determine if a div changes its height or any css attribute? 如何确定DIV的长度和高度相对于其中的元素数量? - How to determine the length and height of a DIV relative to the amount of elements in it? 使用jQuery确定height:auto并将其应用于另一个div - using jquery to determine height:auto and applying it to another div
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM