lazy-load-graphs.js 1.6 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344
  1. document.addEventListener('DOMContentLoaded', function() {
  2. var lazyGraphs = [].slice.call(document.querySelectorAll('[lazy]'));
  3. var active = false;
  4. var lazyLoad = function() {
  5. if (active === false) {
  6. active = true;
  7. setTimeout(function() {
  8. lazyGraphs.forEach(function(lazyGraph) {
  9. if (
  10. lazyGraph.getBoundingClientRect().top <= window.innerHeight &&
  11. lazyGraph.getBoundingClientRect().bottom >= 0 &&
  12. getComputedStyle(lazyGraph).display !== 'none'
  13. ) {
  14. lazyGraph.data = lazyGraph.getAttribute('lazy');
  15. lazyGraph.removeAttribute('lazy');
  16. lazyGraphs = lazyGraphs.filter(function(image) { return image !== lazyGraph});
  17. if (lazyGraphs.length === 0) {
  18. document.removeEventListener('scroll', lazyLoad);
  19. window.removeEventListener('resize', lazyLoad);
  20. window.removeEventListener('orientationchange', lazyLoad);
  21. }
  22. }
  23. });
  24. active = false;
  25. }, 200);
  26. }
  27. };
  28. // initial load
  29. lazyLoad();
  30. var container = document.querySelector('.container-fluid.modules');
  31. if (container) {
  32. container.addEventListener('scroll', lazyLoad);
  33. window.addEventListener('resize', lazyLoad);
  34. window.addEventListener('orientationchange', lazyLoad);
  35. }
  36. });