deblan.io-murph/assets/js/app/px-image.js

35 lines
1 KiB
JavaScript
Raw Normal View History

2022-09-10 11:12:14 +02:00
class PxImage {
2022-09-08 10:26:58 +02:00
constructor(w) {
this.window = w
}
2022-09-08 10:26:58 +02:00
init() {
const doc = this.window.document
2022-09-25 20:43:24 +02:00
const images = doc.querySelectorAll('.quick-image img, .card figure img')
2022-09-08 10:26:58 +02:00
for (let i = 0, len = images.length; i < len; i++) {
2023-01-05 19:53:34 +01:00
((image) => {
2022-09-08 10:26:58 +02:00
const source = image.getAttribute('data-src')
2023-01-05 19:53:34 +01:00
const sourceError = image.getAttribute('data-src-error')
const color = image.getAttribute('data-color')
2022-09-08 10:26:58 +02:00
const loader = new Image()
2023-01-05 19:53:34 +01:00
loader.onload = () => {
image.style.background = `${color ? color : null} url(${source})`
2022-09-08 10:26:58 +02:00
image.style.backgroundSize = 'cover'
2022-09-14 11:02:41 +02:00
image.style.backgroundPosition = 'center'
2022-09-08 10:26:58 +02:00
}
2023-01-05 19:53:34 +01:00
loader.onerror = () => {
image.style.background = `${color ? color : null} url('${sourceError}') center center`
}
2022-09-08 10:26:58 +02:00
loader.src = source
})(images[i])
}
}
}
2022-09-10 11:12:14 +02:00
module.exports = PxImage