javascript - 如何使方法链有条件?

标签 javascript ecmascript-6 anime.js

我正在使用一个名为anime.js 的动画库。我需要根据一些检查来制作有条件的链(时间线)动画。例如,这是我的时间表:

const tl = anime.timeline();
tl
  .add(animation_1)
  .add(animation_2)
  .add(animation_3);

我想要animation_1仅当变量 atrue ,所以我

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 中?

最佳答案

使用 if 语句,无需链接即可实现:

const tl = anime.timeline();

if (a) {
    tl.add(animation_1); 
}

t1.add(animation_2)
  .add(animation_3);

关于javascript - 如何使方法链有条件?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/59737453/

相关文章:

javascript - 使用javascript获取变形svg的当前坐标

javascript - 为什么 setTimeout 在此函数中不起作用?

javascript - 如何在 Visual Studio Code 中调试单个 Nightwatch JS 测试

javascript - 为什么 for() 函数比 ES6 map() 和 some() 更快地查找重复项?

css - 使用 Anime.js 将元素定位到任何设备的右下角

javascript - 在输入时启动/停止 js 动漫时间轴的最佳方法是什么

javascript - Dojo 增强网格 : how to get selected column?

javascript - 从我的网站播放声音的最佳方式是什么?

javascript - 混合类型 react 的映射函数中的 typescript 类型问题

javascript - 防止 babel 尝试将资源转换为 javascript 模块