本文實例講述了JavaScript簡單獲取頁面圖片原始尺寸的方法。分享給大家供大家參考,具體如下:
這裡通過Image()對象獲取原始寬高
這種方式就沒有那麼麻煩,直接new一個Image()對象,然後把img的src賦值給他即可獲取。
var img = new Image();
img.src = $("#target").attr("src");
if(img.complete){
alert('width:'+img.width+',height'+img.height);
img = null;
}else{
img.onload = function(){
alert('width:'+img.width+',height'+img.height);
img = null;
};
}
並且不要擔心new Image對象會多一個http請求,浏覽器加載圖片後已經有緩存,你new N個image對象都沒問題,當然,內存會消耗,所以用完後img置為null。
更多關於JavaScript相關內容感興趣的讀者可查看本站專題:《JavaScript切換特效與技巧總結》、《JavaScript查找算法技巧總結》、《JavaScript動畫特效與技巧匯總》、《JavaScript錯誤與調試技巧總結》、《JavaScript數據結構與算法技巧總結》、《JavaScript遍歷算法與技巧總結》及《JavaScript數學運算用法總結》
希望本文所述對大家JavaScript程序設計有所幫助。