Skip to content

Commit

Permalink
feat: 实现一个简单的图片预加载;
Browse files Browse the repository at this point in the history
  • Loading branch information
jiaoyanlinmeitu committed Sep 27, 2019
1 parent 1f411b5 commit e7a48b6
Show file tree
Hide file tree
Showing 14 changed files with 200 additions and 28 deletions.
39 changes: 39 additions & 0 deletions dist/index.js

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions dist/index.js.map

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

39 changes: 39 additions & 0 deletions dist/index.min.js

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions dist/index.min.js.map

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

31 changes: 31 additions & 0 deletions es/index.js

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions es/index.js.map

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

32 changes: 31 additions & 1 deletion example/index.html
Original file line number Diff line number Diff line change
Expand Up @@ -5,9 +5,39 @@
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<meta http-equiv="X-UA-Compatible" content="ie=edge">
<title>example</title>
<style>
.img-wrap {
display: none;
}
.img {
width: 100px;
}
</style>
</head>
<body>

<div class="loading">loading</div>
<div class="img-wrap">
<img class="img img-1" src="" alt="">
<img class="img img-2" src="" alt="">
<img class="img img-3" src="" alt="">
<img class="img img-4" src="" alt="">
</div>
</body>
<script src="./index.js"></script>
<script>
const images = [
'./static/images/1.jpg',
'./static/images/2.jpg',
'./static/images/3.jpg',
'./static/images/4.jpg',
];
window.npmLibraryDemo(images, function() {
console.log('---------图片预加载完毕');
document.querySelector('.loading').style.display = 'none';
document.querySelector('.img-wrap').style.display = 'block';
for (var i = 0; i < images.length; i++) {
document.querySelector('.img-' + (i + 1)).setAttribute('src', images[i]);
}
});
</script>
</html>
Binary file added example/static/images/1.jpg
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added example/static/images/2.jpg
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added example/static/images/3.jpg
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added example/static/images/4.jpg
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
33 changes: 33 additions & 0 deletions lib/index.js

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions lib/index.js.map

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

50 changes: 23 additions & 27 deletions src/main.js
Original file line number Diff line number Diff line change
@@ -1,29 +1,25 @@
import demo from './demo.js';
import { version } from '../package.json';
import answer from 'the-answer';
import _merge from 'lodash/merge';

console.log('version:' + version);

let users = {
'data': [{ 'user': 'barney' }, { 'user': 'fred' }]
};
let ages = {
'data': [{ 'age': 36 }, { 'age': 40 }]
};
const a = _merge(users, ages);
console.log('-------lodash:', a);


async function initDemo() {
console.log('the answer is ' + answer);
const obj = {}
const newObj = Object.assign(obj, { age: 30 });
let data = await demo();
console.log('initDemo:', data, newObj);
return data;
const preload = (images, cb) => {
const len = images.length;
const imgArr = [];
let count = 0;
if (len) {
images.forEach((url, i) => {
const img = new Image();
img.src = url;
img.onload = () => imgLoad(i);
img.onerror = imgLoad;
imgArr.push(img);
});
function imgLoad(i) {
console.log(`---------img-${i} load`);
count++;
if (count === len) {
cb();
}
}
} else {
cb();
}
}

initDemo();

export default initDemo;
export default preload;

0 comments on commit e7a48b6

Please sign in to comment.