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

CentOS 7 switching boot kernel and switching boot mode explanation

centos7 switch boot kernel Note: If necessary, it...

Design Theory: Ten Tips for Content Presentation

<br /> Focusing on the three aspects of text...

Detailed explanation of ActiveMQ deployment method in Linux environment

This article describes the deployment method of A...

Solve the problem that Mysql5.7.17 fails to install and start under Windows

Install MySQL for the first time on your machine....

How to hide the border/separation line between cells in a table

Only show the top border <table frame=above>...

Analysis of CocosCreator's new resource management system

Table of contents 1. Resources and Construction 1...

Understanding innerHTML

<br />Related articles: innerHTML HTML DOM i...

Detailed explanation of overflow:auto usage

Before starting the main text, I will introduce s...

mysql solves the problem of finding records where two or more fields are NULL

Core code /*-------------------------------- Find...

Steps to transfer files and folders between two Linux servers

Today I was dealing with the issue of migrating a...

Reasons and solutions for prompting to save action after uploading files in form

The json data must be returned in html format That...

How to implement a lucky wheel game in WeChat applet

I mainly introduce how to develop a lucky wheel g...

How to import/save/load/delete images locally in Docker

1. Docker imports local images Sometimes we copy ...