【发布时间】:2023-03-08 17:49:02
【问题描述】:
示例:http://jsfiddle.net/forgetcolor/rZSCg/3/
假设 div 设置为 overflow:auto,它(可能)包含一个大于其窗口的图像,我试图找出一种方法来等待该图像加载,然后再查询其属性。我追求的属性是它的内部宽度,减去它的滚动条。感谢以前的stackoverflow问题(http://stackoverflow.com/questions/10692598/how-to-get-innerwidth-of-an-element-in-jquery-without-scrollbar)我有一种技术可以做到这一点,但这仅适用于小图像或缓存中的图像。当它获得一个大图像时,执行查询的代码会在图像加载完成之前执行。
我想要的是一种在执行查询之前等待该图像加载的方法。有什么想法吗?
完整示例:http://jsfiddle.net/forgetcolor/rZSCg/3/
Javascript:
// using a random appended to the URL to simulate non-cached
// varying images
var r = Math.floor((Math.random()*100000)+1);
$('#box1').html("<img src='http://lorempixel.com/output/fashion-q-c-1920-1920-4.jpg?"+r+"' />");
var helperDiv = $('<div />');
$('#box1').append(helperDiv);
$('#iw').text("inner width is: " + helperDiv.width());
helperDiv.remove();
【问题讨论】:
标签: jquery