Jump to content
Search Community

For rotations, how to keep element centred on window resize?

heldrida
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

Posted

Hi,

Using GSAP TimelineMax for a 360 rotate loop animation; On window resize, I can't seem to keep the element centred;  I've got a live link here

If the page is refreshed, the instance when initialised at that point, has the element aligned properly. Only on window is resized, it breaks;

So, I've tried to use the method "kill()" and also setAttribute style and removed any css styles from there; create a new instance, but it breaks, making the animation super slow;

 

I wonder if there's a solution for this. Any tip is appreciated! Thanks!

See the Pen wGBBmm by helderoliveira (@helderoliveira) on CodePen.

Posted

Since you're setting the width as a percent, you can avoid the issue by changing your CSS a bit. Instead of left: 50% and transforming it to center it, instead use left: 25%.

 

I also cleaned up your JS a bit in

See the Pen oxggPz?editors=0110 by Zeaklous (@Zeaklous) on CodePen.

Posted

Hello heldrida. and Welcome to the GreenSock forum

 

To center your element, you dont need transforms to do this. You can just use regular CSS margin: 0 auto; and the element will stay in the center.

 

Remove your transform CSS property and add margin: 0 auto; to your img CSS rule.

 

See the Pen Wwbbgg by jonathan (@jonathan) on CodePen.


 

/* no need to use transforms when you can use a simple margin with auto */
img {
    width: 50%;
    height: auto;
    display: block;
    position: relative;
    left:0;
    /*left: 50%;
    transform: translateX(-50%);
    -webkit-transform: translateX(-50%);*/

    margin: 0 auto; /* add margin auto to your img CSS rule */
  }

Also regarding the kill method.. what you can do is chain the seek() method to bring your timeline back to zero before using the kill() method

 

So you have this inside your resize event handler function callback:

tl.kill();

But add this to the return timeline to zero and then kill() timeline:

tl.seek(0).kill();

Resources:

CSS margin property: https://developer.mozilla.org/en-US/docs/Web/CSS/margin

GSAP seek() method: http://greensock.com/docs/#/HTML5/GSAP/TimelineMax/seek/

 

:)

  • Like 4
Posted

Thanks guys!

So embarassing, I've failed testing this, I was sure centring margin 0 auto didn't work for some reason.

Posted

No worries heldrida ;)

 

If you also had a transform and position offset left property on the same element with margin auto.. then it wont work since it would be overridden by the transform and position offset left properties.

 

Happy Tweening! :)

  • Like 1

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