简体   繁体   English

如何使用greensock JavaScript库控制主线程?

[英]How can I control the main thread with greensock JavaScript library?

I have the following pseudo code, with two functions that execute some animations using to() method, namely minimise() and maximise(). 我有以下伪代码,其中有两个函数使用to()方法执行一些动画,即minimise()和maximise()。 Now when I call them from another function for example fooOne() what happens is that the main thread rushes in the minimise() function, starts the timeline animations then quickly returns to fooOne() and jumps into maximise() where it too starts the timeline animations. 现在,当我从另一个函数(例如fooOne())调用它们时,会发生的情况是主线程冲入了minimise()函数,启动了时间轴动画,然后迅速返回fooOne()并跳至maximise(),它也开始了时间轴动画。 Now what I am getting is the animations in minimise() and maximise() running almost concurrently, how can I go about and only execute maximise after minimise finished? 现在我得到的是minimise()和maximise()中的动画几乎同时运行,我该如何去做,只有在最小化完成后才执行最大化?

I can do this with jQuery but I couldn't find any documentations or posts about this topic on the internet. 我可以使用jQuery来做到这一点,但在互联网上找不到有关此主题的任何文档或帖子。

Regards. 问候。

function fooOne(){
   minimise() ;
   maximise() ;
}

function fooTwo(){
   maximise() ;
}

function fooThree(){
   minimise() ;
}

function minimise(){
   var timeline = new TimelineLite();
   timeline.to(........);
   timeline.to(........);
   // loads more animations below.
}

function maximise(){
   var timeline = new TimelineLite();
   timeline.to(........);
   timeline.to(........);
   // loads more animations below.
}

Sidenote: you're mixing up the terms a bit - 'multithreading' has a little different meaning in the programming world and has to do with being able to execute multiple processes at the same time. 旁注:您可能会混淆一些术语-“多线程”在编程世界中的含义有所不同,并且与能够同时执行多个进程有关。 Javascript is by design single-threaded (apart from web workers), so it is not applicable here. Javascript是设计为单线程的(除Web worker以外),因此此处不适用。 (You might want to remove the multithreading tag). (您可能要删除多线程标记)。

As for your question: TimelineLite has a nice way to handle this - from your 'minimise' and 'maximise' functions you can return the newly created timelines and add them together inside the 'fooOne' function: 关于您的问题:TimelineLite有一个很好的处理方法-从“最小化”和“最大化”功能中,您可以返回新创建的时间线,并将它们添加到“ fooOne”功能内:

function fooOne(){
   var mainTimeline = new TimelineLite();
   mainTimeline.add(minimise());
   mainTimeline.add(maximise());
}

function fooTwo(){
   maximise() ;
}

function fooThree(){
   minimise() ;
}

function minimise(){
   var timeline = new TimelineLite();
   timeline.to(........);
   timeline.to(........);
   // loads more animations below.

   return timeline;
}

function maximise(){
   var timeline = new TimelineLite();
   timeline.to(........);
   timeline.to(........);
   // loads more animations below.

   return timeline;
}

Calling 'add' on a timeline will by default append tweens/timelines at the end. 默认情况下,在时间轴上调用“添加”将在最后附加补间/时间轴。 This is a great way to split complex animations into simpler ones. 这是将复杂的动画拆分为更简单的动画的好方法。

Sample codepen: http://codepen.io/anon/pen/JGBEYj 代码库示例: http ://codepen.io/anon/pen/JGBEYj

Also take a look at the documentation for details: http://greensock.com/docs/#/HTML5/GSAP/TimelineLite/add/ 另请查看文档以了解详细信息: http : //greensock.com/docs/#/HTML5/GSAP/TimelineLite/add/

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM