I am currently trying to implement lazy loading using the IntersectionObserver in my angularjs app.
But when I scroll up and down it doesn't always call the callback function of the observer.
My directive looks like this:
var app = angular.module("test", []);
app.directive("inViewport", function() {
return {
restrict: "A",
link: function(scope, element, attrs) {
const observer = new IntersectionObserver(callback);
const img = angular.element(element)[0];
observer.observe(img);
function callback(changes) {
changes.forEach(change => {
change.target.classList.toggle(
"visible",
change.intersectionRatio > 0
);
});
}
}
};
});
See this pen for a demo.