Created
April 23, 2020 15:12
-
-
Save mitevpi/e7bdd6cfbdf47632ab0375a059cd0d5c to your computer and use it in GitHub Desktop.
Vue.js 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
<template> | |
<div :style="containerStyle" class="div-image-container"> | |
<transition name="fade"> | |
<img | |
v-if="source !== ''" | |
v-show="loaded" | |
:height="height" | |
:width="width" | |
:src="source" | |
alt="image" | |
@load="onLoad" | |
> | |
</transition> | |
</div> | |
</template> | |
<script> | |
export default { | |
name: "LazyImage", | |
components: {}, | |
props: { | |
source: String, | |
height: String, | |
width: String | |
}, | |
data: () => ({ | |
loaded: false | |
}), | |
computed: { | |
containerStyle() { | |
return { | |
"--width": `${this.width}px`, | |
"--height": `${this.height}px` | |
}; | |
} | |
}, | |
methods: { | |
onLoad() { | |
// console.log("IMAGE LOADED"); | |
this.loaded = true; | |
} | |
} | |
}; | |
</script> | |
<style scoped lang="scss"> | |
.div-image-container { | |
width: var(--width); | |
height: var(--height); | |
} | |
</style> |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment