Jump to content
GreenSock

Search In
  • More options...
Find results that contain...
Find results in...
DanTheProgrammerMan

Animating REM and EM units unexpected behavior

Recommended Posts

Hello,

 

I am trying to animate using rem units and am encountering unexpected behavior. In short, I'm doing this tween:

 

    TweenLite.to("#blue-circle-1", 1.2, {
      delay: 0.2,
      left: '-1rem',
      top: '4rem',
      ease: Elastic.easeOut
    });
 
But the blue circle actually ends up at top:0px, left:0px instead of the expected left: -1rem, top: 4rem. This is demonstrated in the codepen . Interestingly, if I run the same exact Tween again, after the first failed Tween, it will go to the proper position.
 
In addition, when trying to do the same thing with EM units, as seen at this codepen

See the Pen zGwvPd by anon (@anon) on CodePen

,
we see that the element does reach the correct position of  left: -1rem, top: 4rem, but the animation itself is not fluid, it looks like it has teleported. 
 
And finally, the same animation works perfectly using the jquery animate function, doing what I'd expect it to do. See at this codepen:

See the Pen bdWVKN by anon (@anon) on CodePen

 
Can anyone explain why this is happening, or is this a bug?
 
Thanks!

See the Pen mJmeMa by anon (@anon) on CodePen

Link to post
Share on other sites

This happens with special units because the computed style is in px. If you set the start values before tweening, it should work.

 

See the Pen GJmoJR by osublake (@osublake) on CodePen

  • Like 3
Link to post
Share on other sites

Thanks for the response, that seems to work for EM,

 

but for REM, using the set method sets it to top:0, left:0 still. In this pen I simply set the position to be exactly the same as its starting position, and yet we see it move to top:0px left:0px. What's up with that? 

 

See the Pen pJPgyr by anon (@anon) on CodePen

 

Thanks

Link to post
Share on other sites

I don't know if its even recognizing "rem" as a unit. I think Jack will probably have to answer that, as I really couldn't find an answer by just skimming through the source code.

 

I don't know of an easy fix for you at the moment. You might just have to convert the units manually. For what its worth, I ran into a similar problem before, and raised an issue on GitHub.

Link to post
Share on other sites

Hmm it's too bad. For this particular circumstance I need to go back to using jquery animate (never thought I'd say that) I guess as it is able to properly handle REM units.

Link to post
Share on other sites

The only other thing I can think of is to tween an object and update the css in the onUpdate callback or by using a getter/setter.

 

See the Pen doWGQP by osublake (@osublake) on CodePen

  • Like 2
Link to post
Share on other sites

Sorry about the em/rem hassles. We will consider adding support in a future CSSPlugin update, but this is the first request we've had and we have a number of high priority features in the works right now. 

Link to post
Share on other sites

I second the request for REM support, it's my favorite unit and an absolute joy to use to make responsive websites :)

Link to post
Share on other sites

+1 for full rem support 😀, I just spent an hour and a half trying to figure out why my animation did not look right only to find out it was related to use rem.

Link to post
Share on other sites
7 minutes ago, InTheOne said:

+1 for full rem support 😀, I just spent an hour and a half trying to figure out why my animation did not look right only to find out it was related to use rem.

rem units should be supported for the most part at least: 

See the Pen WNvMyeb by GreenSock (@GreenSock) on CodePen

 

What situation are they not working in? Can you provide a minimal demo?

  • Like 1
Link to post
Share on other sites

Ah, I was using ".5rem" instead of "0.5rem" which was actually causing the problem.

Link to post
Share on other sites
3 minutes ago, InTheOne said:

I was using ".5rem" instead of "0.5rem" which was actually causing the problem.

Hmm. That still works in the demo above. Any idea how to reproduce the error?

Link to post
Share on other sites

Hi, sorry for the slow responce. This issue was that I did not have a 0 in front of my .5rem. When I added that it solved the issue. 

Link to post
Share on other sites
2 minutes ago, InTheOne said:

This issue was that I did not have a 0 in front of my .5rem. When I added that it solved the issue. 

Right but I'm saying that if I leave out the 0 in front in the demo above it doesn't reproduce the issue. So do you know of a way to reproduce the issue in CodePen?

Link to post
Share on other sites

Oh, no that fixed the issue for me in my application. I'm not sure what would be different about my setup to where I needed the 0 for it to work.

  • Thanks 1
Link to post
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.

×