【发布时间】:2021-04-10 09:35:41
【问题描述】:
只要元素可见,我就使用IntersectionObserver 添加一个类,这样我就可以在我的 nuxt 项目中添加淡入/淡出动画。它运行良好,但如果我用nuxt-link 更改页面(或返回同一页面)IntersectionObserver 不会添加类(所以一切都是opacity: 0)
这是我的主要 nuxt 布局 (default.vue) 中的 Intersection 观察者脚本
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);
});
},
我希望每次使用 nuxt-link 更改页面时都运行此代码
【问题讨论】:
标签: vue.js nuxt.js nuxtjs intersection-observer