如何使方法链成为条件? [英] How to make a method chain conditional?
本文介绍了如何使方法链成为条件?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我正在使用一个名为anime.js的动画库。我需要根据一些检查来有条件地制作链(时间轴)的动画。例如,这是我的时间轴:
I'm using an animation library called anime.js. I need to make conditional an animation of the chain (timeline) basing on some check. For example, this is my timeline:
const tl = anime.timeline();
tl
.add(animation_1)
.add(animation_2)
.add(animation_3);
,我只希望 animation_1
变量 a
是 true
,所以我
and I want the animation_1
only if the variable a
is true
, so I
const tl = anime.timeline();
tl
.add(animation_1) // <== I need an if(a == true) on this animation
.add(animation_2)
.add(animation_3);
一般如何使用javascript(尤其是anime.js)做到这一点?
How can do this in general with javascript and specifically in anime.js?
推荐答案
使用if语句而无需链接:
Do it without chaining, using an if statement:
const tl = anime.timeline();
if (a) {
tl.add(animation_1);
}
t1.add(animation_2)
.add(animation_3);
这篇关于如何使方法链成为条件?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文