-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathpreloadimg.html
52 lines (48 loc) · 1.32 KB
/
preloadimg.html
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
<!DOCTYPE html>
<!--
Created using JS Bin
http://jsbin.com
Copyright (c) 2015 by anonymous (http://jsbin.com/nubuqesoba/4/edit)
Released under the MIT license: http://jsbin.mit-license.org
-->
<meta name="robots" content="noindex">
<html>
<head>
<script src="http://code.jquery.com/jquery-2.1.1.min.js"></script>
<meta charset="utf-8">
<title>JS Bin</title>
</head>
<body>
<script id="jsbin-javascript">
$(function(){
var arr = [];
function preload(picArr, callback){
var picObj = {},
picLen = picArr.length,
addImgToObj = function(imgName, img){
picObj[imgName]=img;
};
while(picLen--){
var img = new Image(),
url = picArr[picLen],
imgName = url.slice(url.lastIndexOf('/')+1, -4);
img.src = picArr[picLen];
picObj[imgName] = img;
img.onload = addImgToObj(imgName, img);
}
callback.call(this, picObj);
}
var testArr = [
'http://s.cn.bing.net/az/hprichbg/rb/DevetakiCave_ZH-CN13229318639_1920x1080.jpg',
'http://s.cn.bing.net/az/hprichbg/rb/KelpGoose_ZH-CN11521792782_1920x1080.jpg',
'http://s.cn.bing.net/az/hprichbg/rb/LihuBridge_ZH-CN13125700510_1920x1080.jpg'
];
preload(testArr, function(obj){
for(var item in obj){
$('body').append('<img src="'+obj[item].src+'">');
}
});
});
</script>
</body>
</html>