画像がキャッシュされる可能性があることを考慮して、画像が読み込まれた後、画像に合うようにdivの高さを調整しようとしています。
image completeプロパティを使用して、画像が既に読み込まれているかどうかを確認し、ハンドラー関数を呼び出したり、画像が完全に読み込まれた後に実行されるload関数にハンドラー関数を追加したりしようとしています。
image.completeを呼び出すと、未定義になります。
この種の動作を引き起こす可能性があるのは何ですか?
console.logを使用して画像を検査していたとき、完全なプロパティが明確に存在し、真の値を持っていました。
//functions
var setHeight = function(imageObj){
var imageHeight = imageObj.height();
console.log(imageObj);
console.log(imageHeight);
displayElement.css("height", imageHeight);
}
var setImageHeight = function(imageObj){
displayElement.html(imageObj);
if (imageObj.complete){
setHeight(imageObj);
} else {
imageObj.load(setHeight(imageObj));
}
}
// define and image, send it to the function to add to html and adjust height.
var imageObj = jQuery('<img class="current-image"/>').attr('src', ...);
setImageHeight(imageObj);