有没有一种简单的方法来重置 TimelineMax JavaScript 对象?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/14608999/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-26 22:05:04  来源:igfitidea点击:

Is there a simple way to reset a TimelineMax JavaScript object?

javascriptgreensock

提问by landons

I'm having trouble re-initializing a TimelineMax sequence. When the window is resized, I need to revert all the tweens to their default styles and re-initialize them based on the new window size. Is there a simple way to effectively destroy the timeline and start fresh, without manually resetting all the CSS properties?

我在重新初始化 TimelineMax 序列时遇到问题。调整窗口大小时,我需要将所有补间恢复为默认样式,并根据新窗口大小重新初始化它们。有没有一种简单的方法可以有效地销毁时间线并重新开始,而无需手动重置所有 CSS 属性?

回答by ahren

Depending on what you are trying to achieve, I can suggest two methods.

根据您要实现的目标,我可以建议两种方法。

The first is exactly as you are describing:

第一个和你描述的完全一样:

myTimeline.pause(0, true); //Go back to the start (true is to suppress events)
myTimeline.remove();

This takes everything back to the way they were at the start of the timeline. You can remove any initialization properties by calling .invalidate()as well.

这使一切恢复到时间线开始时的状态。您也可以通过调用删除任何初始化属性.invalidate()

The second method, I'm adding because it may be worth thinking about...
Rather than completely restarting the tweens/timeline, why not .invalidate()(items stay where they are, just the timeline itself is cleared) and use the tweening method .to()rather than .from(), as you'll get a nicer user experience when everything moves from it's previous position to it's new position rather than completely restarting.

第二种方法,我正在添加,因为它可能值得考虑......
而不是完全重新启动补间/时间线,为什么不.invalidate()(项目保持原样,只是时间线本身被清除)并使用补间方法.to()而不是.from(),因为当一切都从以前的位置移动到新的位置而不是完全重新启动时,您将获得更好的用户体验。

Also, the majority of users don't resize the window. So think about whether or not it's worth it if it becomes a major time consumer.

此外,大多数用户不会调整窗口大小。所以想想如果它成为一个主要的时间消费者是否值得。

API/Docs: http://api.greensock.com/js/

API/文档:http: //api.greensock.com/js/