Home >Web Front-end >JS Tutorial >In-depth understanding of JavaScript animation in Anime.js, Part 2: Detailed explanation of parameters

In-depth understanding of JavaScript animation in Anime.js, Part 2: Detailed explanation of parameters

王林
王林Original
2023-08-26 18:09:15689browse

In-depth understanding of JavaScript animation in Anime.js, Part 2: Detailed explanation of parameters

In the first tutorial in the Anime.js series, you learned about the different ways to specify the target element to animate and the types of CSS properties and DOM properties that can be animated. The animation in the previous tutorial was very basic. All target elements simply move a certain distance or change the bounding radius at a fixed speed.

Sometimes you may need to animate a target element in a more rhythmic manner. For example, you might have 10 different elements that you want to move from left to right, with a 500ms delay between the start of each element's animation. Likewise, you may want to increase or decrease the animation duration based on the position of each element.

In this tutorial, you will learn how to use Anime.js to correctly time animations of different elements using specific parameters. This will allow you to control the playback order of animation sequences for individual elements or for all elements.

Attribute parameters

These parameters allow you to control the duration, delay, and easing of a single property or a group of properties at once. The duration and delay parameters are specified in milliseconds. The default value for duration is 1000 milliseconds or 1 second.

This means that any animation applied to an element will play for 1 second unless otherwise specified. delay The parameter specifies how long it takes for the animation to start after triggering. The default value for delay is 0. This means the animation will start as soon as it is triggered.

You can use the easing parameter to control the rate at which the animation plays during the activity. Some animations start slow, speed up in the middle, and then slow down again at the end. Others start out fast and then slow down the rest of the way.

However, in all cases, the animation always completes within the time specified with the duration parameter. Anime.js provides a number of easing functions that you can apply directly to an element using just its name. For some easing functions, you can also set a value for the elasticity parameter to control how much the element's value bounces back and forth like a spring.

You'll learn more about the different easing functions in the final tutorial of this series. The following code snippet shows how to apply all these parameters to different animations.

var slowAnimation = anime({
  targets: '.square',
  translateY: 250,
  borderRadius: 50,
  duration: 4000
});

var delayAnimation = anime({
  targets: '.square',
  translateY: 250,
  borderRadius: 50,
  delay: 800
});

var cubicAnimation = anime({
  targets: '.square',
  translateY: 250,
  borderRadius: 50,
  duration: 1200,
  easing: 'easeInOutCubic'
});

As you can see, these parameters can be used independently of other parameters or in combination with them. cubicAnimation applies both the duration and easing parameters. If no duration is specified, the animation will run for 1 second. Now, it will run for 1,200 milliseconds or 1.2 seconds.

One major limitation of the attribute parameters in the example above is that all animations of the target element will have the same duration, delay and easing values.

This may or may not be the desired behavior. For example, you might want to first translate the target element and then animate its border radius, rather than simultaneously panning and changing the target element's border radius. Anime.js allows you to specify different value properties for each of the duration, delay, easing, and elasticity parameters. The code and demo below should make it clearer.

var indiParam = anime({
  targets: '.square',
  translateY: {
    value: 250
  },
  rotate: {
    value: '2.125turn'
  },
  backgroundColor: {
    value: 'rgb(255,0,0)',
    duration: 400,
    delay: 1500,
    easing: 'linear'
  },
  duration: 1500
});

In the above code, all the properties we want to animate have different values. The background color animation has a duration of 400ms, while the rotation and panning animations use a global duration value of 1500ms.

The background color animation is also delayed, so any change in color only starts after 1500 milliseconds have passed. The rotate and translateY properties use the default values ​​for the delay and easing parameters, since we are not providing values ​​that are not local to them, Nor is it a global value.

基于函数的参数

当您想要更改单个属性的动画顺序和持续时间时,基于属性的参数非常有用。但是,相同的 durationdelay 仍将应用于所有目标元素上的各个属性。基于函数的参数允许您为不同的目标元素分别指定 durationdelayelasticityeasing以紧凑的方式。

在这种情况下,您可以使用函数而不是数字来设置不同参数的值。这些函数接受三个参数:targetindextargetCounttarget 参数存储对当前目标元素的引用。 index 参数存储当前目标元素的索引或位置。 targetCount 参数存储目标元素的总数。

当需要根据目标元素的某些属性设置动画值时,target 参数非常有用。例如,您可以将目标元素的 delaydurationeasing 值存储在数据属性中,然后稍后访问它们。 p>

类似地,您可以访问目标元素的背景颜色,然后操作它来为各个元素设置最终的唯一颜色值。通过这种方式,您可以对所有元素进行动画处理,使其背景颜色比当前颜色深 20%。

index 参数为您提供当前目标在目标元素列表中的位置。您可以使用它逐步更改不同元素的 durationdelay 等参数的值。

当您想要按升序设置值时,这通常很有用。您还可以从 targetCount 中减去 index 以降序设置值。以下代码片段使用这两个参数来按升序和降序指定值。

var delaySequence = anime({
  targets: '.square',
  translateY: 250,
  delay: function(target, index) {
    return index * 200;
  }
});

var delaySequenceR = anime({
  targets: '.square',
  translateY: 250,
  delay: function(target, index, targetCount) {
    return (targetCount - index) * 200;
  }
});

以下代码使用 index 参数为每​​个目标元素设置不同的 easing 值。

var easeInValues = ['easeInQuad', 'easeInCubic', 'easeInQuart', 'easeInQuint', 'easeInSine', 'easeInExpo', 'easeInCirc', 'easeInBack', 'easeInElastic'];

var easeInSequence = anime({
  targets: '.square',
  translateY: 250,
  duration: 2000,
  easing: function(target, index) {
    return easeInValues[index];
  },
  autoplay: false
});

动画参数

最后一组参数允许您指定动画应播放的次数以及播放的方向。您可以使用 loop 参数指定动画播放的次数。还有一个 autoplay 参数,可以设置为 truefalse。它的默认值为 true,但您可以通过将其设置为 false 来阻止动画自行启动。

direction 参数控制动画播放的方向。它可以具有三个值:normalreversealternate。默认值为 normal,这使得动画从开始值到结束值正常播放。一旦目标元素达到结束值,如果 loop 值大于 1,目标元素会突然跳回起始值,然后再次开始动画。

direction 设置为 reverse 并且 loop 值大于 1 时,动画将反转。换句话说,目标元素从最终状态开始动画,然后向后到达初始状态。一旦它们处于初始状态,元素就会跳回到最终状态,然后再次开始反向动画。 alternate 方向值会在每次循环后更改动画方向。

var normalLoop = anime({
  targets: '.square',
  translateY: 250,
  delay: function(target, index) {
    return index * 200;
  },
  loop: 4,
  easing: 'easeInSine',
  autoplay: false
});

在下面的演示中,我将循环次数设置为四,以便您可以轻松注意到不同模式下元素动画的差异。

使用 stagger() 方法

到目前为止,在本教程中,我们已经使用函数将不同的值传递给目标元素的动画延迟或持续时间。您还可以借助 Anime.js 中的 stagger() 方法获得相同的功能。

stagger() 方法基本上允许您控制动画如何在多个元素上发生。它接受两个参数。第一个是您想要应用的值,第二个是一个带有一堆参数的对象,这些参数决定如何应用交错。

下面是一个示例,展示 stagger() 如何与我们到目前为止编写的常规函数​​进行比较:

// A function to introduce animation delay in elements.
delay: function(target, index) {
    return index * 200;
}

// The stagger() Equivalent
delay: anime.stagger(200);

您现在可能会问是否有一种方法可以反向应用动画延迟,就像我们对函数所做的那样。是的,这绝对是可能的。这是一个例子:

// Reversing the delay direction
delay: function(target, index, targetCount) {
    return (targetCount - index) * 200;
}

// Equivalent functionality with stagger()
delay: anime.stagger(200, {"direction": "reverse"})
  

我们可以类似地对动画持续时间应用交错。由于交错,前面示例中第一个元素的延迟值被设置为 0,这也是我们想要做的。但是,第一个元素的动画持续时间必须非零。否则,页面加载后就会处于结束阶段。

可以借助 start 参数设置第一个元素的非零动画持续时间,该参数设置为 1000 以获得惊人的效果。这是一个例子:

// Duration starts at 1000 and increases by 800
duration: function(target, index) {
    return 1000 + index * 800;
}

// Equivalent functionality with stagger()
duration: anime.stagger(800, {"start": 1000})

如果在开始最后一个元素时必须应用非零持续时间值怎么办?在这种情况下,我们可以对 stagger() 方法使用以下参数:

// A non-zero duration in reverse direction
duration: function(target, index, targetCount) {
    return 1000 + (targetCount - index) * 800;
}

// Equivalent functionality with stagger()
duration: anime.stagger(800, {"start": 1000, "direction": "reverse"})

以下 CodePen 演示的所有操作与“基于函数的参数”部分中的示例类似,但它使用 stagger() 方法来执行此操作。正如您所看到的,最终结果没有任何区别。

我想指出的一件事是交错方法在旧版本的库中不起作用。确保您使用的是最新版本以避免任何错误。

最终想法

在本教程中,您了解了可用于控制 Anime.js 中目标元素的动画的不同类型的参数。属性参数用于控制各个属性的动画。

您可以使用它们来控制各个元素的动画播放顺序。函数参数允许您控制单个元素相对于整个组的动画时间和速率。动画参数允许您控制不同元素的动画本身的播放方式。

The above is the detailed content of In-depth understanding of JavaScript animation in Anime.js, Part 2: Detailed explanation of parameters. For more information, please follow other related articles on the PHP Chinese website!

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn