Skip to content

Instantly share code, notes, and snippets.

@BrockReece
Last active January 31, 2018 21:06
Show Gist options
  • Save BrockReece/855f2df4fda3278c377fc65615a78ea4 to your computer and use it in GitHub Desktop.
Save BrockReece/855f2df4fda3278c377fc65615a78ea4 to your computer and use it in GitHub Desktop.
Lazy Load HOC
<template>
<div v-if="outOfView" class="placeholder">&nbsp;</div>
<div v-else>
<slot></slot>
</div>
</template>
<script>
export default {
props: {
options: {
type: Object,
default() {
return {}
},
}
},
data() {
return {
outOfView: true,
}
},
mounted() {
const observer = new IntersectionObserver((entries) => {
entries.forEach(entry => {
if (entry.intersectionRatio > 0) {
this.outOfView = false
this.$emit('in-view')
observer.disconnect()
}
})
}, this.options)
observer.observe(this.$el)
},
}
</script>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment