This repository has been archived by the owner on Nov 15, 2018. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 37
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
11 changed files
with
186 additions
and
150 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,112 @@ | ||
import http from "@/http.js"; | ||
import _ from "lodash"; | ||
|
||
function onEvent(el, type, func, capture = false) { | ||
el.addEventListener(type, func, { | ||
capture: capture | ||
}); | ||
} | ||
/** | ||
* 图片懒加载, 支持背景图片 | ||
*/ | ||
|
||
/* eslint-disable no-extend-native */ | ||
/** | ||
* 数组item remove方法 | ||
* @author jsonleex <[email protected]> | ||
*/ | ||
if (!Array.prototype.remove) { | ||
Array.prototype.remove = function(item) { | ||
if (!this.length) return; | ||
var index = this.indexOf(item); | ||
if (index > -1) { | ||
this.splice(index, 1); | ||
return this; | ||
} | ||
}; | ||
} | ||
export default (Vue, options = {}) => { | ||
const listenList = []; | ||
const imageFileCatch = []; | ||
const imageCatcheList = []; | ||
const isAlredyLoad = fileID => { | ||
return imageFileCatch.indexOf(fileID); | ||
}; | ||
|
||
/* | ||
* check el is in view | ||
* @return {Boolean} el is in view | ||
*/ | ||
const checkInView = el => { | ||
const rect = el.getBoundingClientRect(); | ||
return ( | ||
rect.top < window.innerHeight * 1.3 && | ||
rect.bottom > 1.3 && | ||
(rect.left < window.innerWidth * 1.3 && rect.right > 0) | ||
); | ||
}; | ||
const isCanShow = item => { | ||
const { el, src, file, q = 40 } = item; | ||
const isIMG = el.nodeName === "IMG"; | ||
if (checkInView(el)) { | ||
let image = new Image(); | ||
http.get(`/files/${file}?q=${q}&json=true`).then(({ data: { url } }) => { | ||
image.src = url; | ||
image.onload = () => { | ||
isIMG ? (el.src = url) : (el.style.backgroundImage = `url(${url})`); | ||
listenList.remove(item); | ||
imageFileCatch.push(file); | ||
imageCatcheList.push(url); | ||
image = null; | ||
}; | ||
image.onerror = () => { | ||
listenList.remove(item); | ||
}; | ||
}); | ||
return true; | ||
} else { | ||
return false; | ||
} | ||
}; | ||
const addListener = (el, binding) => { | ||
const file = binding.value; | ||
const index = isAlredyLoad(file.file); | ||
const isIMG = el.nodeName === "IMG"; | ||
if (index > -1) | ||
return isIMG | ||
? (el.src = imageCatcheList[index]) | ||
: (el.style.backgroundImage = `url(${imageCatcheList[index]})`); | ||
const item = { | ||
el, | ||
nodeName: el.nodeName, | ||
error: false, | ||
loading: true, | ||
...file | ||
}; | ||
if (isCanShow(item)) return; | ||
listenList.push(item); | ||
}; | ||
Vue.directive("async-image", { | ||
inserted: addListener, | ||
updated: addListener | ||
}); | ||
[ | ||
"scroll", | ||
"wheel", | ||
"mousewheel", | ||
"resize", | ||
"animationend", | ||
"transitionend", | ||
"touchmove" | ||
].forEach(evt => | ||
onEvent( | ||
window, | ||
evt, | ||
_.debounce(() => { | ||
listenList.map(img => { | ||
isCanShow(img); | ||
}); | ||
}, 1000 / 60) | ||
) | ||
); | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.