简体   繁体   English

如果文本在文档加载时换行,则 DIV 的高度计算不正确

[英]Height of DIV not calculated correctly if text insides wraps on document load

I have a div.我有一个 div。

<div id="test">this is a lot of text and more and more and more and more and more and more and more and more text</div>

On document.ready I am getting the height of the div.在 document.ready 上,我得到了 div 的高度。

$(document).ready(function() {
  var intDivHeight = $("#test").outerHeight(true);
});

If I expand my browser window to be very wide so the text does not wrap and reload the page the height is 32. If I shrink my browser window to be skinny so that the text wraps on two or three lines, the height is still 32.如果我将我的浏览器窗口扩展到非常宽,因此文本不会换行并重新加载页面,则高度为 32。如果我将浏览器窗口缩小到很窄,以便文本在两行或三行上换行,则高度仍为 32 .

Odd.奇怪的。

If I put the above code in a window.resize function, I get the wrong size on initial page load, but I get the correct size as soon as I start resizing the window.如果我将上面的代码放在 window.resize 函数中,我会在初始页面加载时得到错误的大小,但是一旦我开始调整窗口大小,我就会得到正确的大小。

$(window).resize(function() {
  var intDivHeight = $("#test").outerHeight(true);
});

32 changes to something like 56 or 78 on resize. 32 更改为 56 或 78 之类的调整大小。

Odd.奇怪的。

Could someone please explain to me why this happens?有人可以向我解释为什么会发生这种情况吗? And, is there a solution so that I can get the correct "wrapped" height on document.ready?并且,是否有解决方案可以让我在 document.ready 上获得正确的“包裹”高度? I am using this to try and position certain elements using JavaScript.我正在使用它来尝试使用 JavaScript 定位某些元素。

Thanks!谢谢!

You can get scrollHeight through just plain ol' javascript!您可以通过简单的 ol' javascript 获得scrollHeight Or, if you plan to have overflow content in the div, you can stick to using the jQuery outerHeight() function, instead.或者,如果您计划在 div 中包含溢出内容,您可以坚持使用 jQuery 的outerHeight()函数。

As far as reporting the numbers on page load vs resize, you'll need to bind 2 event handlers to do so.至于报告页面加载与调整大小的数字,您需要绑定 2 个事件处理程序来执行此操作。 The first one, on load , and the second on resize .第一个在load ,第二个在resize

I can make a quick example:我可以举一个简单的例子:

 $(function() { $(window).on("load resize", function() { console.log("Our height is " + $("div")[0].scrollHeight); }); });
 div { background-color: #f1f1f1; padding: 10px; }
 <script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script> <div>Look at all this text! Look at all this text! Look at all this text! Look at all this text! Look at all this text! Look at all this text! Look at all this text! Look at all this text! Look at all this text! Look at all this text! Look at all this text! Look at all this text! Look at all this text! Look at all this text! Look at all this text!</div>

You can do this by binding the window load and resize events.您可以通过绑定窗口loadresize事件来做到这一点。

var intDivHeight;

$(document).ready(function() {
  $(window).on("load resize", function() {
    intDivHeight = $("#test").outerHeight(true);
  });
});

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

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