Initial commit
This commit is contained in:
24
app/plugins/visibility-directive.ts
Normal file
24
app/plugins/visibility-directive.ts
Normal file
@@ -0,0 +1,24 @@
|
||||
export default defineNuxtPlugin((nuxtApp) => {
|
||||
/**
|
||||
* Directive that calls a callback when an element gets visible in the viewport.
|
||||
*/
|
||||
nuxtApp.vueApp.directive('on-visible', {
|
||||
mounted(el, binding) {
|
||||
const callback = binding.value
|
||||
|
||||
const observer = new IntersectionObserver(
|
||||
(entries) => {
|
||||
entries.forEach((entry) => {
|
||||
if (entry.isIntersecting) {
|
||||
callback(entry)
|
||||
observer.disconnect()
|
||||
}
|
||||
})
|
||||
},
|
||||
{ threshold: 0.5 }
|
||||
)
|
||||
|
||||
observer.observe(el)
|
||||
}
|
||||
})
|
||||
})
|
||||
Reference in New Issue
Block a user