index.js 478 B

12345678910111213141516
  1. function loadingFailed(imgEl) {
  2. return imgEl.complete && imgEl.naturalWidth === 0
  3. }
  4. document.querySelectorAll('[data-ol-fallback-image]').forEach(imgEl => {
  5. function showFallback() {
  6. imgEl.src = imgEl.getAttribute('data-ol-fallback-image')
  7. }
  8. if (loadingFailed(imgEl)) {
  9. // The image loading failed before the sprinkle ran.
  10. showFallback()
  11. } else {
  12. // The image loading might fail in the future.
  13. imgEl.addEventListener('error', showFallback)
  14. }
  15. })