Jump to content
Search Community

Wrestling with how to keep styles seperate from TweenMax javascript

jstafford test
Moderator Tag

Go to solution Solved by PointC,

Warning: Please note

This thread was started before GSAP 3 was released. Some information, especially the syntax, may be out of date for GSAP 3. Please see the GSAP 3 migration guide and release notes for more information about how to update the code to GSAP 3's syntax. 

Recommended Posts

I have been struggling today in finding a clean way to refactor my GSAP Javascript from CSS styles. I am doing this at the request of coworkers who would like to see the CSS Style seperate from the GSAP Javascript. I  thought I would give it a try with += className and -= className, but the gsap specific styles like autoAlpha, xPercent, yPercent don't work when housed in an external stylesheet. Can anyone suggest a clean way where there is little if no reference of styles in my GSAP javascript while also accounting for "gsap specific styling variables"?

 

To illustrate what I am trying to do, I have two codepens.

 

GSAP Transform with External xPercent and yPercent (the gsap transform doesn't work for xPercent and yPercent here; I thought gsap might be able to parse this and still use the xPercent and yPercent)
 
GSAP Transform with xPercent and yPercent in TweenMax call (traditional way: works, block is centered)

See the Pen xwmqwp by jstafford (@jstafford) on CodePen

See the Pen WQLpQv by jstafford (@jstafford) on CodePen

Link to comment
Share on other sites

Also - if you don't want to use GSAP's autoAlpha in your Tweens and are just trying to tween between classes, you can just use opacity and visibility since autoAlpha is a combination of those two properties.
 
Per the docs:
 

Identical to opacity except that when the value hits 0 the visibility property will be set to "hidden" in order to improve browser rendering performance and prevent clicks/interactivity on the target. When the value is anything other than 0, visibility will be set to "inherit". It is not set to "visible" in order to honor inheritance (imagine the parent element is hidden - setting the child to visible explicitly would cause it to appear when that's probably not what was intended). And for convenience, if the element's visibility is initially set to"hidden" and opacity is 1, it will assume opacity should also start at 0. This makes it simple to start things out on your page as invisible (set your css visibility:hidden) and then fade them in whenever you want.

  • Like 2
Link to comment
Share on other sites

Thanks PointC. I was tying my hands behind my back thinking that gsap only recognized the xPercent and yPercent for transforming something relative to its own width and height. I had read this about autoalpha, but seeing it again from you again has reassured me to just try it with opacity and visibility in my css. Making this autoAlpha more mysterious than it needs to be. Thank you sir.

  • Like 2
Link to comment
Share on other sites

Yep prefixes can be hassle ;)

 

If you really didn't want to worry about browser vendor prefixes you could also just use the GSAP set() method. Allow GSAP to add the right CSS property with or without prefix for you.

 

And add it before all your other custom JS, and before your window load event

 

TweenMax.set("#block",{x:"-50%",y:"-50%"});

 

But it doesn't hurt, just to have it in your CSS stylesheet like PointC suggested. Whatever works best for your project :)

Link to comment
Share on other sites

You really should not be mixing GSAP with CSS transitions, since they will be both fighting over animating the elements. Its best to use either or in regards to CSS transitions, CSS animations, and CSS keyframes.

 

CSS transitions interpolate the values and then you are asking GSAP to animate those values as well. Creating a tug of war or just buggy unexpected behavior.

 

According to the spec .. CSS transition property is not an animatable property. Since it is what helps animate other CSS properties, But itself can not be animated.

I would remove any CSS transitions on elements you are animating with GSAP, and / or set the CSS transition property to none on those elements.

 

So when it comes to CSS transitions just use it or use GSAP. I would use GSAP since it can animate, even in IE6. So basically GSAP is your best choice for cross-browser animation in the web browser!

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