Image resize in JavaScript

设置图片组的 src 为自定义属性 data-img-src 的值

1
2
3
4
5
6
7
8
9
10
11
12
13
//注意for循环里的闭包
for(var i = 0; i < images.length; i++){
(function(im){
var img = new Image();
img.onload = function(){
im.src = img.src;
self.imgResize(im, img.width, img.height, 75, 75);
img.onload = null;
}
img.onerror = function(){}
img.src = im.getAttribute("data-img-src");
})(images[i]);
}

jQuery 版本:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
images.each(function(){
if( this.src.indexOf('loading.gif') != -1){
var im = new Image();
var img = this;
im.onload = function(){
img.src = im.src;
self.imgResize(img, im.width, im.height, 75, 75);
im.onload = null;
}
im.onerror = function(){
this.style.width = '75px';
this.style.height = '75px';
im.onerror = null;
}
im.src = this.getAttribute('data-img-src');
}
})
0%