add lazyload

This commit is contained in:
Harvey Zack committed 2018-10-22 11:10:57 +08:00
1 parent 8b6258c4e5
commit a26afec5ac
4 files changed
+68 -18

No files matched your search

+30 -2
View File
@@ -2,6 +2,7 @@ export default class Thumbnails {
constructor(art, option) {
this.art = art;
this.option = option;
this.isLoad = false;
this.init();
}
@@ -12,8 +13,10 @@ export default class Thumbnails {
} = this.art;
proxy($progress, 'mousemove', event => {
this.option.ref.style.display = 'block';
this.showThumbnails(event);
this.checkLoad(this.art.option.thumbnails.url).then(() => {
this.option.ref.style.display = 'block';
this.showThumbnails(event);
});
});
proxy($progress, 'mouseout', () => {
@@ -21,6 +24,30 @@ export default class Thumbnails {
});
}
checkLoad(url) {
if (this.isLoad) {
return Promise.resolve(url);
}
const { proxy } = this.art.events;
return new Promise((resolve, reject) => {
const $img = new Image();
$img.src = url;
if ($img.complete) {
this.isLoad = true;
resolve(url);
}
proxy($img, 'load', () => {
this.isLoad = true;
resolve(url);
});
proxy($img, 'error', () => {
reject(url);
});
});
}
showThumbnails(event) {
const { $progress } = this.art.refs;
const { width: posWidth } = this.getPosFromEvent(event);
@@ -29,6 +56,7 @@ export default class Thumbnails {
const perIndex = Math.ceil(posWidth / perWidth);
let yIndex = Math.ceil(perIndex / column);
let xIndex = perIndex % column || column;
this.option.ref.style.backgroundImage = `url(${url})`;
this.option.ref.style.height = `${height}px`;
this.option.ref.style.width = `${width}px`;
+2 -7
View File
@@ -11,14 +11,9 @@ export default class Events {
}
target.addEventListener(name, callback, option);
const destroy = () => {
this.destroyEvents.push(() => {
target.removeEventListener(name, callback, option);
};
this.destroyEvents.push(destroy);
return destroy;
});
}
destroy() {