123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869 |
- function isInSight(el) {
- var bound = el.getBoundingClientRect();
- var clientHeight = window.innerHeight;
-
-
- return bound.top <= clientHeight + 100;
- }
- var index = 0;
- function checkImgs() {
- var imgs = document.querySelectorAll('.my-photo');
- for (var i = index; i < imgs.length; i++) {
- if (isInSight(imgs[i])) {
- loadImg(imgs[i]);
- index = i;
- }
- }
- }
- function loadImg(el) {
- var loaded = el.getAttribute("loaded");
- if (!Boolean(loaded)) {
- var source = el.getAttribute("data-src");
- el.setAttribute("loaded", true);
- el.src = source;
- }
- }
- 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 refresh(fn, remaining, context, args, previous, now) {
- if (remaining >= 500) {
- fn.apply(context, args);
- previous = now;
- }
- }
|