Last active
January 31, 2018 21:06
-
-
Save BrockReece/855f2df4fda3278c377fc65615a78ea4 to your computer and use it in GitHub Desktop.
Lazy Load HOC
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 v-if="outOfView" class="placeholder"> </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