mirror of
https://gitee.com/kekingcn/file-online-preview.git
synced 2025-09-18 09:44:37 +08:00
42 lines
915 B
JavaScript
42 lines
915 B
JavaScript
function isInSight(el) {
|
|
const bound = el.getBoundingClientRect();
|
|
const clientHeight = window.innerHeight;
|
|
//只考虑向下滚动加载
|
|
//const clientWidth=window.innerWeight;
|
|
return bound.top <= clientHeight + 100;
|
|
}
|
|
|
|
let index = 0;
|
|
function checkImgs() {
|
|
const imgs = document.querySelectorAll('.my-photo');
|
|
for (let i = index; i < imgs.length; i++) {
|
|
if (isInSight(imgs[i])) {
|
|
loadImg(imgs[i]);
|
|
index = i;
|
|
}
|
|
}
|
|
}
|
|
|
|
function loadImg(el) {
|
|
const source = el.dataset.src;
|
|
el.src = source;
|
|
}
|
|
|
|
function throttle(fn, mustRun = 500) {
|
|
const timer = null;
|
|
let previous = null;
|
|
return function() {
|
|
const now = new Date();
|
|
const context = this;
|
|
const args = arguments;
|
|
if (!previous) {
|
|
previous = now;
|
|
}
|
|
const remaining = now - previous;
|
|
if (mustRun && remaining >= mustRun) {
|
|
fn.apply(context, args);
|
|
previous = now;
|
|
}
|
|
}
|
|
}
|