Jump to content
Search Community

Remove attributes instead of setting to 'auto'

Gary test
Moderator Tag

Go to solution Solved by Carl,

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

The attached codepen centers text on the screen, and by default auto-sizes the div to the text content.

 

It auto-sizes because white-space is set to nowrap in the .text css class.

 

I can then animate to a width using GSAP, by clicking the 500/800 buttons.

 

To undo the width and go back to auto-size, I need to set the width to "auto" and the white-space back to "nowrap".  However, this leaves behind width and white-space attributes in the element that aren't needed, and weren't there originally.

 

 

It works fine in the codepen, but I'd prefer to remove the unneeded attributes instead because there will be many text elements in play.  I tried setting them to undefined but that didn't work.  Is there a way to do that using GSAP or do I need to manipulate the DOM directly? I'm concerned that manipulating the DOM directly will leave the GSAP tween state misaligned with the DOM.

 

Bonus question:  Any thoughts on how to animate back to the auto-sized width?

 

 

Thanks,

Gary

 

See the Pen myqXRX?editors=011 by GaryC (@GaryC) on CodePen

Link to comment
Share on other sites

  • Solution

you can use clearProps like so

 

$("#sizeNone").on("click", function() {
 //will revmoe the in-line style for white-space and width
  TweenLite.set(".text", {clearProps:"whiteSpace, width"})
})

also, clearProps:"all" will remove all in-line properties (in case you don't want to list them all)

 

 

you can try something like this for the bonus question: 

http://codepen.io/GreenSock/pen/pvdaXg?editors=011

 

I'm placing an autoWidth attribute on the .text element so that it sort of knows its normal width.

  • Like 2
Link to comment
Share on other sites

autoWidth was just a name of an attribute that i'm tacking onto the DOM element. I could have used attr("theBestNumberInTheWorld").

 

Take a peak in dev tools at the .text element and you will see something like:

 

 

<div class="text" data-autowidth="978.777777671814" style="transform: translate(-50%, -50%) matrix(1, 0, 0, 1, 0, 0);">The quick brown fox jumped over the lazy dog</div>

 

I added the data- prefix to the CodePen to make the intentions more clear.

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