const DISTANCE = 150
const DURATION = 1000
function isBelowViewport(el) {
const rect = el.getBoundingClientRect()
return rect.top > window.innerHeight
}
const animationMap = new WeakMap()
const ob = new IntersectionObserver((entries) => {
console.log(entries)
for (const entry of entries) {
if (entry.isIntersecting) {
const animation = animationMap.get(entry.target)
animation.play()
ob.unobserve(entry.target)
}
}
})
export default {
mounted(el) {
if (!isBelowViewport(el)) {
return
}
const animation = el.animate(
[
{
transform: `translateY(${DISTANCE}px)`,
opacity: 0.5
},
{
transform: 'translateY(0)',
opacity: 1
}
],
{
duration: DURATION,
easing: 'ease'
}
)
ob.observe(el)
animation.pause()
},
unmounted(el) {
ob.unobserve(el)
}
}