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

王林
Release: 2023-08-26 18:09:15
Original
565 people have browsed it

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. Thedurationanddelayparameters 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.delayThe 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 theeasingparameter 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 thedurationparameter. 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 theelasticityparameter 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' });
Copy after login

As you can see, these parameters can be used independently of other parameters or in combination with them.cubicAnimationapplies both thedurationandeasingparameters. 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 sameduration,delayandeasingvalues.

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 theduration,delay,easing, andelasticityparameters. 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 });
Copy after login

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. TherotateandtranslateYproperties use the default values for thedelayandeasingparameters, 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值存储在数据属性中,然后稍后访问它们。

类似地,您可以访问目标元素的背景颜色,然后操作它来为各个元素设置最终的唯一颜色值。通过这种方式,您可以对所有元素进行动画处理,使其背景颜色比当前颜色深 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; } });
Copy after login

以下代码使用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 });
Copy after login

动画参数

最后一组参数允许您指定动画应播放的次数以及播放的方向。您可以使用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 });
Copy after login

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

使用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);
Copy after login

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

// Reversing the delay direction delay: function(target, index, targetCount) { return (targetCount - index) * 200; } // Equivalent functionality with stagger() delay: anime.stagger(200, {"direction": "reverse"})
Copy after login

我们可以类似地对动画持续时间应用交错。由于交错,前面示例中第一个元素的延迟值被设置为 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})
Copy after login

如果在开始最后一个元素时必须应用非零持续时间值怎么办?在这种情况下,我们可以对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"})
Copy after login

以下 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!

source:php.cn
Statement of this Website
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
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!