【发布时间】:2015-07-13 02:04:37
【问题描述】:
目前有一个确定图片最高高度的功能来设置图片容器元素的最小高度,本例为图形。
该函数在窗口加载和调整窗口大小时可以正常工作。但是,在 ajax 调用将内容加载到我的目标 div 后,函数的结果只返回“0”。我读过我可能太快获得图像尺寸,这就是它返回“0”但不知道如何正确修改的原因。
如何等到图像加载完毕后再将我的函数应用于 ajax 成功(如果这就是问题所在!)?
JAVASCRIPT
// Get the highest image to use as the minimum Figure <figure> height
function minFigureHeight() {
var imgs = $('.property-image > img');
var maxImgHeight = 0;
imgs.each(function () {
var imgHeight = $(this).outerHeight();
maxImgHeight = maxImgHeight > imgHeight ? maxImgHeight : imgHeight;
});
return maxImgHeight
}
// When the window loads add a min-height style to the Figure <figure>
$(window).load(function() {
var newHeight = minFigureHeight();
$('.property-image').css("min-height", newHeight);
});
// If the window resizes recalculate and add a min-height style to the Figure <figure>
$(window).resize(function(){
var newHeight = minFigureHeight();
$('.property-image').css("min-height", newHeight);
});
HTML
<article class="property">
<figure class="property-image">
<img src="images/36048_BAO150030_IMG_00.JPG" alt="Any Street" />
</figure>
<div class="property-details">
<p>2 Bedroomed House</p>
</div>
</article>
函数运行后的 HTML(正常工作)
<article class="property">
<figure class="property-image" style="min-height: 240px">
<img src="images/36048_BAO150030_IMG_00.JPG" alt="Any Street" />
</figure>
<div class="property-details">
<p>2 Bedroomed House</p>
</div>
</article>
AJAX
$('#filter-search').on('change', function(e) {
e.preventDefault(); // prevent native submit
$(this).ajaxSubmit({
target: '#mytarget',
url: '/search_ajax',
type: 'POST',
success: function() {
var newHeight = minFigureHeight();
$('.property-image').css("min-height", newHeight);
}
});
});
函数在成功的 ajax 调用上运行后的 HTML(不工作)
<article class="property">
<figure class="property-image" style="min-height: 0px">
<img src="images/36048_BAO150030_IMG_00.JPG" alt="Any Street" />
</figure>
<div class="property-details">
<p>2 Bedroomed House</p>
</div>
</article>
【问题讨论】: