I use IntersectionObserver to add a class whenever a element is visible so I can add a fade-in/out animation in my nuxt project. It work well but if I change the page with nuxt-link (or go back to the same page) IntersectionObserver doesn't add the class (so everything is opacity: 0)
Here is the Intersection observer script in my main nuxt layout (default.vue)
mounted() {
const animate = document.querySelectorAll('.animate');
const observer = new IntersectionObserver(entries => {
entries.forEach(entry => {
if (entry.isIntersecting) {
entry.target.classList.add('in')
} else {
entry.target.classList.remove('in')
}
})
}, {
rootMargin: '-15px',
})
animate.forEach(entry => {
observer.observe(entry);
});
},
I would like this code to run everytime I change page with nuxt-link