mirror of
https://git.hmsn.ink/kospo/svcm/oa.git
synced 2026-03-20 13:13:41 +09:00
33 lines
654 B
TypeScript
33 lines
654 B
TypeScript
import type { Directive, DirectiveHook } from 'vue'
|
|
|
|
const onUpdate: DirectiveHook = (el: HTMLElement, bindings) => {
|
|
const src = bindings.value.src
|
|
const placeholder = bindings.value.placeholder
|
|
|
|
if (src) {
|
|
const image = new Image()
|
|
|
|
if (placeholder) {
|
|
image.onerror = () => {
|
|
image.onerror = null
|
|
el.style.backgroundImage = `url(${placeholder})`
|
|
}
|
|
}
|
|
|
|
image.onload = () => {
|
|
image.onload = null
|
|
el.style.backgroundImage = `url(${src})`
|
|
}
|
|
|
|
image.src = src
|
|
}
|
|
}
|
|
|
|
export const vBackground: Directive = {
|
|
getSSRProps() {
|
|
return {}
|
|
},
|
|
updated: onUpdate,
|
|
mounted: onUpdate,
|
|
}
|