【发布时间】:2015-12-04 18:15:11
【问题描述】:
我有一个程序
jQuery(function($){
function makeParentHeightOfSmallestChild (parentSelector)
{
var parent = $(parentSelector);
var children = parent.children();
if (children.length < 2) return;
var smallestHeight = Number.MAX_VALUE;
children.each(function(){
var thisHeight = $(this).height();
if (thisHeight < smallestHeight) smallestHeight = thisHeight;
});
parent.height(smallestHeight);
}
$(window).load(function(){
makeParentHeightOfSmallestChild('#contact-page-container > .row');
});
$(window).resize(function(){
makeParentHeightOfSmallestChild('#contact-page-container > .row');
});
});
这是不言自明的。我遇到的问题是smallestHeight 的值明显小于已迭代计算它的元素的实际最小高度。元素是您可以在
<div class="container" id="contact-page-container">
<h1>Get in touch</h1>
<div class="row">
<div class="col-xxs-12 col-xs-12 col-sm-6 col-md-6 col-lg-6">
<h3>Email</h3>
<p>S,madsn sandaskldasjk laskldaskjd skajkasdkjas k</p>
<input type="text" value="Your Name"/>
<input type="text" value="Your Email Address" />
<textarea value="Your Message" rows="18">Your Message</textarea>
</div>
<div class="col-xxs-12 col-xs-12 col-sm-6 col-md-6 col-lg-6">
<h3>Address</h3>
<p>Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam, quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo consequat. Duis aute irure dolor in reprehenderit in voluptate velit esse cillum dolore eu fugiat nulla pariatur. Excepteur sint occaecat cupidatat non proident, sunt in culpa qui officia deserunt mollit anim id est laborum.</p>
<iframe class="same-height-as-width" src="https://www.google.com/maps/embed?pb=!1m18!1m12!1m3!1d2686.2302199264063!2d-122.13376389999996!3d47.67994880000002!2m3!1f0!2f0!3f0!3m2!1i1024!2i768!4f13.1!3m3!1m2!1s0x54900d52444ba747%3A0x577e2db631b0711c!2s8620+154th+Ave+NE%2C+Redmond%2C+WA+98052!5e0!3m2!1sen!2sus!4v1441747735537" frameborder="0" allowfullscreen></iframe>
</div>
</div>
当我查看 Inspect Element 时,高度分别为 595.556 像素和 747.778 像素,但不知何故,父级的高度被设置为 242 像素。这是为什么呢?
【问题讨论】:
-
试试 outerHeight() - 包括填充或 outerHeight(true),也包括边距。 api.jquery.com/outerheight
-
创建具有足够 css 的演示来复制问题
标签: javascript jquery css algorithm responsive-design