Jump to content
Search Community

From() animation after to() animation on one element sequentially

Tony_ton test
Moderator Tag

Go to solution Solved by Rodrigo,

Recommended Posts

Hello, I wanted to ask - is it possible to run the from() animation on an element after the to() animation has worked on it?
Here's an example, first I run the to() animation:

let hideSearchTl = gsap.timeline({
    defaults: {
      ease: 'cubic-1',
      duration: 0.2,
      overwrite: true,
    },
  }),
  elements = $('selector');

hideSearchTl.to(elements, {
  width: 0,
  autoAlpha: 0,
  padding: 0,
});

Then, when performing certain actions, I want to return the element with a from() animation:

hideSearchTl.from(elements, {
  width: 0,
  autoAlpha: 0,
  padding: 0,
});

Using reverse() is not entirely suitable (or I don’t know how to use it) because it “reverses” the easy property. I need easy the same as in to() animation. As far as I understand the complexity in the filled style attribute, I just can’t come up with a solution.

Link to comment
Share on other sites

  • Tony_ton changed the title to From() animation after to() animation on one element sequentially

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

  • Solution

Hi @Tony_ton and welcome to the GSAP Forums!

 

Nothing wrong with the code you're posting, GSAP is doing what's supposed to, the issue stems from not understanding exactly how from() and fromTo() instances work.

 

A from instance takes the element you're tweening from the value you're passing in the config to the value it has before that instance is rendered. So if you have an element with opacity: 1 as is current value and you use a from instance with opacity: 0, GSAP tweens the opacity value from 0 to the value it had before, that is 1. But if you have an element with opacity 0 and you create a from() instance with opacity 0, GSAP takes the element from the value you passed in the config (0) to the value it had before (also 0). GSAP is doing what is supposed to, the problem is that you're telling GSAP tween the opacity value from 0 to 0, so nothing happens visually but GSAP is actually doing that.

 

If you want your element back to opacity 1 then just use another to instance:

hideSearchTl.to(elements, {
  width: someValue,
  autoAlpha: 1,
  padding: someValue,
});

Hopefully this clear things up.

Happy Tweening!

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