简体   繁体   English

使用jQuery计算高度在Firefox和Chrome中有所不同

[英]Calculating height using jQuery differs in Firefox and Chrome

I have a question that was already asked here , but the solution offered there did not work. 在这里已经提出了一个问题,但是那里提供的解决方案没有用。 The problem is that I'm using the jQuery height() function to return the height of a div. 问题是我使用jQuery height()函数返回div的高度。 It works nicely in Firefox, but returns a value that is 300px smaller in Chrome... 它在Firefox中运行良好,但在Chrome中返回的值小于300px ...

You can see an example of this bug here . 你可以在这里看到这个bug的一个例子。 Though I must say it's in Hebrew. 虽然我必须说它是希伯来语。 Though that shouldn't matter much... 虽然那不应该太重要......

Has anyone had that happen before? 有人曾经发生过这种事吗? Here's the code that calculates the height: 这是计算高度的代码:

var heightLeftCol = $('#leftCol').height();
var sidebarHeight = $('#sidebar').height();
var minHeight = heightLeftCol > sidebarHeight ? heightLeftCol : sidebarHeight; 
$('#postArea').css('min-height', minHeight+100);

EDIT: This problem was not fixed but worked around in a way that I don't like, but it'll do for now. 编辑:这个问题没有修复,但以我不喜欢的方式解决 ,但它现在会做。 Here's the "solution" that I came up with: 这是我提出的“解决方案”:

if (jQuery.browser.safari) {
    $('#postArea').css('min-height', minHeight+400 + 'px');
}
else {
    $('#postArea').css('min-height', minHeight+100 + 'px');
}

Since both Safari and Chrome run on WebKit, the browser.safari actually selects chrome as well..I definitely do not consider this an optimal solution. 由于Safari和Chrome都在WebKit上运行,因此browser.safari实际上也选择了chrome。我绝对不认为这是一个最佳解决方案。

Thanks! 谢谢! Amit 阿米特

In Chrome, the height of the div does not include the height of your 300-pixel tall image "sheli.jpg" because it isn't specified anywhere in the html or css. 在Chrome中,div的高度不包括300像素高图像“sheli.jpg”的高度,因为它未在html或css中的任何位置指定。 If you specify the height="300" in your <img> tag or height: 300px; 如果在<img>标签或height: 300px;指定height="300" height: 300px; as part its style, it will work. 作为其风格的一部分,它将起作用。

If is because your image at that time is not yet loaded, therefore the height is 0. That explains the height deficit you got in Chrome. 如果是因为那时您的图像尚未加载,那么高度为0.这就解释了您在Chrome中获得的高度不足。 To solve this problem, put the piece of code that set the height inside jQuery(window).load() like this: 要解决这个问题,请将设置高度的代码放在jQuery(window).load()如下所示:

jQuery(window).load(function(){
     jQuery("#div1").css("height", jQuery("#div2").height());
});

根据讨论,Justin和我在上面的评论中将jQuery代码包装在$(window).load()中将允许此代码在图像完全加载后正确执行。

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

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