Last active
November 24, 2017 20:47
-
-
Save bob-lee/b71b70cc58b825806a3f1c10c742a248 to your computer and use it in GitHub Desktop.
IntersectionObserver setup 1
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
const INTERSECT_PAGESIZE = 2; | |
const list = document.querySelectorAll('img.image1'); | |
let intersectionObserver; // instance of observer | |
let intersectionRatio; // 1: fully shown, 0.5: half shown, ... | |
let elementToObserve; // img tag being observed | |
let _indexToObserve; // index of img tag being observed | |
Object.defineProperty(this, "indexToObserve", { | |
get: function () { return _indexToObserve; }, | |
set: function (value) { | |
const len = list.length; | |
if (_indexToObserve && (!value || value <= _indexToObserve || value >= len)) { | |
console.error('indexToObserve', value); | |
return; | |
} | |
_indexToObserve = value; | |
// load more images | |
for (let i = 0; i < INTERSECT_PAGESIZE; i++) { | |
const index = _indexToObserve + i; | |
if (index === len) { | |
return // reached page end | |
} | |
// set 'src' to load image | |
const img = list[index]; | |
img.setAttribute('src', img.getAttribute('data-src')); | |
console.log('[' + index + ']'); | |
} | |
} | |
}); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment