What is the rationale behind storing animation settings in two distinct object literals within TweenMax?

While going through this Codepen, I came across the following code:

var tmax_options = {
  repeat: -1,
  yoyo: true
};

var tmax_tl          = new TimelineMax(tmax_options),
    tween_options_to = {
      css: {
        transform: 'scale(0)',
        transformOrigin: 'center center'
      },
      ease: Cubic.easeInOut,
      force3D: true
    };

// Last Argument is Position Timing.
// Use this argument to stagger the visibility of surrounding circles
tmax_tl.to($('svg > circle:nth-of-type(1)'), 1, tween_options_to, 0)
       .to($('svg > circle:nth-of-type(2)'), 1, tween_options_to, 0)
       .to($('svg > circle:nth-of-type(3)'), 1, tween_options_to, 0)
       .to($('svg > circle:nth-of-type(4)'), 1, tween_options_to, 0)
       .to($('svg > circle:nth-of-type(5)'), 1, tween_options_to, 0)
       .to($('svg > circle:nth-of-type(6)'), 1, tween_options_to, 0)
       .to($('svg > circle:nth-of-type(7)'), 1, tween_options_to, 0)

Although I understand most of the JS code, I have one specific question about the unusual syntax - the options are assigned from two separate object literals like this:

var tmax_options = {
  repeat: -1,
  yoyo: true
};

This is followed by:

  tween_options_to = {
      css: {
        transform: 'scale(0)',
        transformOrigin: 'center center'
      },
      ease: Cubic.easeInOut,
      force3D: true
    };

I am wondering why the animation settings are loaded separately. Is there a reason these settings couldn't be included in a single object literal? I wanted to know if there is any specific rationale for having the settings split into two separate object literals.

Answer №1

When using GSAP, the css:{} object is not required but can be used for convenience. You have the option to include all your CSS properties in one object without it. The use of css:{} provides a slight performance boost as GSAP doesn't have to wrap your CSS properties for you. However, starting from GSAP version 1.18.0, it is no longer necessary.

If you decide to utilize the css:{} object wrapper for your CSS properties, make sure to place force3D: true inside this object since it belongs to the GSAP CSSPlugin.

tween_options_to = {
  css: {
    transform: 'scale(0)',
    transformOrigin: 'center center',
    force3D: true
  },
  ease: Cubic.easeInOut,
};

Alternatively, you can simply combine everything into a single object like this:

tween_options_to = {
  transform: 'scale(0)',
  transformOrigin: 'center center',
  force3D: true,
  ease: Cubic.easeInOut,
};

Another approach involves using the scale property within the transform declaration as GSAP recognizes it as part of CSS transforms.

tween_options_to = {
  scale: 0,
  transformOrigin: 'center center',
  force3D: true,
  ease: Cubic.easeInOut,
};

For CSS transform equivalents in GSAP:

  • x = translateX()
  • y = translateY()
  • z = translateZ()
  • rotation = rotateZ() or rotate()
  • rotationX = rotateX()
  • rotationY = rotateY()
  • skewX = skewX()
  • skewY = skewY()

Refer to the GSAP CSSPlugin Docs for further details:

Note regarding the css:{} wrapper:

In the past, CSS-specific properties had to be wrapped in their own object when passing them in, like TweenLite.to(element, 1, {css:{left:"100px", top:"50px"}}); however, with the introduction of version 1.8.0, TweenLite and TweenMax now automatically handle this for DOM element targets. This means that if the target is a DOM element, the css object is created for you, grouping any non-reserved properties together. While both styles are acceptable, the newer syntax omits the css:{} object for brevity.

For example:

// Starting from 1.8.0, these lines yield the same results:
TweenLite.to(element, 1, {top:"20px", backgroundColor:"#FF0000", ease:Power2.easeOut});
// Longer syntax for comparison:
TweenLite.to(element, 1, {css:{top:"20px", backgroundColor:"#FF0000"}, ease:Power2.easeOut});

Similar questions

If you have not found the answer to your question or you are interested in this topic, then look at other similar questions below or use the search

How to choose `optgroup` in Vue 1.x

In previous iterations of vue.js, developers had the ability to generate a dynamic select list utilizing optgroups similar to the example found here. In the latest versions of vue, the documentation suggests using v-for within the options instead of optgr ...

How can I reset an ng-model value of a range input in AngularJS?

Utilizing a filter with two inputs that share the same ng-model concurrently: <!-- Number input --> <input type="number" maxlength="5" step="0.25" min="-10" max="10" placeholder="0.00" ng-model="sphere"> <!-- Range Input --> <input ...

Building a custom WebRTC Unity to JavaScript client for one-way video streaming within the local network

In this project, Unity is used to capture video and it communicates with JavaScript via WebRTC. Despite successful communication between the two clients, the JavaScript client is unable to display any video from the Unity client. The issue of no video out ...

Guide to incorporating jsTree into Express applications

I just set up jsTree in my project's bower_components folder (following the steps outlined in this post). Here's how my jade template is structured: doctype html html head title= title script(src='/bower_components/jstre ...

What is the best way to ensure the div expands to the width of the screen as it is being

What changes should I make to the CSS in order to adjust the div class="breadcrumbs" when the table width exceeds the screen length and the background does not extend all the way to the end? See the screenshot for reference. I want the dark blue bar to str ...

JavaScript struggles when dealing with dynamically loaded tables

I am currently working on integrating the javascript widget from addtocalendar.com into my website. The code example provided by them is displayed below. Everything functions as expected when I place it on a regular page. However, I am facing an issue wher ...

Issue encountered while attempting to save the date to a json file

While working on my code to directly print the date from index.js into data.json, I encountered an error stating that data is not defined. This is what I have done so far: Ran npm init Installed jsonfile using npm i jsonfile index.js const json ...

Building a dynamic webpage using AJAX, MVC, and web APIs to generate a div element filled

I'm currently working with a restful API, MVC, and ajax. My goal is to retrieve data from the backend and then display images within certain div elements. The expected outcome should resemble this example: https://i.stack.imgur.com/BFqWL.png This sni ...

Tips for showcasing a "loading" animation as a lazy-loaded route component loads

Utilizing webpack's code splitting feature, I have divided my application into multiple chunks to prevent the entire bundle from being downloaded at once when a user visits my website. Some routes require large chunks that may take some time to downl ...

Implementing Session Variables in C# with AJAX

Can someone please assist me? I am trying to add a variable into the session state using AJAX, but it doesn't seem to be working. When I click the button, it redirects to Travellerinfo.aspx page. Here is my test.aspx: <%@ Page Language="C#" Auto ...

Changing the value of a form input name after a $scope.$watch event is activated

In my AngularJS 1.4 project, I have a form input element where I am attempting to dynamically set the name attribute. <input type="email" name="{{ctrl.name}}" class="form-control" id="email" ng-minlength="5" required> The assignment of the name att ...

The member's voiceChannel is undefined

I've encountered an issue with my discord bot not being able to determine which channel a user is in. When I check member.voiceChannel, it always returns undefined, even when I am currently in a voice channel. Here is the code snippet that illustrate ...

There are no values in the request.query object in express.js

I am facing an issue with the redirect URL I received from Google OAuth2: http://localhost:997/?#state=pass-through%20value&access_token=ya29.ImC6B1g9LYsf5siso8n_UphOFB0SXc5dqsm6LqHRWXbtNHisEblxjeLoYtGgwSVtCTGxOjjODiuTyH7VCHoZCEfUd_&token_type=Bea ...

Vue.js event change method does not initiate the trigger

I have been attempting to implement a change event in my Vue application, where a statement switches between true and false each time I check a checkbox. However, the functionality doesn't seem to be working as expected. This issue arose while follow ...

What is the method to trigger the alt + f4 combination in AngularJS when a button is clicked?

Is there a way to manually close the current tab in AngularJS by simulating Alt+F4 key press? $scope.cancel = function (event) { var keycode = event.keycode; }; <button class="btn btn-outline-primary" ng-click="cancel($event);">OK</button&g ...

Forwarding to a specific webpage

`<!DOCTYPE html> <html class="no-js"> ... </body> </html> I'm in the process of developing a website where users can enroll in programs by clicking on an ...

Is there a way to call class methods from external code?

I am seeking clarification on Class files. Below is an example of a Class that I have: class CouchController { constructor(couchbase, config) { // You may either pass couchbase and config as params, or import directly into the controller ...

The mobile navigation panel fails to open

I have a menu that I would like to toggle hide/show by adjusting its top position. Before logging in, the menu is structured as follows: <div class="lc"> <h1><a href="./"><img src="logo.png" /></a></h1> <a h ...

Is it possible to manipulate elements within an overflow container using JavaScript/jQuery when using the HTML style "overflow:hidden"?

My <div> has a styling of style="overflow:hidden" and the size of the <body> is fixed, intended as a multi-screen display without a user interface. Is there a method to access these "invisible" elements to identify the first one that exceeds t ...

Tips for creating an image rotation effect when hovering over a hyperlink

Here is what I currently have: a:link { color: #A39B9E; background-color: transparent; text-decoration: none; } a:hover { color: #7E7E7E; background-color: transparent; text-decoration: none; } r { font-size: 10px; color: #7E ...