Jump to content
Search Community

GSAP scrollTrigger and Nuxt Links

paddywinc test
Moderator Tag

Recommended Posts

Hi All,

 

So I'm building something quite basic in Nuxt and using scrollTrigger to toggle a class on certain elements when they enter the viewport. 

 

I have seen some other threads on here about scrollTrigger not refreshing when using Nuxt Link and it looks like I need to add scrollTrigger.refresh() in here somewhere. 

 

This all works perfectly when I refresh the page, but when I navigate to another page, scrollTrigger doesn't work. 

 

I am calling GSAP via a plugin in nuxt/config like so:

 

   plugins: [
       { src: '~/plugins/gsap', ssr: false },
  ],

 

And my plugin file looks like this. 

 

import { gsap } from "gsap";
import { ScrollTrigger } from "gsap/ScrollTrigger";
 
if (process.client) {
    gsap.registerPlugin(ScrollTrigger);
}

console.log('gsap installed');


const sections = document.querySelectorAll('.window, .gummy');

    sections.forEach((section, index) => {
      gsap.to(section, {autoAlpha: 1,
        scrollTrigger: {
          trigger: section,
           start: 'top bottom',
            // end: 'bottom top',
          toggleActions: 'play none none reverse',
          // markers: true
        }
      });
      
      ScrollTrigger.create({
        trigger: section,
        id: index+1,
        start: 'top bottom',
        end: 'bottom top',
        toggleActions: 'play reverse none reverse',
        toggleClass: {targets: section, className: "is-active"},
        once:true
        // markers: true
      })
      
    })

 

 

Where can I add scrollTrigger().refresh or kill in the above code?

 

Thanks

Paddy

Link to comment
Share on other sites

Nuxt development is widely different than WordPress. You're going to need to learn how to use Vue's lifecycle events to do such stuff. Animations/ScrollTriggers should be created in mounted and killed in beforeDestroy.

 

Here's a simple starter sandbox for Nuxt.

 

https://codesandbox.io/s/gsap-nuxt-starter-r5lkg?file=/pages/index.vue

 

ScrollTrigger might be off on page transitions if your pages have transition on them, so you may need to tap into Vue's JavaScript hooks for transitions.

 

 

Link to comment
Share on other sites

I totally agree! Been quite the learning experience for me. 

 

I do have page transitions currently, I did get this working briefly by adding this 

 

destroyed (); {
            const scrollTriggersInstances = ScrollTrigger.getAll()
            scrollTriggersInstances.forEach(el => {
                if (el && el.kill) {
                    el.kill(false)
                }
            })
            this.animation.kill()
        }

 

But it stopped the page transitions

Link to comment
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
  • Recently Browsing   0 members

    • No registered users viewing this page.
×
×
  • Create New...