Jump to content
Search Community

Animate a DIV to center of screen with translate3d

trueicecold test
Moderator Tag

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

Hello all,

 

if I have a div positioned at 0,0 (translated), and I want to bring it to the center of the screen, let's say X=320.

if I move it with GSAP, the top left corner is calculated towards 320px, meaning it's not fully centered, and what I really need to do is move it to 320-div.width/2 to get the proper results.

 

I've tried using transform-origin but that doesn't seem to help (also, transform-origin is 50% 50% by deault if I'm not mistaken).

I have lots of elements, and the vast majority of them should be move from their center point.

 

Any quick solution for this using GSAP?

 

Attached a codepen to illustrate the problem

 

Thanks! :)

See the Pen WoJMwq by trueicecold (@trueicecold) on CodePen

Link to comment
Share on other sites

Hi trueicecold,

 

Welcome to the forums!

 

Is there a particular reason as to why you have to use the CSS plugin to tween the translate property?

 

The easiest way I know to achieve what you are explaining is to use the following:

TweenMax.to($(".div1"), 2, {x:window.innerWidth/2, xPercent:-50});

GSAP's x property will use the transform anyway so, that should give you the results you are after.

  • Like 2
Link to comment
Share on other sites

There you go, glad you worked it all out. :)

 

Most of the CSS properties can be written without the css:{} encapsulation, GSAP will recognise them all and work its magic - All in the name of convenience.

 

If you want, you can read a much more detailed explanation of the CSS plugin here: http://greensock.com/docs/#/HTML5/GSAP/Plugins/CSSPlugin/

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