LoeiFy / Recordum

GitHub issues blog
MIT License
33 stars 6 forks source link

加载图片获取宽高问题 #5

Closed LoeiFy closed 8 years ago

LoeiFy commented 9 years ago

浏览器在加载图片过程中,可能很早就获得图片宽高,这时候可以用 js 获取宽高,而不用在 onload 时候获得

var img = new Image();
img.src = 'xxx.jpg?'+ +new Date();

// interval
var check = function(){
    console.log('unload:'+ img.width +'####'+ img.height)
}
var set = setInterval(check,40);

// loaded
img.onload = function(){
    console.log('loaded:'+ img.width +'####'+ img.height)
    clearInterval(set)
}
LoeiFy commented 9 years ago

图片很多的话,要注意定时器问题,不能多个一起