2018-08-23 06:00:53 +02:00
|
|
|
<div class="lazy-image" style={computedStyle} >
|
2018-05-02 02:05:36 +02:00
|
|
|
{#if displaySrc}
|
2018-03-14 07:07:30 +01:00
|
|
|
<img
|
2018-08-23 06:00:53 +02:00
|
|
|
class={className}
|
|
|
|
aria-hidden={ariaHidden}
|
|
|
|
{alt}
|
|
|
|
{title}
|
2018-05-02 02:05:36 +02:00
|
|
|
{width}
|
|
|
|
{height}
|
2018-08-23 06:00:53 +02:00
|
|
|
src={displaySrc}
|
2018-03-14 07:07:30 +01:00
|
|
|
/>
|
2018-05-02 02:05:36 +02:00
|
|
|
{/if}
|
2018-03-14 07:07:30 +01:00
|
|
|
</div>
|
|
|
|
<style>
|
|
|
|
.lazy-image {
|
|
|
|
overflow: hidden;
|
|
|
|
}
|
|
|
|
</style>
|
|
|
|
<script>
|
2018-03-14 15:24:16 +01:00
|
|
|
import { mark, stop } from '../_utils/marks'
|
2018-08-23 06:00:53 +02:00
|
|
|
import { decodeImage } from '../_utils/decodeImage'
|
2018-03-14 15:24:16 +01:00
|
|
|
|
2018-03-14 07:07:30 +01:00
|
|
|
export default {
|
2018-08-23 06:00:53 +02:00
|
|
|
async oncreate () {
|
2018-03-14 15:24:16 +01:00
|
|
|
mark('LazyImage oncreate()')
|
2018-08-23 06:00:53 +02:00
|
|
|
let { src, fallback } = this.get()
|
|
|
|
try {
|
|
|
|
await decodeImage(src)
|
|
|
|
this.set({ displaySrc: src })
|
|
|
|
} catch (e) {
|
|
|
|
if (fallback) {
|
2018-08-30 06:42:57 +02:00
|
|
|
this.set({ displaySrc: fallback })
|
2018-08-23 06:00:53 +02:00
|
|
|
}
|
2018-03-14 15:24:16 +01:00
|
|
|
}
|
|
|
|
stop('LazyImage oncreate()')
|
2018-04-30 07:13:41 +02:00
|
|
|
},
|
|
|
|
data: () => ({
|
|
|
|
displaySrc: void 0,
|
|
|
|
hidden: false,
|
2018-08-23 06:00:53 +02:00
|
|
|
fallback: void 0,
|
|
|
|
background: '',
|
|
|
|
width: void 0,
|
|
|
|
height: void 0,
|
|
|
|
className: '',
|
|
|
|
ariaHidden: '',
|
|
|
|
alt: '',
|
|
|
|
title: ''
|
|
|
|
}),
|
|
|
|
computed: {
|
2018-08-30 06:42:57 +02:00
|
|
|
computedStyle: ({ width, height, background }) => {
|
2018-08-23 06:00:53 +02:00
|
|
|
return [
|
|
|
|
width && `width: ${width}px;`,
|
|
|
|
height && `height: ${height}px;`,
|
|
|
|
background && `background: ${background};`
|
|
|
|
].filter(Boolean).join('')
|
|
|
|
}
|
|
|
|
}
|
2018-03-14 07:07:30 +01:00
|
|
|
}
|
|
|
|
</script>
|