Jump to content
Search Community

Create gsap simple animation for each slide with swiper everytime the slider change

xyzmoonlight test
Moderator Tag

Go to solution Solved by Carl,

Recommended Posts

I tried creating a simple animation for each slide whenever the slider changes. For example, just adding y: 100px to it. However, when I added GSAP onSlideChange, it work just for the first time and the other slides didn't animate; instead, they got stuck at y: 100px.

demo: https://stackblitz.com/edit/swiper-react-saklce?file=src%2FApp.js

<Swiper
  slidesPerView={1}
  onSlideChange={e => {
    gsap.to(".swiper-slide-active .hero-cta", {
      y: "100%"
    })
  }}
  loop
>
  <SwiperSlide>
    <button className="hero-cta">slide 1</button
  </SwiperSlide>
  <SwiperSlide>
    <button className="hero-cta">slide 1</button
  </SwiperSlide>
</Swiper>

my expected, each button get animated everytime the slide change, thanks in advance

Link to comment
Share on other sites

  • xyzmoonlight changed the title to Create gsap simple animation for each slide with swiper everytime the slider change

it's very hard to tell without a minimal demo.

it sounds like you might just be animating it to 100 multiple times which won't show any change if the y is already at 100.

make sure you are increasing the y value by 100 each time.

This can be done using the relative string syntax y:"+=100"

 

gsap.to(something, {y:"+=100"})

 

 

 

  • Like 2
Link to comment
Share on other sites

18 hours ago, Carl said:

it's very hard to tell without a minimal demo.

it sounds like you might just be animating it to 100 multiple times which won't show any change if the y is already at 100.

make sure you are increasing the y value by 100 each time.

This can be done using the relative string syntax y:"+=100"

 

gsap.to(something, {y:"+=100"})

 

 

 

you right, i need to edit my question

[UPDATE]: QUESTION UPDATED

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

57 minutes ago, GSAP Helper said:

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.

 

 

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. 

Thank you, i just add the minimal demo but using stackblitz

Link to comment
Share on other sites

Hi,

 

Your demo is quite out of date. You're using React 17 and Swiper 6. I tried looking into swiper's API to find a way to clear the styles but I can only reach the docs for version 11 (the current one).

 

https://swiperjs.com/swiper-api

 

The issue here is exactly what @Carl mentioned, the issue is that the slide content gets animated and the styles remain like that, so the next time the particular callback is executed (onSlideChange) the styles have the same value you're animating them to, so already is transform: translate(0px, 100%); so nothing happens, but GSAP is actually working, is just that the start and end values are the same so there is no visual change.

 

This is not a GSAP related question but more about how Swiper's API actually works. I'd recommend you to use the latest version of React and Swiper in order to use the latest API and see how you can tackle this.

 

Good luck with your project!

Happy Tweening!

Link to comment
Share on other sites

  • Solution

Thanks for the demo. 

In the future please add new information to new replies. Editing the first post repeatedly makes it difficult for us and others to follow the conversation.

 

I don't know enough about swiper in react to help you with this, particularly how to reference the Swiper inside the onChange callback.

 

However, this bare-bones example shows how to animate the active slide (change it's scale) and animate something inside it (rotate the number div)

 

Hopefully you can find a way to apply something similar to your project

 

See the Pen WNmzezX?editors=0011 by snorkltv (@snorkltv) on CodePen

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