Skip to content

Instantly share code, notes, and snippets.

@seveves
Last active August 5, 2019 18:23
Show Gist options
  • Save seveves/43385ac809233d311f50124b12244e05 to your computer and use it in GitHub Desktop.
Save seveves/43385ac809233d311f50124b12244e05 to your computer and use it in GitHub Desktop.
import { h, Component } from 'preact';
import style from './style';
export default class LazyImage extends Component {
render({ image }) {
let style = { backgroundImage: 'url(' + image.base64 + ')' };
return (
<div class="lazy-image" style={style}>
<InnerImage imageUrl={image.imageUrl} />
</div>
);
}
}
class InnerImage extends Component {
setOpacity() {
var promise = new Promise((resolve, reject) => {
setTimeout(function() {
resolve();
}, 100);
}).then(() => {
this.setState({ style: { opacity: 1 } });
});
}
render({ imageUrl }, { imgSource='', my='', style='' }) {
if (imgSource === '') {
fetch(imageUrl)
.then(response => response.blob())
.then(blob => window.URL.createObjectURL(blob))
.then(imgSource => {
this.setState({ imgSource });
this.setOpacity();
});
}
return (
<img class="fade-in" src={imgSource} style={style} />
);
}
}
Trying to lazy load images.
.lazy-image {
background-repeat: no-repeat;
background-size: 300px auto;
width: 300px;
float: left;
margin-left: 5px;
}
.lazy-image img {
opacity: 0;
width: 300px;
}
.fade-in {
transition: opacity 1500ms;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment