deblan.io-murph/assets/js/app/px-image.js
Simon Vieille 49de74ec24
Some checks failed
ci/woodpecker/push/woodpecker Pipeline was successful
ci/woodpecker/deployment/woodpecker Pipeline failed
add posts as card on homepage
2023-01-05 19:53:34 +01:00

35 lines
1 KiB
JavaScript

class PxImage {
constructor(w) {
this.window = w
}
init() {
const doc = this.window.document
const images = doc.querySelectorAll('.quick-image img, .card figure img')
for (let i = 0, len = images.length; i < len; i++) {
((image) => {
const source = image.getAttribute('data-src')
const sourceError = image.getAttribute('data-src-error')
const color = image.getAttribute('data-color')
const loader = new Image()
loader.onload = () => {
image.style.background = `${color ? color : null} url(${source})`
image.style.backgroundSize = 'cover'
image.style.backgroundPosition = 'center'
}
loader.onerror = () => {
image.style.background = `${color ? color : null} url('${sourceError}') center center`
}
loader.src = source
})(images[i])
}
}
}
module.exports = PxImage