Jump to content
Search Community

Is this way of writing reasonable?

sagexiang test
Moderator Tag

Go to solution Solved by GreenSock,

Recommended Posts

Is this way of writing reasonable? When there are two divs in the panel that need to be animated simultaneously~Is there a more advanced way of writing it?

 

tl.to(".section .panel div:first-child", {
        duration: 1,
        x: "80vw",
        stagger: 1,
     })
   .to(".section .panel div:
last-child", {
        duration: 1,
        x: "-80vw",
        stagger: 1,
    }, "<");

See the Pen yLwKeGM?editors=0010 by Sage-Xiang (@Sage-Xiang) on CodePen

Link to comment
Share on other sites

  • Solution

Sure, that's fine. There's nothing wrong with that code. 

 

An alternate option: 

panels.forEach((panel, index) => {
  tl.to(panel.querySelectorAll("div:first-child, div:last-child"), {
    duration: 1,
    x: (i) => i ? "-80vw" : "80vw"
  }, index);
});

You can also simplify this:

document.addEventListener("DOMContentLoaded", () => {
      const liElements = document.querySelectorAll(".section .panel");

      liElements.forEach((li, index) => {
        li.style.zIndex = liElements.length - index;
      });
});

To this: 

gsap.set(panels, {
  zIndex: (i) => panels.length - i
});

See the Pen PoLRzpE?editors=0010 by GreenSock (@GreenSock) on CodePen

 

Have fun!

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