Skip to content

Instantly share code, notes, and snippets.

@BrockReece
Created September 1, 2019 13:02
Show Gist options
  • Save BrockReece/5696cbc411fcbfa6b12d163b20974282 to your computer and use it in GitHub Desktop.
Save BrockReece/5696cbc411fcbfa6b12d163b20974282 to your computer and use it in GitHub Desktop.
import { onMounted, onUnmounted, ref } from "@vue/composition-api";
export function useOnScreen() {
let isOnScreen = ref(false);
const observer = new IntersectionObserver(entries => {
isOnScreen.value = entries.some(entry => entry.intersectionRatio > 0);
});
onMounted(function() {
observer.observe(this.$el);
});
onUnmounted(function(vm) {
observer.unobserve(this.$el);
});
return {
isOnScreen,
observer
};
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment