Jump to content
Search Community

Get duration from array of timelines

tobiasger 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

How would I go about getting the total duration of all timelines in an array? I have a function that looks like this:

 

function createSlides(scene) {
        var slides = [];
        $("#" + scene + " .slide").each(function (i, element) {
            var tl = new TimelineMax({ delay: i * 4 });

            if ($(this).hasClass("image")) {
                tl.to(element, 1, { className: "-=pointer-events-none" })
                if ($(this).hasClass("packshot")) {
                    tl.to(element, 1, { opacity: 0.999, scale: 0.95, force3D: true })
                    tl.to(element, 2, { scale: 1, force3D: true })
                    tl.to(element, 1, { opacity: 0, scale: 1.05, force3D: true })
                } else {
                    tl.to(element, 1, { opacity: 0.999, scale: 1, force3D: true })
                    tl.to(element, 2, { scale: 1.2, force3D: true })
                    tl.to(element, 1, { opacity: 0, scale: 1.3, force3D: true })
                }
                tl.to(element, 1, { className: "+=pointer-events-none" })
            } else {
                tl.to(element, 1, { className: "-=pointer-events-none" })
                tl.to(element, 1, { opacity: 0.999, force3D: true })
                tl.to(element, 1, { opacity: 0, force3D: true, delay: 2 })
                tl.to(element, 1, { className: "+=pointer-events-none" })
            }

            slides.push(tl);
        })
        return slides;
    }


I then add this array of timelines to the master timeline like this:

 

var slides1 = createSlides("scene1");

var timeline = new TimelineMax()
.add(slides1, "scene1Slides");


I now want another animation, a to(), to start and finish at the same time as these timelines in the array. So starting at the "scene1Slides" label and then going for the same amount of time as all the timeline's total duration.

How would I do this?

Link to comment
Share on other sites

You can get total duration by calling timeline.totalDuration(). Then set totalDuration of your another animation to same as totalDuration of your timeline.

 

If your timeline has other animations as well, then you can add your slides related timeline to another timeline first and get it's total duration then add it to your main timeline.

  • Like 4
Link to comment
Share on other sites

8 minutes ago, Sahil said:

You can get total duration by calling timeline.totalDuration(). Then set totalDuration of your another animation to same as totalDuration of your timeline.

 

If your timeline has other animations as well, then you can add your slides related timeline to another timeline first and get it's total duration then add it to your main timeline.

Okay, so the trick is to first collect the timelines in an array, then add them to a separate timeline and then add that separate timeline to the main timeline?
 

Basically doing like this, considering my code above?:

 

var slides1 = createSlides("scene1");

var slides1Tl = new TimelineMax().add(slides1);

var timeline = new TimelineMax()
.to(element, slides1Tl.totalDuration(), { ... }, "scene1Slides")
.add(slides1Tl, "scene1Slides")

 

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