[英]How do I get alerted in Javascript that an image's dimensions are available?
[英]How do I get original image dimensions?
我知道以下問題的解決方案: 使用JavaScript獲取圖像的實際寬度和高度? (在Safari / Chrome中)
但是我想知道如果您確定圖像已經加載,是否有一種更簡單的方法來查找圖像的原始尺寸。
在我的代碼中,我已經使用了:
$("<img/>").attr("src", $(img).attr("src")).load(function() {
imageWidth = this.width;
imageHeight = this.height;
});
但這在不確定圖像是否已加載或緩存時很有用。
當您不必擔心圖像被緩存或加載時,是否還有另一種方法可能會大大加快執行速度?
我想在調整窗口$(window).resize(function(){});
大小時檢測圖像的真實尺寸$(window).resize(function(){});
。
由於此事件的觸發非常快,因此我想確保使用最有效的方法,而不是最謹慎的方法。
嘗試這個。 我認為應該更快。
var newImg = new Image();
newImg.src = $(img).attr('src');
imageWidth = newImg.width;
imageHeight = newImg.height;
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.