js 适应网页图片

javascript

16-3-19 17:01:08

 <img src="https://img.alicdn.com/tps/TB1YKLrPpXXXXbJXFXXXXXXXXXX-1600-350.jpg">
<img src="https://img.alicdn.com/tps/TB1UUOHPFXXXXbRaXXXXXXXXXXX-1600-350.jpg">
<img src="https://img.alicdn.com/tps/TB1_dSQPFXXXXa3XXXXXXXXXXXX-1600-350.jpg">
<img src="timg (1).bmp">

<script>
var imgdefereds=[];
$('img').each(function(){
 var dfd=$.Deferred();
 $(this).bind('load',function(){
 dfd.resolve();
 }).bind('error',function(){
 //图片加载错误,加入错误处理
 // dfd.resolve();
 })
 if(this.complete) setTimeout(function(){
 dfd.resolve();
 },1000);
 imgdefereds.push(dfd);
})
$.when.apply(null,imgdefereds).done(function(){
  callback();
});

function callback(){
console.log('img ok'); //函数
}
</script>