pinafore/src/routes/_components/NonAutoplayImg.html

66 lines
1.6 KiB
HTML
Raw Normal View History

<img
class={computedClass}
aria-hidden={ariaHidden}
{alt}
{title}
{width}
{height}
src={displaySrc}
on:mouseover="onMouseOver(event)"
2018-08-31 23:35:26 +00:00
ref:node
/>
<style>
.non-autoplay-zoom-in {
cursor: zoom-in;
}
.non-autoplay-zoom-in.is-link {
cursor: pointer;
}
</style>
<script>
import { mouseover } from '../_utils/events'
import { decodeImage } from '../_utils/decodeImage'
import { classname } from '../_utils/classname'
import { ONE_TRANSPARENT_PIXEL } from '../_static/media'
export default {
async oncreate () {
2019-08-03 20:49:37 +00:00
const { currentSrc } = this.get()
try {
2019-08-03 20:49:37 +00:00
const image = new Image()
image.src = currentSrc
await decodeImage(image)
this.set({ loaded: true })
this.fire('imgLoad')
} catch (e) {
this.fire('imgLoadError', e)
}
},
methods: {
2018-04-20 04:38:01 +00:00
onMouseOver (mouseOver) {
2018-08-31 23:35:26 +00:00
this.set({ mouseOver })
}
},
events: {
mouseover
},
data: () => ({
alt: '',
2018-08-31 23:35:26 +00:00
title: '',
mouseOver: false,
loaded: false,
ariaHidden: false
}),
computed: {
computedClass: ({ className, src, staticSrc, isLink }) => (classname(
className,
src !== staticSrc && 'non-autoplay-zoom-in',
isLink && 'is-link'
2018-08-31 23:35:26 +00:00
)),
currentSrc: ({ mouseOver, src, staticSrc }) => mouseOver ? src : staticSrc,
// using a transparent pixel as placeholder ensures broken images don't have wrong sizes
displaySrc: ({ loaded, currentSrc }) => loaded ? currentSrc : ONE_TRANSPARENT_PIXEL
}
}
</script>