Three ways to implement animation in CSS3

Three ways to implement animation in CSS3

This is a test of the interviewee's basic knowledge of CSS.

There are three main ways to implement animation in CSS

The first one is: transition to achieve gradient animation

The second is: transform transition animation

The third is: animation to implement custom animation

The following is a detailed description of the implementation methods of the three types of animations.

transition gradient animation

Let's first look at the properties of transition:

  • Property: Fill in the CSS properties that need to be changed, such as width, line-height, font-size, color, etc., all properties that act on DOM styles;
  • Duration: The time unit required to complete the transition effect (s or ms)
  • timing-function: the speed curve of the effect (linear, ease, ease-in, ease-out, etc.)

The specific value of timing-function can be seen in the table below:

value describe
linear Uniform speed (equal to cubic-bezier(0,0,1,1))
ease From slow to fast and then slow again (cubic-bezier(0.25,0.1,0.25,1))
ease-in Slowly getting faster (equal to cubic-bezier(0.42,0,1,1))
ease-out Slowly slow down (equal to cubic-bezier(0,0,0.58,1))
ease-in-out First fast and then slow (equal to cubic-bezier(0.42,0,0.58,1)), gradually appearing and fading
cubic-bezier( n , n , n , n ) Define your own values ​​in the cubic-bezier function. Possible values ​​are between 0 and 1.

  • delay: delay trigger time of animation effect (unit: ms or s)

Let's look at a complete example:

<div class="base"></div>
.base {
            width: 100px;
            height: 100px;
            display: inline-block;
            background-color: #0EA9FF;
            border-width: 5px;
            border-style: solid;
            border-color: #5daf34;
            transition-property: width,height,background-color,border-width;
            transition-duration: 2s;
            transition-timing-function: ease-in;
            transition-delay: 500ms;
        /*Abbreviation*/
        /*transition: all 2s ease-in 500ms;*/
            &:hover {
                width: 200px;
                height: 200px;
                background-color: #5daf34;
                border-width: 10px;
                border-color: #3a8ee6;
            }
        }

Operation effect:

You can see that when the mouse moves up, the animation starts with a delay of 0.5s, and because the border-color is not set in the transition-property, there is no gradient animation.

transform animation

The transform property applies a 2D or 3D transformation. This attribute allows us to rotate, scale, tilt, and move elements. It is usually used in conjunction with the transition attribute.

  1. none: defines no conversion, generally used to register the conversion.
  2. transform-functions: Defines the type functions to be transformed. The main ones are:

2.1 Rotate: mainly divided into 2D rotation and 3D rotation. rotate(angle), 2D rotation, the parameter is the angle, such as 45deg; rotate(x,y,z,angle), 3D rotation, 3D rotation around the line from the original position to (x,y,z); rotateX(angle), 3D rotation along the X axis; rotateY(angle); rotateZ(angle);

2.2 Scale: Generally used to set the size of an element. The main types are the same as above, including scale(x, y), scale3d(x, y, z), scaleX(x), scaleY(y), and scaleZ(z), where x, y, and z are the shrinkage ratios.

2.3 Skew: Mainly used to tilt the style of an element. skew(x-angle, y-angle), 2D skew transformation along the x and y axes; skewX(angle), 2D skew transformation along the x axis; skew(angle), 2D skew transformation along the y axis.

2.4 Translate: Mainly used to move elements. translate(x, y), defines the pixel points moved along the x and y axes; translate(x, y, z), defines the pixel points moved along the x, y, and z axes; translateX(x); translateY(y); translateZ(z).

<h5>Transition is used together with transform</h5>
<div class="base base2"></div>
.base2{
          transform:none;
          transition-property: transform;
          &:hover {
              transform:scale(0.8, 1.5) rotate(35deg) skew(5deg) translate(15px, 25px);
          }
      }

Operation effect:

You can see that the box is rotated, tilted, translated, and enlarged.

animationcustom animation

In order to achieve more flexible animation effects, CSS3 also provides custom animation functions.

(1) name: The keyframe name that needs to be bound to the selector.

(2) duration: The time required to complete the animation, in seconds or milliseconds.

(3) timing-function: same as transition-linear.

(4) delay: sets the delay before the animation starts.

(5) iteration-count: sets the number of times the animation is executed. Infinite means an infinite loop.

(6) direction: whether to poll the animation in reverse. normal, the default value, the animation should play normally; alternate, the animation should play in reverse order.

<h5 class="title">animate custom animation</h5>
<div class="base base3"></div>
.base3 {
          border-radius: 50%;
          transform:none;
          position: relative;
          width: 100px;
          height: 100px;
          background: linear-gradient(
                  35 degrees,
                  #ccffff,
                  #ffcccc
          );
          &:hover {
              animation-name: bounce;
              animation-duration: 3s;
              animation-iteration-count: infinite;
          }
      }
      @keyframes bounce{
          0% {
              top: 0px;
          }
          50% {
              top: 249px;
              width: 130px;
              height: 70px;
          }
          100% {
              top: 0px;
          }
      }

Operation effect:

As you can see, custom animation can achieve more flexible animation effects, including all the functions of the first and second animations, and the properties are more comprehensive.

Online Production

The above code can be experienced online: Address

Source code address

The above are the details of the three ways to implement animation with CSS3. For more information about implementing animation with CSS3, please pay attention to other related articles on 123WORDPRESS.COM!

<<:  Five things a good user experience designer should do well (picture and text)

>>:  Specific use of routing guards in Vue

Recommend

mysql-5.7.28 installation tutorial in Linux

1. Download the Linux version from the official w...

Detailed explanation of Nginx passively checking the server's survival status

introduce Monitors the health of HTTP servers in ...

MySQL database Load Data multiple uses

Table of contents Multiple uses of MySQL Load Dat...

Solution to the IP address not being displayed under Linux

Table of contents Preface Solution: Step 1 Step 2...

Best Practices for Implementing Simple Jira Projects with React+TS

A set of projects for training react+ts Although ...

JavaScript imitates Taobao magnifying glass effect

This article shares the specific code for JavaScr...

How to install Docker using scripts under Linux Centos

What is the main function of Docker? At present, ...

Example of viewing and modifying MySQL transaction isolation level

Check the transaction isolation level In MySQL, y...

jQuery implements Table paging effect

This article shares the specific code of jQuery t...

Detailed explanation of Nginx reverse proxy example

1. Reverse proxy example 1 1. Achieve the effect ...

How to view the network routing table in Ubuntu

What are Routing and Routing Table in Linux? The ...

vue+echarts realizes the flow effect of China map (detailed steps)

@vue+echarts realizes the flow effect of China ma...

UrlRewriter caching issues and a series of related explorations

When developing a website function, the session c...