Home > Web Front-end > JS Tutorial > Starting with the Mojs animation library: HTML components

Starting with the Mojs animation library: HTML components

王林
Release: 2023-09-01 16:37:02
Original
1398 people have browsed it

从 Mojs 动画库开始:HTML 组件

Many websites these days use some kind of animation to make their landing pages more attractive. Thankfully, there are many libraries that allow you to animate elements on your web page without having to do everything from scratch. In this tutorial, you will learn about one such library called mojs.

This library is very easy to use due to its simple declarative API. The animations you can create using mojs will be smooth and retina-friendly so everything looks professional.

Install Mojs

There are many ways to include mojs into your project. You can get the library from the GitHub repository. Alternatively, you can include links to the latest repositories from different CDNs directly in your project.

<script src="//cdn.jsdelivr.net/mojs/latest/mo.min.js"></script>
Copy after login

Developers can also install mojs using package managers such as npm and Bower by running the following command:

npm install mo-js

bower install mojs
Copy after login

Once you have included the library into your project, you can start using the different modules to create interesting animations.

HTML Modules in Mojs

This tutorial will focus on the HTML module in the mojs library. This module can be used to animate different HTML elements on a web page.

In order to animate a DOM element, the first thing you need to do is create a mojs Html object. You can specify selectors for the elements and their properties that you want to animate within this object.

Setting a value for el allows you to identify the element to animate using mojs. You can set its value to a selector or DOM node.

The HTML module has some predefined properties that can be used to animate different transform-related properties of DOM elements. For example, you can specify the x, y, and z properties. You can also rotate any element along different axes using the angleX, angleY and angleZ properties. This is similar to the corresponding rotateX(), rotateY(), and rotateZ() transformations in CSS. You can also skew elements along the X or Y axis with the help of the skewX and skewY properties.

Applying scaling animations to different elements is just as easy. If you want to scale an element in both directions, just set a value for the scale property. Likewise, you can animate the scaling of an element along different axes by setting appropriate values ​​for the scaleX and scaleY properties.

In addition to all these properties that allow you to set the initial and final values ​​of the animation, there are a few other properties that control the animation playback. You can specify the duration of the animation using the duration property. The provided value expects a number which will set the animation duration in milliseconds. If you want to start the animation after a delay, you can set the delay value using the delay property. Just like duration, delay also expects its value to be a number.

With the repeat attribute, the animation can be repeated multiple times. Its default value is zero, which means the animation will only play once. A setting of 1 will play the animation twice, a setting of 2 will play the animation 3 times. After the animation completes its first iteration, the element returns to its initial state and starts animation again (if you have set a value for the repeat attribute). This sudden jump from the final state to the initial state may not be ideal in all situations.

If you want the animation to play backwards after reaching the final state, you can set the value of the isYoyo property to true. The default setting is false . Finally, you can set the speed at which the animation plays using the speed property. Its default value is 1. Setting it to 2 will make the animation play twice as fast. Likewise, setting it to 0.5 will play the animation at half speed.

The mojs Html object you create will not animate individual elements by itself. You must call the play() method on every mojs Html animation you want to play. Here's an example that uses all the properties we just discussed to animate three different boxes:

var htmlA = new mojs.Html({
  el: ".a",
  x: {
    0: 400
  },
  angleZ: {
    0: 720
  },
  duration: 1000,
  repeat: 4,
  isYoyo: true
});

var htmlB = new mojs.Html({
  el: ".b",
  x: {
    400: 0
  },
  angleY: {
    0: 360
  },
  angleZ: {
    0: 720
  },
  duration: 1000,
  repeat: 4
});

var htmlC = new mojs.Html({
  el: ".c",
  x: {
    0: 400
  },
  angleY: {
    0: 360
  },
  scaleZ: {
    1: 2
  },
  skewX: {
    0: 30
  },
  duration: 1000,
  repeat: 4,
  isYoyo: true
});

document.querySelector(".play").addEventListener("click", function() {
  htmlA.play();
  htmlB.play();
  htmlC.play();
});
Copy after login

您不仅限于为元素的变换属性设置动画。 mojs 动画库还允许您对所有其他可动画化的 CSS 属性进行动画处理。您只需确保为它们提供有效的初始值和最终值即可。例如,您可以通过为 background 指定有效值来设置元素的背景颜色动画。

如果要设置动画的 CSS 属性包含连字符,则在 mojs camelCase >Html 对象。这意味着您可以通过为 borderRadius 属性设置有效值来对 border-radius 进行动画处理。下面的例子应该可以让一切变得清晰:

var htmlA = new mojs.Html({
  el: ".a",
  x: { 
    0: 400
  },
  angleZ: {
    0: 360
  },
  background: {
    red: 'black'
  },
  borderWidth: {
    10: 20
  },
  borderColor: {
    'black': 'red'
  },
  borderRadius: {
    0: '50%'
  },
  duration: 2000,
  repeat: 4,
  isYoyo: true
});

document.querySelector(".play").addEventListener("click", function() {
  htmlA.play();
});
Copy after login

在上面的示例中,边框颜色从黑色变为红色,同时边框半径从 0 变化到 50%。您应该注意,无单位的数字将被视为像素值,而带单位的数字应指定为“50%”等字符串。

到目前为止,我们已经使用了一组补间属性来控制不同动画的播放。这意味着一个元素从 x:0 移动到 x:200 所需的时间与从 scale 进行缩放所需的时间相同: 1规模:2

这可能不是一个理想的行为,因为您可能希望延迟 延迟 某些属性的动画并控制它们的 duration 。在这种情况下,您可以在属性对象本身内部指定每个属性的动画播放参数。

var htmlA = new mojs.Html({
  el: ".a",
  x: { 
    0: 400,
    duration: 1000,
    repeat: 8,
    isYoyo: true
  },
  angleY: {
    0: 360,
    delay: 500,
    duration: 1000,
    repeat: 4,
    isYoyo: true
  },
  angleZ: {
    0: 720,
    delay: 1000,
    duration: 1000,
    repeat: 4,
    isYoyo: true
  }
});

document.querySelector(".play").addEventListener("click", function() {
  htmlA.play();
});
Copy after login

Mojs 中可用的缓动功能

默认情况下,您创建的每个动画都会应用 sin.out 缓动。如果您希望使用不同的缓动函数进行动画播放,可以使用 easing 属性指定其值。默认情况下,当动画向后播放时,也会使用为 easing 指定的值。如果您想对向后动画应用不同的缓动,可以为 backwardEasing 属性设置一个值。

mojs 库有 11 种不同的内置缓动函数。这些是 lineareasesinquadcubicquartquintexpocircback elastic。线性缓动只有一种变体,名为 linear.none。这是有道理的,因为动画在不同阶段将以相同的速度进行。所有其他缓动函数都具有三种不同的变体,其中 inoutinout 添加在末尾。

有两种方法可以指定动画的缓动函数。您可以使用 elastic.in 这样的字符串,也可以使用 mojs.easing 对象直接访问缓动函数,例如 mojs.easing。 elastic.inout。在嵌入式 CodePen 演示中,我在每个条形上应用了不同的缓动函数,因此其宽度将以不同的速度变化。这将使您了解每次缓动时动画速度有何不同。

var allBoxes = document.querySelectorAll(".box");

var animations = new Array();

var easings = ['ease.in', 'sin.in', 'quad.in', 'cubic.in', 'quart.in', 'quint.in', 'expo.in', 'circ.in', 'back.in', 'elastic.in'];

allBoxes.forEach(function(box, index) {
  var animation = new mojs.Html({
    el: box,
    width: {
      0: 550,
      duration: 4000,
      repeat: 8,
      isYoyo: true,
      easing: easings[index]
    }
  });
  animations.push(animation);
});

document.querySelector(".play").addEventListener("click", function() {
  animations.forEach(function(anim) {
    anim.play();
  });
});
Copy after login

由于我们只想更改应用于每个框的缓动函数,因此我创建了一个循环来迭代它们,然后应用从 easings 数组中选取的缓动函数。这可以防止不必要的代码重复。您可以使用相同的技术为多个元素设置动画,其中属性值根据模式而变化。

控制动画播放

Mojs 提供了很多方法,允许我们在动画开始后控制不同元素的动画播放。您可以随时通过调用 pause() 方法暂停动画。同样,您可以通过调用 resume() 方法来恢复任何暂停的动画。

使用 pause() 暂停的动画将始终从您调用 pause() 的位置恢复。如果您希望动画在暂停后从头开始,您应该使用 stop() 方法。

您还可以使用 playBackward() 方法向后播放动画。之前,我们使用 speed 属性来控制 mojs 播放动画的速度。 Mojs 还有一个 setSpeed() 方法,可以在动画仍在进行时设置动画速度。在下面的示例中,我使用了所有这些方法来控制基于按钮点击的动画播放。

var speed = 1;

var htmlA = new mojs.Html({
  el: ".a",
  angleZ: {
    0: 720
  },
  borderRadius: {
    0: '50%'
  },
  borderWidth: {
    10: 100
  },
  duration: 2000,
  repeat: 9999,
  isYoyo: true
});

document.querySelector(".play").addEventListener("click", function() {
  htmlA.play();
});

document.querySelector(".pause").addEventListener("click", function() {
  htmlA.pause();
});

document.querySelector(".stop").addEventListener("click", function() {
  htmlA.stop();
});

document.querySelector(".faster").addEventListener("click", function() {
  speed = speed + 1;
  htmlA.setSpeed(speed);
  document.querySelector(".data").innerHTML = "Speed: " + speed;
});

document.querySelector(".slower").addEventListener("click", function() {
  speed = speed/2;
  htmlA.setSpeed(speed);
  document.querySelector(".data").innerHTML = "Speed: " + speed;
});
Copy after login

在下面的演示中,当前动画播放速度显示在左下角的黑框中。点击更快会将当前速度提高 1,点击更慢会将当前速度减半。

最终想法

在本教程中,我们学习了如何使用 mojs 中的 HTML 模块为网页上的不同 DOM 元素设置动画。我们可以使用选择器或 DOM 节点指定要设置动画的元素。该库允许您使用 mojs Html 对象的内置属性对不同的变换属性和任何元素的不透明度进行动画处理。但是,您还可以通过使用 camelCase 表示法指定名称来对其他 CSS 属性进行动画处理。

JavaScript 并非没有学习曲线,而且还有大量的框架和库可以让您忙碌起来。如果您正在寻找其他资源来学习或在工作中使用,请查看我们在 Envato 市场中提供的资源。

如果您希望我在本教程中澄清任何内容,请告诉我。我们将在下一个教程中介绍 mojs 库中的 Shape 模块。

The above is the detailed content of Starting with the Mojs animation library: HTML components. 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
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template