你需要等待图像加载:
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 **是一种“常规”编程语言。 –