我一直在努力将locomotive-scroll和Gsap实施到 Nuxt 项目中。我相信与 DOM 和 SSR 有关。我检查了从 scrollproxy Gsap 到 Nuxt 和机车的所有文档,但我没有更多的线索。
如果你们有任何建议,非常欢迎帮助。感谢您的时间。
这是错误
TypeError:无法读取未定义的属性“滚动”未
捕获类型错误:_triggers[_i].update 不是函数未捕获类型错误
:无法读取未定义的属性“匹配”
在 plugins/locomotive.js 中创建
import Vue from "vue";
import locomotiveScroll from "locomotive-scroll";
Object.defineProperty(Vue.prototype, "locomotiveScroll", {
value: locomotiveScroll
});
在 nuxt.config.js 中配置
css: [
//smooth scroll
'@/assets/css/scroll.css',
],
plugins: [
{
src: "~/plugins/locomotive.js",
mode: "client"
}
],
在 app.vue 中添加
<script>
import { gsap } from "gsap";
import { ScrollTrigger } from "gsap/ScrollTrigger";
gsap.registerPlugin(ScrollTrigger);
import LocomotiveScroll from "locomotive-scroll"
</script>
在 app.vue 的挂载钩子中触发
this.locoScroll = new LocomotiveScroll({
el: document.querySelector('.smooth-scroll'),
smooth: true,
})
this.locoScroll.on('scroll', ScrollTrigger.update)
ScrollTrigger.scrollerProxy('.smooth-scroll', {
scrollTop(value) {
return arguments.length
? this.locoScroll.scrollTo(value, 0, 0)
: this.locoScroll.scroll.instance.scroll.y
},
getBoundingClientRect() {
return { top: 0, left: 0, width: window.innerWidth, height: window.innerHeight }
},
pinType: document.querySelector('.smooth-scroll').style.transform ? 'transform' : 'fixed',
})
ScrollTrigger.addEventListener('refresh', () => this.locoScroll.update())
ScrollTrigger.refresh()