Implementation methods of common CSS3 animations

Implementation methods of common CSS3 animations

1. What is

CSS Animations is a proposed module for Cascading Style Sheets that allows Extensible Markup Language (XML) elements to be animated using CSS.

It refers to the process of elements gradually transitioning from one style to another.

There are many common animation effects, such as translation, rotation, scaling, etc. Complex animation is a combination of multiple simple animations.

There are several ways to implement animation with CSS:

  • transition implements gradient animation
  • transform animation
  • animation implements custom animation

2. Implementation

transition implements gradient animation

The properties of transition are as follows:

  • Property: Fill in the CSS property that needs to be changed
  • Duration: The time unit required to complete the transition effect (s or ms)
  • timing-function: The speed curve of the completed effect
  • delay: delay trigger time of animation effect

The values ​​of timing-function are as follows:

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.

Note: Not all properties can be used in transitions, such as display:none<->display:block

For example, to achieve the animation effect of changing when the mouse moves

<style>
       .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;*/
        .base:hover {
            width: 200px;
            height: 200px;
            background-color: #5daf34;
            border-width: 10px;
            border-color: #3a8ee6;
        }
</style>
<div ></div>

transform animation

Contains four commonly used functions:

  • translate: displacement
  • scale: zoom
  • rotate: rotate
  • skew: tilt

Generally used with transition overuse

Note that transform does not support inline elements, so convert it to block before using it.

For example

<style>
    .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;
    }
    .base2 {
        transform: none;
        transition-property: transform;
        transition-delay: 5ms;
    }

    .base2:hover {
        transform: scale(0.8, 1.5) rotate(35deg) skew(5deg) translate(15px, 25px);
    }
</style>
 <div ></div>

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

animation implements custom animation

Animation is an abbreviation of 8 properties, as follows:

property describe Property Value
animation-duration Specifies the time required for the animation to complete a cycle, in seconds (s) or milliseconds (ms). The default value is 0
animation-timing-function Specifies the animation timing function, that is, the speed curve of the animation. The default is "ease" linear, ease, ease-in, ease-out, ease-in-out
animation-delay Specify the animation delay time, that is, when the animation starts. The default value is 0.
animation-iteration-count Specifies the number of times the animation is played. The default value is 1.
animation-direction specifies the direction of animation playback The default is normal normal, reverse, alternate, alternate-reverse
animation-fill-mode Specifies the animation fill mode. The default is none forwards, backwards, both
animation-play-state Specifies the animation playback state, running or paused. The default is running running、pauser
animation-name Specify the name of the @keyframes animation

CSS animation only needs to define some key frames, and the browser will calculate the rest of the frames based on the interpolation of the timing function.

Define keyframes via @keyframes

Therefore, if we want the element to rotate in a circle, we only need to define the start and end frames:

@keyframes rotate{
    from{
        transform: rotate(0deg);
    }
    to{
        transform: rotate(360deg);
    }
}

From means the first frame, and to means the last frame.

You can also use percentages to describe the life cycle

@keyframes rotate{
    0%{
        transform: rotate(0deg);
    }
    50%{
        transform: rotate(180deg);
    }
    100%{
        transform: rotate(360deg);
    }
}

After defining the keyframe, you can use it directly:

animation: rotate 2s;

Conclusion

property meaning
transition Used to set the style of an element. It has a similar effect to animation, but the details are very different.
transform Used to rotate, scale, move or tilt elements. It has nothing to do with setting style animations. It is equivalent to color, which is used to set the "appearance" of elements.
translate It is just a property value of transform, that is, moving
animation Used to set animation properties. It is a shorthand property that contains 6 properties.

The above is the detailed content of the implementation methods of common CSS3 animations. For more information on the implementation of CSS3 animations, please pay attention to other related articles on 123WORDPRESS.COM!

<<:  Detailed explanation of jQuery's core functions and event handling

>>:  HTML+CSS+JS sample code to imitate the brightness adjustment effect of win10

Recommend

Use PSSH to batch manage Linux servers

pssh is an open source software implemented in Py...

Detailed explanation of docker network bidirectional connection

View Docker Network docker network ls [root@maste...

HTML Tutorial: Collection of commonly used HTML tags (4)

These introduced HTML tags do not necessarily ful...

Front-end JavaScript housekeeper package.json

Table of contents 1. Required attributes 1. name ...

CentOS 6.4 MySQL 5.7.18 installation and configuration method graphic tutorial

The specific steps of installing mysql5.7.18 unde...

Docker meets Intellij IDEA, Java development improves productivity tenfold

Table of contents 1. Preparation before developme...

Example of how to change the domestic source in Ubuntu 18.04

Ubuntu's own source is from China, so the dow...

Detailed explanation of FTP environment configuration solution (vsftpd)

1. Install vsftpd component Installation command:...

Count the list tags in HTML

1. <dl> defines a list, <dt> defines ...

Detailed explanation of Linux file permissions and group modification commands

In Linux, everything is a file (directories are a...

The whole process of node.js using express to automatically build the project

1. Install the express library and generator Open...

Do you know how to use vue-cropper to crop pictures in vue?

Table of contents 1. Installation: 2. Use: 3. Bui...

Javascript to achieve the drag effect of the login box

This article shares the specific code of Javascri...

How to allow all hosts to access mysql

1. Change the Host field value of a record in the...