【发布时间】:2012-12-22 01:29:07
【问题描述】:
我有一组动态获取的图像。我为每个图像应用最大高度,我将其用于调整大小。
我有一个 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 而不是实际宽度。但是,如果我测试图像时使用 firebug,我会得到正确的宽度。
注意: 有时我得到了正确的答案,但我刷新并再次得到 0。请问有什么建议吗??令人沮丧的是我花了几个小时在这上面..
【问题讨论】:
-
能否请您给我看一下您页面的完整代码我想知道您是如何在 html 中传递函数的,或者如果您可以让我知道的话,请告诉我
-
我添加了 HTML,这是唯一缺少的部分。感谢您的帮助:)
标签: javascript jquery image-resizing