IE兼容性问题,目前已兼容到IE9

This commit is contained in:
1045485954@qq.com
2019-07-17 07:21:43 +08:00
committed by kl
parent 551eeb0390
commit e4407467dd

View File

@ -1,41 +1,65 @@
function isInSight(el) { function isInSight(el) {
const bound = el.getBoundingClientRect(); var bound = el.getBoundingClientRect();
const clientHeight = window.innerHeight; var clientHeight = window.innerHeight;
//只考虑向下滚动加载 //只考虑向下滚动加载
//const clientWidth=window.innerWeight; //const clientWidth=window.innerWeight;
return bound.top <= clientHeight + 100; return bound.top <= clientHeight + 100;
} }
let index = 0; var index = 0;
function checkImgs() { function checkImgs() {
const imgs = document.querySelectorAll('.my-photo'); var imgs = document.querySelectorAll('.my-photo');
for (let i = index; i < imgs.length; i++) { for (var i = index; i < imgs.length; i++) {
if (isInSight(imgs[i])) { if (isInSight(imgs[i])) {
loadImg(imgs[i]); loadImg(imgs[i]);
index = i; index = i;
}
} }
}
} }
function loadImg(el) { function loadImg(el) {
const source = el.dataset.src; var source = el.getAttribute("data-src");
el.src = source; el.src = source;
}
// var mustRun = 500
// function throttle(fn, mustRun) {
// var timer = null;
// var previous = null;
// return function() {
// var now = new Date();
// var context = this;
// var args = arguments;
// if (!previous) {
// previous = now;
// }
// var remaining = now - previous;
// if (mustRun && remaining >= mustRun) {
// fn.apply(context, args);
// previous = now;
// }
// }
// }
function throttle(fn) {
var timer = null;
var previous = null;
return function () {
var now = new Date();
var context = this;
var args = arguments;
if (!previous) {
previous = now;
}
var remaining = now - previous;
setTimeout(refresh(fn, remaining, context, args, previous, now));
}
} }
function throttle(fn, mustRun = 500) { function refresh(fn, remaining, context, args, previous, now) {
const timer = null; if (remaining >= 500) {
let previous = null; fn.apply(context, args);
return function() { previous = now;
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;
}
}
} }