search
HomeWeb Front-endCSS TutorialDetailed introduction to CSS3 animation animation related properties and keyframe rules keyframes

When I wrote the three-dimensional cube yesterday, I used the syntax of animation

Come to the system todayReview
Transition has Its limitation
is simple, but it can only change between two states
and it requires the driver of the event to be able to
Cannot move by itself
So in order to solve this problem
We need animation animation

Animation

If you want to achieve animation effects
Only animationAttributes is not enough
We also need@keyframes rules
Let’s look at an example first

p class="demo"></p>
.demo {    width: 100px;    height: 100px;    background-color: gold;}.demo:hover {    animation: change 2s linear;}@keyframes change {
    0% {        background-color: red;    }
    50% {        background-color: purple;    }
    100% {        background-color: lime;    }}

When the mouse is hovered, the element first turns red and then transitions There is a transition from purple to green

Let’s take a look at @keyframes rules first

keyframes

In @keyframes, we define animation keyframes
Then the animation will perform transition execution according to the frame state we specify in the keyframes keyframes
0% - 100% represents the time transition of the animation
0% and 100% in the rules,
can be replaced by from and to keywords

@keyframes xxx {    from {        ......
    }
    to {
        ......
    }
}

If we omit the starting frame, the browser will transition according to its original style

@keyframes change {
    100% {        background-color: lime;    }}


In addition, we can also write the same frames together like this

@keyframes change {    from,to {        background-color: red;    }
    50% {        background-color: blue;    }}

animation

animation is a composite attribute with the following sub-attributes

  • animation-name
    Specify the name of the keyframes animation

  • animation-duration
    Specify the animation execution time

  • animation-timing-function
    Specify the speed curve of the animation, the default is "ease" easing

  • animation-delay
    Specify animation delay time, default "0" no delay

  • animation-iteration-count
    Specify the number of times the animation is played, the default is "1" to execute once

  • animation-direction
    Specifies the animation Execution direction, the default is "normal"

This composite attribute is more complicated than our transition
The first four attributes will not be explained much, they are similar to our transition
For those who have forgotten, click here -> Portal

animation-iteration-count In addition to filling in the number
, we can also use a commonly used keyword infiniteloop

animation-direction has the following attribute values ​​in addition to normal

  • reverse
    Reverse animation

  • alternate
    Play animation in turn

  • ##alternate-reverse Play animation in reverse turn

Explained through an example

.demo {    width: 100px;    height: 100px;    background-color: gold;}.demo:hover {    animation: change 1s 2 linear;}@keyframes change {    to {        width: 200px;    }}

Default

normal:
Two test animations:
100px -> 200px
100px -> 200px

.demo:hover {    animation: change 1s 2 linear reverse; /*改*/}

reverse
Two test animations:
200px -> ; 100px
200px -> 100px

.demo:hover {    animation: change 1s 2 linear alternate; /*改*/}

alternate
Two test animations:
100px -> 200px
200px -> 100px

.demo:hover {    animation: change 1s 2 linear alternate-reverse; /*改*/}

alternate-reverse
Two test animations:
200px -> 100px
100px -> 200px

animation-fill-mode

The two properties I am going to talk about below are not sub-properties of animation

So they cannot be written in animation

animation-fill-mode specifies

the state of the object outside the animation time, the default is "none" In addition to none, there are the following attribute values ​​

  • forwards After the animation is completed, keep the last property (defined in the last frame)

  • backwards  
    在animation-delay指定时间内、动画显示之前,应用起始属性(定义在第一帧)

  • both  
    应用forwards和backwards两种模式


forwards
这个属性还是蛮有用的
还是我们上面的例子

.demo:hover {    animation: change 1s linear; /*改*/
    animation-fill-mode: forwards; /*改*/}


我们发现1s之后,元素并没有回到最初的100px,而是保持了我们最后一帧的200px状态


backwards  
理解这个属性,我们需要先加一个延时

.demo:hover {    animation: change 1s linear 1s; /*改*/
    /*animation-fill-mode: backwards;*/ /*待增*/}@keyframes change {    from {  /*增*/
        width: 150px;    }
    to {        width: 200px;    }}

我就不配图了
我们发现鼠标悬浮后,1s后瞬间变为150px,然后再过渡到200px
hover-0s -> 1s -> 2s
100px ->瞬变150px –> 过渡到200px

现在增加backwards

.demo:hover {    animation: change 1s linear 1s; /*改*/
    animation-fill-mode: backwards; /*增*/}

这回我们发现鼠标悬浮的一瞬间就变为15px,然后1s后过渡到200px
hover-0s -> 1s -> 2s
瞬变150px ->150px –> 过渡到200px
这就是backwards的作用,延迟前就应用第一帧动画的样式,然后准备过渡

animation-play-state

animation-play-state    指定动画的运行或暂停。默认 “running”
除了running还有paused
利用这个属性再配合js我们可以控制动画的暂停和运行

demo.style.animationPlayState = "paused";

今天的动画就先写这么多,感觉写了很长时间
日后再总结动画相关的性能问题

The above is the detailed content of Detailed introduction to CSS3 animation animation related properties and keyframe rules keyframes. For more information, please follow other related articles on the PHP Chinese website!

Statement
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
Draggin' and Droppin' in ReactDraggin' and Droppin' in ReactApr 17, 2025 am 11:52 AM

The React ecosystem offers us a lot of libraries that all are focused on the interaction of drag and drop. We have react-dnd, react-beautiful-dnd,

Fast SoftwareFast SoftwareApr 17, 2025 am 11:49 AM

There have been some wonderfully interconnected things about fast software lately.

Nested Gradients with background-clipNested Gradients with background-clipApr 17, 2025 am 11:47 AM

I can't say I use background-clip all that often. I'd wager it's hardly ever used in day-to-day CSS work. But I was reminded of it in a post by Stefan Judis,

Using requestAnimationFrame with React HooksUsing requestAnimationFrame with React HooksApr 17, 2025 am 11:46 AM

Animating with requestAnimationFrame should be easy, but if you haven’t read React’s documentation thoroughly then you will probably run into a few things

Need to scroll to the top of the page?Need to scroll to the top of the page?Apr 17, 2025 am 11:45 AM

Perhaps the easiest way to offer that to the user is a link that targets an ID on the element. So like...

The Best (GraphQL) API is One You WriteThe Best (GraphQL) API is One You WriteApr 17, 2025 am 11:36 AM

Listen, I am no GraphQL expert but I do enjoy working with it. The way it exposes data to me as a front-end developer is pretty cool. It's like a menu of

Weekly Platform News: Text Spacing Bookmarklet, Top-Level Await, New AMP Loading IndicatorWeekly Platform News: Text Spacing Bookmarklet, Top-Level Await, New AMP Loading IndicatorApr 17, 2025 am 11:26 AM

In this week's roundup, a handy bookmarklet for inspecting typography, using await to tinker with how JavaScript modules import one another, plus Facebook's

Various Methods for Expanding a Box While Preserving the Border RadiusVarious Methods for Expanding a Box While Preserving the Border RadiusApr 17, 2025 am 11:19 AM

I've recently noticed an interesting change on CodePen: on hovering the pens on the homepage, there's a rectangle with rounded corners expanding in the back.

See all articles

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

AI Hentai Generator

AI Hentai Generator

Generate AI Hentai for free.

Hot Article

R.E.P.O. Energy Crystals Explained and What They Do (Yellow Crystal)
1 months agoBy尊渡假赌尊渡假赌尊渡假赌
R.E.P.O. Best Graphic Settings
1 months agoBy尊渡假赌尊渡假赌尊渡假赌
R.E.P.O. How to Fix Audio if You Can't Hear Anyone
1 months agoBy尊渡假赌尊渡假赌尊渡假赌
R.E.P.O. Chat Commands and How to Use Them
1 months agoBy尊渡假赌尊渡假赌尊渡假赌

Hot Tools

SecLists

SecLists

SecLists is the ultimate security tester's companion. It is a collection of various types of lists that are frequently used during security assessments, all in one place. SecLists helps make security testing more efficient and productive by conveniently providing all the lists a security tester might need. List types include usernames, passwords, URLs, fuzzing payloads, sensitive data patterns, web shells, and more. The tester can simply pull this repository onto a new test machine and he will have access to every type of list he needs.

PhpStorm Mac version

PhpStorm Mac version

The latest (2018.2.1) professional PHP integrated development tool

DVWA

DVWA

Damn Vulnerable Web App (DVWA) is a PHP/MySQL web application that is very vulnerable. Its main goals are to be an aid for security professionals to test their skills and tools in a legal environment, to help web developers better understand the process of securing web applications, and to help teachers/students teach/learn in a classroom environment Web application security. The goal of DVWA is to practice some of the most common web vulnerabilities through a simple and straightforward interface, with varying degrees of difficulty. Please note that this software

Dreamweaver Mac version

Dreamweaver Mac version

Visual web development tools

Dreamweaver CS6

Dreamweaver CS6

Visual web development tools