Jump to content
Search Community

Is it possible to 'wrap' property IDs?

elegantseagulls test
Moderator Tag

Recommended Posts

I'm trying to stagger tween several CSS Custom properties, and was wondering if it's possible to do this without breaking it into several .to() tweens. eg: gsap.to('body', {gsap.utils.wrap(['--bg-1','--bg-2']): 'blue', stagger: .1 })

 

CodePen has desired result commented out, for clarity.

See the Pen MWooPPP?editors=0110 by ryan_labar (@ryan_labar) on CodePen

Link to comment
Share on other sites

@_Greg _,

Yeah, I thought about keyframes, but was really looking to use the power of stagger for this, as there are more than just two custom properties, and was looking to possibly distribute that stagger on an ease curve.

 

@OSUblake That may work! Yeah, I may have to get a bit creative with this one. Was trying to use CSS custom properties to avoid some conflicting tween/timeline issues.

Link to comment
Share on other sites

Here's a helper function that may prove useful:

function staggerProps(targets, vars) {
  let tl = gsap.timeline(),
      stagger = (vars.stagger || 0.1),
      nonStaggeredVars = {},
      props = [],
      values = [];
  for (let p in vars) {
    if (~p.indexOf(",")) {
      props.push(p.split(","));
      values.push(gsap.utils.wrap(typeof(vars[p]) === "string" ? [vars[p]] : gsap.utils.toArray(vars[p])));
    } else if (p !== "stagger") {
      nonStaggeredVars[p] = vars[p];
    }
  }
  props.forEach((a, i) => {
    let getValue = values[i];
    a.forEach((p, i) => tl.to(targets, {[p]: getValue(i), ...nonStaggeredVars}, i * stagger));
  });
  return tl;
}

That way, you can use it like a gsap.to() but it'll return a timeline with things staggered. Just use a comma-delimited list of property names as the property name of the vars object like this: 

staggerProps("body", {"--bg-1,--bg-2": "blue", stagger: 0.5, duration: 1});

It also lets you define multiple end values in an Array if you need that, and you can do multiple sets of variables too: 

staggerProps("body", {
  "--bg-1,--bg-2": "blue", // single value
  "--other-1,--other-2": [100,200], // Array of values
  stagger: 0.5, 
  duration: 1
});

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

 

Is that what you were looking for @elegantseagulls

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