我有一组动态获取的图像。对于我应用最大高度的每个图像,我使用它来调整大小。
我有一个 jquery re-size 函数:
// 调整我的图片大小
function resizeMyImg(objct, containerWidth, containerHeight) {
var width = $(objct).width(); // I try alerting here and get a 0 answer
if (width < containerWidth) {
var percentageToAdd = ((100 * width) / containerWidth) + 100;
$(objct).css({ maxHeight: percentageToAdd + "%" });
}
}
我正在尝试将此调整大小功能应用于我的图像:
$(document).ready(function () {
$(".photoAlbum").find("img").each(function () {
var myImage2 = $(this);
myImage2.load(function () {
alert(myImage2.height()); // I try alerting here and also get a 0 answer
resizeMyImg(myImage2, 240, 240);
});
});
});
这是我在页面中使用的 html,希望对您有所帮助:
<div style="padding-top: 0px;" id="photosHere">
<div class="photoAlbum">
<img alt="" src="/Uploads/553290_471992686161625_498413822_n867.jpg" style="max-height: 100%;">
</div>
<div class="photoAlbum">
<img alt="" src="/Uploads/484524_430827793626240_991120671_n575.jpg" style="max-height: 100%;">
</div>
<div class="photoAlbum">
<img alt="" src="/Uploads/553290_471992686161625_498413822_n717.jpg" style="max-height: 100%;">
</div>
<div class="photoAlbum">
<img alt="" src="/Uploads/me993.jpg" style="max-height: 100%;">
</div>
<div class="photoAlbum">
<img alt="" src="/Uploads/me759.jpg" style="max-height: 100%;">
</div>
</div>
我的问题是我无法获得图像的宽度,我总是得到 0 而不是实际宽度。但是,如果我测试图像使用萤火虫,我会得到正确的宽度。
注意:有时我得到正确的答案,但我刷新并再次得到 0。请问有什么建议吗??令人沮丧的是我花了几个小时在这上面..