Last active
August 5, 2019 18:23
-
-
Save seveves/43385ac809233d311f50124b12244e05 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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} /> | |
); | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Trying to lazy load images. |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
.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