function filterDom(selector) {
return Array.from(document.querySelectorAll(selector));
}
//事件观察者
var observer = new IntersectionObserver(observerCall,{
root: null,
rootMargin : '0px 0px 20px 0px',
threshold:[0, 1]
});
function observerCall(changes) {
changes.forEach(function(change) {
setTimeout(function(){
if(change.intersectionRatio > 0){
var container = change.target;
var dataSrc = container.getAttribute('data-src');
container.setAttribute('src', dataSrc)
observer.unobserve(container);
}
}, 100);
});
}
//过滤元素
filterDom('.lazy-loaded').forEach(function (item) {
observer.observe(item);
});