2018-03-14 07:07:30 +01:00
|
|
|
<div class="lazy-image"
|
2018-05-02 02:05:36 +02:00
|
|
|
style="width: {width}px; height: {height}px; background: {background};">
|
|
|
|
{#if displaySrc}
|
2018-03-14 07:07:30 +01:00
|
|
|
<img
|
2018-05-02 02:05:36 +02:00
|
|
|
class="{hidden ? 'hidden' : ''} {className || ''}"
|
|
|
|
aria-hidden={ariaHidden || ''}
|
|
|
|
alt={alt || ''}
|
|
|
|
title={alt || ''}
|
|
|
|
src={displaySrc}
|
|
|
|
{width}
|
|
|
|
{height}
|
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;
|
|
|
|
}
|
|
|
|
.lazy-image img {
|
|
|
|
transition: opacity 0.2s linear;
|
|
|
|
}
|
|
|
|
</style>
|
|
|
|
<script>
|
2018-03-14 15:24:16 +01:00
|
|
|
import { mark, stop } from '../_utils/marks'
|
|
|
|
|
2018-03-14 07:07:30 +01:00
|
|
|
export default {
|
2018-04-20 06:38:01 +02:00
|
|
|
oncreate () {
|
2018-03-14 15:24:16 +01:00
|
|
|
mark('LazyImage oncreate()')
|
|
|
|
let img = new Image()
|
2018-04-19 18:37:05 +02:00
|
|
|
let { src } = this.get()
|
|
|
|
let { fallback } = this.get()
|
2018-03-14 15:24:16 +01:00
|
|
|
img.onload = () => {
|
|
|
|
requestAnimationFrame(() => {
|
|
|
|
this.set({
|
|
|
|
displaySrc: src,
|
|
|
|
hidden: true
|
|
|
|
})
|
2018-03-14 07:07:30 +01:00
|
|
|
requestAnimationFrame(() => {
|
2018-03-14 15:24:16 +01:00
|
|
|
this.set({hidden: false})
|
2018-03-14 07:07:30 +01:00
|
|
|
})
|
2018-03-14 15:24:16 +01:00
|
|
|
})
|
|
|
|
}
|
|
|
|
img.onerror = () => {
|
|
|
|
this.set({displaySrc: fallback})
|
|
|
|
}
|
|
|
|
img.src = src
|
|
|
|
stop('LazyImage oncreate()')
|
2018-04-30 07:13:41 +02:00
|
|
|
},
|
|
|
|
data: () => ({
|
|
|
|
displaySrc: void 0,
|
|
|
|
hidden: false,
|
|
|
|
ariaHidden: false
|
|
|
|
})
|
2018-03-14 07:07:30 +01:00
|
|
|
}
|
|
|
|
</script>
|