I'm trying to set up an intersection observer that looks for elements on my page that have a data-original-bg and assigns that as the background image url style, so the images are postloaded.
I've written a function in my React component and loaded it in ComponentDid Mount but i'm getting the error 'cannot assign to rvalue that is not a reference' - on the line
return target.hasAttribute('data-original-bg') ? target.getAttribute('style') += 'background-image:' + backgroundImage : target.getAttribute('style') += 'background-image:' + backgroundImage
My functions:
componentDidMount () {
this.setIndex = window.setInterval(() => {
this.setupObserver()
}, 3000)
}
setupObserver () {
var nodes = document.querySelectorAll('img[data-original],div[data-original-bg]')
window.io = new IntersectionObserver(function (items) {
for (var i = 0; i < items.length; i++) {
var item = items[i]
var target = item.target
if (target.hasAttribute('src')) {
window.io.unobserve(target)
continue
}
if (!item.intersectionRatio) continue
return target.hasAttribute('data-original-bg') ? target.getAttribute('style') += 'background-image:' + backgroundImage : target.getAttribute('style') += 'background-image:' + backgroundImage
if (target.hasAttribute('data-original')) target.setAttribute('src', target.getAttribute('data-original'))
window.io.unobserve(target)
}
})
for (var i = 0; i < nodes.length; i++) { window.io.observe(nodes[i]) }
}