你需要等待圖像加載:
function setModalPicture(picName){
//Build the path to the picture
var pic= 'assets/img/art/'+picName;
//Set the picture
var img = $('#g-modal-img');
img.one("load", adjustModalPadding).attr('src', pic);
if (img[0].complete) {
img.off("load", adjustModalPadding);
adjustModalPadding();
}
}
注意上面的順序,因爲它是重要的:
- 首先,用一次性的處理程序掛鉤
load
事件(one
)。
- 然後,設置
src
。
- 檢查圖像是否已經完成:如果是,請刪除處理程序並立即調用您的函數;否則,當
load
觸發時,它將調用adjustModalPadding
並將其作爲處理程序移除。
您可能需要增加搬運到錯誤...
這裏有一個工作示例:
function setModalPicture(picName) {
//Build the path to the picture
var pic = picName; // 'assets/img/art/'+picName;
//Set the picture
var img = $('#g-modal-img');
img.one("load", adjustModalPadding).attr('src', pic);
console.log("img[0].complete after setting src: " + img[0].complete);
if (img[0].complete) {
img.off("load", adjustModalPadding);
adjustModalPadding();
}
}
function adjustModalPadding() {
var img = $("#g-modal-img")[0];
console.log("Size: " + img.width + "x" + img.height);
}
$("input[type=button]").on("click", function() {
console.log("img[0].complete before starting: " + $("#g-modal-img")[0].complete);
setModalPicture("https://graph.facebook.com/1035045703246692/picture?type=large");
});
<!-- In a comment, you said it starts out with src="" -->
<img id="g-modal-img" src="">
<input type="button" value="Click Me">
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
,在Chrome瀏覽器對我的作品, Firefox和IE11。
或者,您也可以通過克隆創建替換img
元素:
function setModalPicture(picName) {
//Build the path to the picture
var pic = picName; // 'assets/img/art/'+picName;
//Set the picture
var img = $("#g-modal-img");
var newImage = img.clone();
img.replaceWith(newImage);
newImage.one("load", adjustModalPadding).attr('src', pic);
console.log("newImage[0].complete after setting src: " + newImage[0].complete);
if (newImage[0].complete) {
newImage.off("load", adjustModalPadding);
adjustModalPadding();
}
}
function adjustModalPadding() {
var img = $("#g-modal-img")[0];
console.log("Size: " + img.width + "x" + img.height);
}
$("input[type=button]").on("click", function() {
console.log("img[0].complete before starting: " + $("#g-modal-img")[0].complete);
setModalPicture("https://graph.facebook.com/1035045703246692/picture?type=large");
});
<!-- In a comment, you said it starts out with src="" -->
<img id="g-modal-img" src="">
<input type="button" value="Click Me">
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
,在Chrome瀏覽器,Firefox和IE11還爲我工作。
最後,您可以從頭開始創建(未克隆)的替代img
元素:
function setModalPicture(picName) {
//Build the path to the picture
var pic = picName; // 'assets/img/art/'+picName;
//Set the picture
var img = $("#g-modal-img");
var newImage = $("<img>").attr("id", "g-modal-img");
img.replaceWith(newImage);
newImage.one("load", adjustModalPadding).attr('src', pic);
console.log("newImage[0].complete after setting src: " + newImage[0].complete);
if (newImage[0].complete) {
newImage.off("load", adjustModalPadding);
adjustModalPadding();
}
}
function adjustModalPadding() {
var img = $("#g-modal-img")[0];
console.log("Size: " + img.width + "x" + img.height);
}
$("input[type=button]").on("click", function() {
console.log("img[0].complete before starting: " + $("#g-modal-img")[0].complete);
setModalPicture("https://graph.facebook.com/1035045703246692/picture?type=large");
});
<!-- In a comment, you said it starts out with src="" -->
<img id="g-modal-img" src="">
<input type="button" value="Click Me">
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
問題是圖片的尺寸未知unti l圖像成功*加載*。你需要在圖像中添加一個'onload'監聽器。有關重複問題,請參閱http://stackoverflow.com/a/626505/1902010。 – ceejayoz
提升不會馬上執行您的功能。 – Gavin
JavaScript **是一種「常規」編程語言。 –