Skip to content

Instantly share code, notes, and snippets.

@mitevpi
Created April 23, 2020 15:12
Show Gist options
  • Save mitevpi/e7bdd6cfbdf47632ab0375a059cd0d5c to your computer and use it in GitHub Desktop.
Save mitevpi/e7bdd6cfbdf47632ab0375a059cd0d5c to your computer and use it in GitHub Desktop.
Vue.js Lazy-Load Images
<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