Skip to content

Commit 27d8b59

Browse files
committed
Tab > Spaces
1 parent 840fc5f commit 27d8b59

File tree

9 files changed

+154
-153
lines changed

9 files changed

+154
-153
lines changed

README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -36,4 +36,5 @@ this.imgLoader.load(imgUrlOriginal, (err, data) => {
3636
使用方法可参考 demo 目录的事例,包括加载单张图片、及加载多张图片的场景,运行效果如下图所示:
3737

3838
![单张图片预加载](http://storage.360buyimg.com/mtd/home/single-img-load1483686270312.gif)
39+
3940
![多张图片预加载](http://storage.360buyimg.com/mtd/home/multi-img-load1483686388552.gif)

demo/multi-load/multi-load.js

Lines changed: 44 additions & 44 deletions
Original file line numberDiff line numberDiff line change
@@ -7,52 +7,52 @@ const ImgLoader = require('../../img-loader/img-loader.js')
77

88
//生成一些测试数据
99
function genImgListData() {
10-
let images = [
11-
'http://img10.360buyimg.com/img/s600x600_jfs/t3586/215/2086933702/144606/c5885c8b/583e2f08N13aa7762.png',
12-
'http://img10.360buyimg.com/img/s600x600_jfs/t3643/111/394078676/159202/a59f6b72/5809b3ccN41e5e01f.jpg',
13-
'http://img10.360buyimg.com/img/s600x600_jfs/t3388/167/1949827805/115796/6ad813/583660fbNe5c34eae.jpg',
14-
'http://img10.360buyimg.com/img/s600x600_jfs/t3163/281/5203602423/192427/db09be58/5865cb7eN808cc6f4.png',
15-
'http://img10.360buyimg.com/img/s600x600_jfs/t3634/225/410542226/185677/c17f0ecf/5809b073N364fe77e.jpg',
16-
'http://img10.360buyimg.com/img/s600x600_jfs/t3808/206/329427377/119593/a8cf7470/580df323Nb641ab96.jpg',
17-
'http://img10.360buyimg.com/img/s600x600_jfs/t3805/133/325945617/116002/e90e0bdf/580df2b5Ncb04c5ac.jpg',
18-
'http://img10.360buyimg.com/img/s600x600_jfs/t3046/316/3037048447/184004/706c779e/57eb584fN4f8b6502.jpg',
19-
'http://img10.360buyimg.com/img/s600x600_jfs/t3580/212/1841964843/369414/e78739fb/58351586Ne20ac82a.jpg',
20-
'http://img10.360buyimg.com/img/s600x600_jfs/t3274/70/4925773051/133266/fae6e84/585c9890Na19001c8.png',
21-
'http://img10.360buyimg.com/img/s600x600_jfs/t3157/27/5204515328/123020/5f061d81/5865cbcaNfdf0557f.png',
22-
'http://img10.360buyimg.com/img/s600x600_jfs/t3265/341/5152556931/143928/bdf279a4/5865cb96Nff26fc5d.png'
23-
]
24-
images = images.concat(images.slice(0, 4))
25-
return images.map(item => {
26-
return {
27-
url: item,
28-
loaded: false
29-
}
30-
})
10+
let images = [
11+
'http://img10.360buyimg.com/img/s600x600_jfs/t3586/215/2086933702/144606/c5885c8b/583e2f08N13aa7762.png',
12+
'http://img10.360buyimg.com/img/s600x600_jfs/t3643/111/394078676/159202/a59f6b72/5809b3ccN41e5e01f.jpg',
13+
'http://img10.360buyimg.com/img/s600x600_jfs/t3388/167/1949827805/115796/6ad813/583660fbNe5c34eae.jpg',
14+
'http://img10.360buyimg.com/img/s600x600_jfs/t3163/281/5203602423/192427/db09be58/5865cb7eN808cc6f4.png',
15+
'http://img10.360buyimg.com/img/s600x600_jfs/t3634/225/410542226/185677/c17f0ecf/5809b073N364fe77e.jpg',
16+
'http://img10.360buyimg.com/img/s600x600_jfs/t3808/206/329427377/119593/a8cf7470/580df323Nb641ab96.jpg',
17+
'http://img10.360buyimg.com/img/s600x600_jfs/t3805/133/325945617/116002/e90e0bdf/580df2b5Ncb04c5ac.jpg',
18+
'http://img10.360buyimg.com/img/s600x600_jfs/t3046/316/3037048447/184004/706c779e/57eb584fN4f8b6502.jpg',
19+
'http://img10.360buyimg.com/img/s600x600_jfs/t3580/212/1841964843/369414/e78739fb/58351586Ne20ac82a.jpg',
20+
'http://img10.360buyimg.com/img/s600x600_jfs/t3274/70/4925773051/133266/fae6e84/585c9890Na19001c8.png',
21+
'http://img10.360buyimg.com/img/s600x600_jfs/t3157/27/5204515328/123020/5f061d81/5865cbcaNfdf0557f.png',
22+
'http://img10.360buyimg.com/img/s600x600_jfs/t3265/341/5152556931/143928/bdf279a4/5865cb96Nff26fc5d.png'
23+
]
24+
images = images.concat(images.slice(0, 4))
25+
return images.map(item => {
26+
return {
27+
url: item,
28+
loaded: false
29+
}
30+
})
3131
}
3232

3333
Page({
34-
data: {
35-
imgList: genImgListData()
36-
},
37-
onLoad() {
38-
//初始化图片预加载组件,并指定统一的加载完成回调
39-
this.imgLoader = new ImgLoader(this, this.imageOnLoad.bind(this))
40-
},
41-
loadImages() {
42-
//同时发起全部图片的加载
43-
this.data.imgList.forEach(item => {
44-
this.imgLoader.load(item.url)
45-
})
46-
},
47-
//加载完成后的回调
48-
imageOnLoad(err, data) {
49-
console.log('图片加载完成', err, data.src)
34+
data: {
35+
imgList: genImgListData()
36+
},
37+
onLoad() {
38+
//初始化图片预加载组件,并指定统一的加载完成回调
39+
this.imgLoader = new ImgLoader(this, this.imageOnLoad.bind(this))
40+
},
41+
loadImages() {
42+
//同时发起全部图片的加载
43+
this.data.imgList.forEach(item => {
44+
this.imgLoader.load(item.url)
45+
})
46+
},
47+
//加载完成后的回调
48+
imageOnLoad(err, data) {
49+
console.log('图片加载完成', err, data.src)
5050

51-
const imgList = this.data.imgList.map(item => {
52-
if (item.url == data.src)
53-
item.loaded = true
54-
return item
55-
})
56-
this.setData({ imgList })
57-
}
51+
const imgList = this.data.imgList.map(item => {
52+
if (item.url == data.src)
53+
item.loaded = true
54+
return item
55+
})
56+
this.setData({ imgList })
57+
}
5858
})

demo/multi-load/multi-load.wxml

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
<view class="img_list">
2-
<view wx:for="{{ imgList }}" class="img_wrap">
3-
<image wx:if="{{ item.loaded }}" src="{{ item.url }}" class="fade_in" />
4-
</view>
2+
<view wx:for="{{ imgList }}" class="img_wrap">
3+
<image wx:if="{{ item.loaded }}" src="{{ item.url }}" class="fade_in" />
4+
</view>
55
</view>
66

77
<button bindtap="loadImages">Click To Load Images</button>

demo/multi-load/multi-load.wxss

Lines changed: 10 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,24 +1,24 @@
11
@keyframes fadeIn {
2-
0% { opacity: 0; }
3-
100% { opacity: 1; }
2+
0% { opacity: 0; }
3+
100% { opacity: 1; }
44
}
55

66
.img_list {
7-
margin: 10px 0;
8-
text-align: center;
7+
margin: 10px 0;
8+
text-align: center;
99
}
1010

1111
.img_wrap {
12-
display: inline-block;
13-
width: 185rpx;
14-
height: 185rpx;
12+
display: inline-block;
13+
width: 185rpx;
14+
height: 185rpx;
1515
}
1616

1717
image {
18-
width: 100%;
19-
height: 100%;
18+
width: 100%;
19+
height: 100%;
2020
}
2121

2222
.fade_in {
23-
animation: fadeIn 1s both;
23+
animation: fadeIn 1s both;
2424
}

demo/single-load/single-load.js

Lines changed: 22 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -11,28 +11,28 @@ const imgUrlThumbnail = 'http://storage.360buyimg.com/mtd/home/lion1483683731203
1111
const imgUrlOriginal = 'http://storage.360buyimg.com/mtd/home/lion1483624894660.jpg'
1212

1313
Page({
14-
data: {
15-
msg: '',
16-
imgUrl: ''
17-
},
18-
onLoad() {
19-
//初始化图片预加载组件
20-
this.imgLoader = new ImgLoader(this)
21-
},
22-
loadImage() {
23-
//加载缩略图
24-
this.setData({
25-
msg: '大图正在拼命加载..',
26-
imgUrl: imgUrlThumbnail
27-
})
14+
data: {
15+
msg: '',
16+
imgUrl: ''
17+
},
18+
onLoad() {
19+
//初始化图片预加载组件
20+
this.imgLoader = new ImgLoader(this)
21+
},
22+
loadImage() {
23+
//加载缩略图
24+
this.setData({
25+
msg: '大图正在拼命加载..',
26+
imgUrl: imgUrlThumbnail
27+
})
2828

29-
//同时对原图进行预加载,加载成功后再替换
30-
this.imgLoader.load(imgUrlOriginal, (err, data) => {
31-
console.log('图片加载完成', err, data.src)
32-
this.setData({ msg: '大图加载完成~' })
29+
//同时对原图进行预加载,加载成功后再替换
30+
this.imgLoader.load(imgUrlOriginal, (err, data) => {
31+
console.log('图片加载完成', err, data.src)
32+
this.setData({ msg: '大图加载完成~' })
3333

34-
if (!err)
35-
this.setData({ imgUrl: data.src })
36-
})
37-
}
34+
if (!err)
35+
this.setData({ imgUrl: data.src })
36+
})
37+
}
3838
})

demo/single-load/single-load.wxml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
<view class="img_wrap">
2-
<image wx:if="{{ imgUrl }}" src="{{ imgUrl }}" />
2+
<image wx:if="{{ imgUrl }}" src="{{ imgUrl }}" />
33
</view>
44

55
<button bindtap="loadImage">Click To Load Image</button>

demo/single-load/single-load.wxss

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,16 @@
11
.img_wrap {
2-
margin-bottom: 10px;
3-
width: 750rpx;
4-
height: 468rpx;
5-
background: #ececec;
2+
margin-bottom: 10px;
3+
width: 750rpx;
4+
height: 468rpx;
5+
background: #ececec;
66
}
77

88
image {
9-
width: 100%;
10-
height: 100%;
9+
width: 100%;
10+
height: 100%;
1111
}
1212

1313
.msg {
14-
margin: 10px 0;
15-
text-align: center;
14+
margin: 10px 0;
15+
text-align: center;
1616
}

img-loader/img-loader.js

Lines changed: 64 additions & 64 deletions
Original file line numberDiff line numberDiff line change
@@ -5,81 +5,81 @@
55
*/
66

77
class ImgLoader {
8-
/**
9-
* 初始化方法,在页面的 onLoad 方法中调用,传入 Page 对象及图片加载完成的默认回调
10-
*/
11-
constructor(pageContext, defaultCallback) {
12-
this.page = pageContext
13-
this.defaultCallback = defaultCallback || function(){}
14-
this.callbacks = {}
15-
this.imgInfo = {}
8+
/**
9+
* 初始化方法,在页面的 onLoad 方法中调用,传入 Page 对象及图片加载完成的默认回调
10+
*/
11+
constructor(pageContext, defaultCallback) {
12+
this.page = pageContext
13+
this.defaultCallback = defaultCallback || function(){}
14+
this.callbacks = {}
15+
this.imgInfo = {}
1616

17-
this.page.data.imgLoadList = [] //下载队列
18-
this.page._imgOnLoad = this._imgOnLoad.bind(this)
19-
this.page._imgOnLoadError = this._imgOnLoadError.bind(this)
20-
}
17+
this.page.data.imgLoadList = [] //下载队列
18+
this.page._imgOnLoad = this._imgOnLoad.bind(this)
19+
this.page._imgOnLoadError = this._imgOnLoadError.bind(this)
20+
}
2121

22-
/**
23-
* 加载图片
24-
*
25-
* @param {String} src 图片地址
26-
* @param {Function} callback 加载完成后的回调(可选),第一个参数个错误信息,第二个为图片信息
27-
*/
28-
load(src, callback) {
29-
if (!src) return;
22+
/**
23+
* 加载图片
24+
*
25+
* @param {String} src 图片地址
26+
* @param {Function} callback 加载完成后的回调(可选),第一个参数个错误信息,第二个为图片信息
27+
*/
28+
load(src, callback) {
29+
if (!src) return;
3030

31-
let list = this.page.data.imgLoadList,
32-
imgInfo = this.imgInfo[src]
31+
let list = this.page.data.imgLoadList,
32+
imgInfo = this.imgInfo[src]
3333

34-
if (callback)
35-
this.callbacks[src] = callback
34+
if (callback)
35+
this.callbacks[src] = callback
3636

37-
//已经加载成功过的,直接回调
38-
if (imgInfo) {
39-
this._runCallback(null, {
40-
src: src,
41-
width: imgInfo.width,
42-
height: imgInfo.height
43-
})
44-
45-
//新的未在下载队列中的
46-
} else if (list.indexOf(src) == -1) {
47-
list.push(src)
48-
this.page.setData({ 'imgLoadList': list })
49-
}
50-
}
37+
//已经加载成功过的,直接回调
38+
if (imgInfo) {
39+
this._runCallback(null, {
40+
src: src,
41+
width: imgInfo.width,
42+
height: imgInfo.height
43+
})
44+
45+
//新的未在下载队列中的
46+
} else if (list.indexOf(src) == -1) {
47+
list.push(src)
48+
this.page.setData({ 'imgLoadList': list })
49+
}
50+
}
5151

52-
_imgOnLoad(ev) {
53-
let src = ev.currentTarget.dataset.src,
54-
width = ev.detail.width,
55-
height = ev.detail.height
52+
_imgOnLoad(ev) {
53+
let src = ev.currentTarget.dataset.src,
54+
width = ev.detail.width,
55+
height = ev.detail.height
5656

57-
//记录已下载图片的尺寸信息
58-
this.imgInfo[src] = { width, height }
59-
this._removeFromLoadList(src)
57+
//记录已下载图片的尺寸信息
58+
this.imgInfo[src] = { width, height }
59+
this._removeFromLoadList(src)
6060

61-
this._runCallback(null, { src, width, height })
62-
}
61+
this._runCallback(null, { src, width, height })
62+
}
6363

64-
_imgOnLoadError(ev) {
65-
let src = ev.currentTarget.dataset.src
66-
this._removeFromLoadList(src)
67-
this._runCallback('Loading failed', { src })
68-
}
64+
_imgOnLoadError(ev) {
65+
let src = ev.currentTarget.dataset.src
66+
this._removeFromLoadList(src)
67+
this._runCallback('Loading failed', { src })
68+
}
6969

70-
//将图片从下载队列中移除
71-
_removeFromLoadList(src) {
72-
let list = this.page.data.imgLoadList
73-
list.splice(list.indexOf(src), 1)
74-
this.page.setData({ 'imgLoadList': list })
75-
}
70+
//将图片从下载队列中移除
71+
_removeFromLoadList(src) {
72+
let list = this.page.data.imgLoadList
73+
list.splice(list.indexOf(src), 1)
74+
this.page.setData({ 'imgLoadList': list })
75+
}
7676

77-
//执行回调
78-
_runCallback(err, data) {
79-
let callback = this.callbacks[data.src] || this.defaultCallback
80-
callback(err, data)
81-
delete this.callbacks[data.src]
82-
}
77+
//执行回调
78+
_runCallback(err, data) {
79+
let callback = this.callbacks[data.src] || this.defaultCallback
80+
callback(err, data)
81+
delete this.callbacks[data.src]
82+
}
8383
}
8484

8585
module.exports = ImgLoader

img-loader/img-loader.wxml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,3 @@
11
<template name="img-loader">
2-
<image wx:for="{{ imgLoadList }}" wx:key="*this" src="{{ item }}" data-src="{{ item }}" bindload="_imgOnLoad" binderror="_imgOnLoadError" style="width:0;height:0;opacity:0" />
2+
<image wx:for="{{ imgLoadList }}" wx:key="*this" src="{{ item }}" data-src="{{ item }}" bindload="_imgOnLoad" binderror="_imgOnLoadError" style="width:0;height:0;opacity:0" />
33
</template>

0 commit comments

Comments
 (0)