Jump to content
Search Community

Nuxt with GSAP, Initialise after an image is loaded

miguelst test
Moderator Tag

Go to solution Solved by Rodrigo,

Recommended Posts

I'm wondering if I'm doing something wrong, I followed the examples on StackBlitz (thanks for that), And I got GSAP working inside my Vue/Nuxt component in onMounted.

 

However I'm not sure, how to start a transition when an image is loaded.

 

The following code is working, however if I paste the onMounted code inside the imageLoaded callback, it fails. Is there a certain method to start the transition once an image is loaded? I have 4 images that are being loaded like that. Thanks!

const imageLoaded = (el) => {
  console.log('loaded ', el)
}

onMounted(() => {
  ctx.value = gsap.context((self) => {
    const tl = gsap.timeline({
          defaults: {
            duration: 1,
            ease: "power4.out",
          }
        }
    )

    tl.from('img', {opacity: 0, y: 50}, 0)
  }, main.value)
});

onUnmounted(() => {
  ctx.value.revert()
});
Link to comment
Share on other sites

Without a minimal demo, it's very difficult to troubleshoot; the issue could be caused by CSS, markup, a third party library, a 3rd party script, etc. Would you please provide a very simple CodePen or Stackblitz that illustrates the issue? 

 

Please don't include your whole project. Just some colored <div> elements and the GSAP code is best. See if you can recreate the issue with as few dependancies as possible. Start minimal and then incrementally add code bit by bit until it breaks. Usually people solve their own issues during this process! If not, at least we have a reduced test case which greatly increases your chances of getting a relevant answer.

 

See the Pen aYYOdN by GreenSock (@GreenSock) on CodePen

that loads all the plugins. Just click "fork" at the bottom right and make your minimal demo

 

Using a framework/library like React, Vue, Next, etc.? 

CodePen isn't always ideal for these tools, so here are some Stackblitz starter templates that you can fork and import the gsap-trial NPM package for using any of the bonus plugins: 

 

Please share the StackBlitz link directly to the file in question (where you've put the GSAP code) so we don't need to hunt through all the files. 

 

Once we see an isolated demo, we'll do our best to jump in and help with your GSAP-specific questions. 

Link to comment
Share on other sites

I managed to get it working using the following code, it's my first Nuxt + GSAP implementation, so feel free to correct me if I'm doing something wrong here

 

const main = ref()
const imageRefs = ref<Array<any>>([])
const ctx = ref()

onMounted(() => {
  ctx.value = gsap.context((self) => {
    const tl = gsap.timeline({
      defaults: {
        duration: 1,
        ease: "power4.out",
      }
    })
    tl.from(self.selector!('h1'), {
      autoAlpha: 0,
      y: 75,
    })
  }, main.value)
})

watchEffect(() => {
  // Wait for all 4 imageRefs to be present
  if (imageRefs.value.length === 4) {
    // Reuse the main context
    ctx.value.add((self: any) => {
      const tl = gsap.timeline({
            defaults: {
              duration: 2,
              ease: "power4.out",
            }
          }
      )
      tl.from(self.selector!('img'), {
        autoAlpha: 0,
        y: 75,
        stagger: .1,
        delay: .5
      })
    })
  }
})
Link to comment
Share on other sites

  • Solution

Hi,

 

The only suggestion I have is that there is no need to make the GSAP Context instance a reactive property. Everytime you update it it'll trigger a re-render and Vue will run it's reactive data callbacks in order to see if something else has to be updated, which it doesn't, since there is nothing in your app that depends on the GSAP Context being updated, so just make it a variable and be done with it:

let ctx;

onMounted(() => {
  ctx = gsap.context(() => {
    ...
  });
});

onUnmounted(() => {
  ctx && ctx.revert();
});

Hopefully this helps.

Happy Tweening!

Link to comment
Share on other sites

21 minutes ago, Rodrigo said:

Hi,

 

The only suggestion I have is that there is no need to make the GSAP Context instance a reactive property. Everytime you update it it'll trigger a re-render and Vue will run it's reactive data callbacks in order to see if something else has to be updated, which it doesn't, since there is nothing in your app that depends on the GSAP Context being updated, so just make it a variable and be done with it:

let ctx;

onMounted(() => {
  ctx = gsap.context(() => {
    ...
  });
});

onUnmounted(() => {
  ctx && ctx.revert();
});

Hopefully this helps.

Happy Tweening!

Thanks @Rodrigo that's a very good point!

  • Like 1
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...