Hi Everyone,
Im new to Greensock and Nuxt3 and I am trying to implement SmoothScroller into a Nuxt3 site and I can not seem to get it to work when routing to a new page.
Any help would be greatly appreciated.
Thank You!
Here is the code to my GSAP component
<template>
<div id="smooth-wrapper">
<div id="smooth-content">
<slot></slot>
</div>
</div>
</template>
<script setup lang="ts">
import { gsap } from 'gsap'
import { ScrollTrigger } from 'gsap/ScrollTrigger'
import { ScrollSmoother } from 'gsap/ScrollSmoother'
const nuxtApp = useNuxtApp()
const router = useRouter()
// Initialize all GSAP plugins
if (process.client) {
gsap.registerPlugin(ScrollTrigger, ScrollSmoother)
}
// Initialize the smooth scroll plugin
onBeforeMount(() => {
ScrollSmoother.create({
smooth: 2, // Seconds it takes to "catch up" to native scroll position
effects: true, // Look for data-speed and data-lag attributes on elements and animate accordingly
})
})
onMounted(() => {
ScrollSmoother.refresh()
})
// Kill all scroll trigger instances before redirecting
router.beforeEach(() => {
ScrollSmoother.refresh()
for (const i of ScrollTrigger.getAll()) i.kill()
})
// Handle Suspense resolution (e. g., async pages)
nuxtApp.hook('page:finish', () => {
// Refresh the scroll trigger
ScrollTrigger.refresh()
// Refresh scroll smoother
ScrollSmoother.refresh()
})
</script>