sparanoid/lightense-images

Lightense on lazy loaded images

bkistyping opened this issue · 0 comments

I'm trying to use Lightense on a page where some images are lazy loaded when a toggle is opened. I have a window.addEventListener('load' set up on the page generally, and am calling Lightense() on the images when they are loaded in.

The issue I'm seeing is that multiple instances of Lightense() break because the !d.querySelector('.lightense-backdrop') check causes config.container to be undefined:

Uncaught TypeError: Cannot read property 'removeEventListener' of undefined
    at unbindEvents (lightense.js:366)
    at removeViewer (lightense.js:299)
    at init (lightense.js:337)
    at HTMLImageElement.<anonymous> (lightense.js:193)

My solution is to set config.container to the already existing backdrop:

if (!d.querySelector('.lightense-backdrop')) {
  ...
} else {
  config.container = d.querySelector('.lightense-backdrop');
}

This fix works great, but I wanted to call it out and ask if there was a better solution to this general problem than what I'm doing. Thanks!