Table of Contents
animation-name
animation-duration
animation-timing-function
animation-delay
animation-iteration-count
animation-direction
animation-play-state
animation
Home Web Front-end HTML Tutorial CSS3 动画_html/css_WEB-ITnose

CSS3 动画_html/css_WEB-ITnose

Jun 24, 2016 am 11:35 AM

概览

通过 CSS3,我们能够创建动画,这可以在许多网页中取代动画图片、Flash 动画以及 JavaScript。

由于是CSS3 嘛,所以部分旧版本浏览器当然无法完美呈现,节哀。

小试牛刀

学习任何东西都需要有一定的成就感才会有继续学习的动力,先别管那么多,先让我们的动画动起来。

<!DOCTYPE html><html><head><style>    @keyframes myfirst {        from {background:red;}        to {background:yellow;}    }    /* Firefox */    @-moz-keyframes myfirst {        from {background:red;}        to {background:yellow;}    }    /* Safari and Chrome */    @-webkit-keyframes myfirst {        from {background:red;}        to {background:yellow;}    }    /* Opera */    @-o-keyframes myfirst {        from {background:red;}        to {background:yellow;}    }    div {        width:100px;        height:100px;        margin: 50px auto;        background:red;        animation:myfirst 5s;        -moz-animation:myfirst 5s; /* Firefox */        -webkit-animation:myfirst 5s; /* Safari and Chrome */        -o-animation:myfirst 5s; /* Opera */    }</style></head><body><div></div></body></html>
Copy after login

是不是很简单,很炫酷呀?

实现CSS3 动画需要至少以下几个条件:

  • 使用 @keyframes 创建动画并命名

  • 使用 animation简写属性 或 其他具体属性 调用动画并设置动画时长

  • 将 animation 绑定到某个选择器上

下面具体介绍各相关属性吧。

创建动画 @keyframes

通过 @keyframes 规则,您能够创建动画。

创建动画的原理是将一套 CSS 样式逐渐变化为另一套样式。

在动画过程中,您能够多次改变这套 CSS 样式。

以百分比来规定改变发生的时间,或者通过关键词 from 和 to来规定改变发生的时间。

0% 是动画的开始时间,100% 动画的结束时间。

为了获得最佳的浏览器支持,您应该始终定义 0% 和 100% 选择器。

@keyframes myfirst {    0%   {background: red; left:0px; top:0px;}    25%  {background: yellow; left:200px; top:0px;}    50%  {background: blue; left:200px; top:200px;}    75%  {background: green; left:0px; top:200px;}    100% {background: red; left:0px; top:0px;}}/* Firefox */@-moz-keyframes myfirst {    0%   {background: red; left:0px; top:0px;}    25%  {background: yellow; left:200px; top:0px;}    50%  {background: blue; left:200px; top:200px;}    75%  {background: green; left:0px; top:200px;}    100% {background: red; left:0px; top:0px;}}/* Safari 和 Chrome */@-webkit-keyframes myfirst {    0%   {background: red; left:0px; top:0px;}    25%  {background: yellow; left:200px; top:0px;}    50%  {background: blue; left:200px; top:200px;}    75%  {background: green; left:0px; top:200px;}    100% {background: red; left:0px; top:0px;}}/* Opera */@-o-keyframes myfirst {    0%   {background: red; left:0px; top:0px;}    25%  {background: yellow; left:200px; top:0px;}    50%  {background: blue; left:200px; top:200px;}    75%  {background: green; left:0px; top:200px;}    100% {background: red; left:0px; top:0px;}}
Copy after login

调用动画 animation

上面我们使用 @keyframes 创建了动画,接下来我们来调用动画。

上面也说了,调用动画最基本的是动画名称和动画花费的时间,下面将具体介绍动画调用的相关属性。

animation-name

指定要调用的动画。

animation-name: keyframename | none;
Copy after login

none 规定无动画效果(可用于覆盖来自级联的动画)。

keyframename 命名遵循如下规则:

名字可以是字母,数字,_ 或 -,区分大小写,只能以字母或单-开头,不能使用none,unset,initial,inherit关键字。

animation-duration

animation-duration 属性定义动画完成一个周期所需要的时间,以秒或毫秒计。

animation-duration: 2s; /*等价于 2000ms*/
Copy after login

animation-timing-function

animation-timing-function 规定动画的速度曲线。

animation-timing-function: value;
Copy after login

此属性值使用名为三次贝塞尔(Cubic Bezier)函数的数学函数来生成速度曲线。

有如下值可选:

描述
linear 动画从头到尾的速度是相同的。
ease 默认。动画以低速开始,然后加快,在结束前变慢。
ease-in 动画以低速开始。
ease-out 动画以低速结束。
ease-in-out 动画以低速开始和结束。
cubic-bezier(n, n, n, n) 在 cubic-bezier 函数中自己的值。可能的值是从 0 到 1 的数值。

5 个预定义关键字对应的贝塞尔函数为:

linear: cubic-bezier(0.0, 0.0, 1.0, 1.0)ease: cubic-bezier(0.25, 0.1, 0.25, 1.0)ease-in: cubic-bezier(0.42, 0, 1.0, 1.0)ease-out: cubic-bezier(0, 0, 0.58, 1.0)ease-in-out: cubic-bezier(0.42, 0, 0.58, 1.0)
Copy after login

简单体会一下 5 种速度曲线的效果:

想亲自去体验各种值对速度的影响,请移步这里:贝塞尔速度曲线

animation-delay

animation-delay 属性定义动画何时开始。

animation-delay: time;
Copy after login

值以秒或毫秒计。允许负值,-2s 使动画马上开始,但跳过 2 秒进入动画。

animation-iteration-count

animation-iteration-count 属性定义动画的播放次数。

animation-iteration-count: n | infinite;
Copy after login

n 表示具体的次数,默认为1,infinite 规定无限次播放。

animation-direction

animation-direction 属性定义是否应该轮流反向播放动画。

animation-direction: normal | reverse | alternate | alternate-reverse;
Copy after login

两个关键字可选,normal 表示动画正常播放,默认值,从 0% -> 100% 再从 0% -> 100%. reverse 与 normal 相反,从 100% -> 0% 再从 100% -> 0%. alternate 表示轮流反向播放,从 0% -> 100% 再从 100% -> 0% 再从 0% -> 100%. alternate-reverse 与 alternate 相反。

animation-play-state

animation-play-state 属性规定动画正在运行还是暂停。

animation-play-state: paused | running;
Copy after login

paused 表示动画正在暂停,动画不会动。running 表示动画正在动,默认。

animation

此属性为上述七个具体属性的简写属性。

animation: name duration timing-function delay iteration-count direction play-state;
Copy after login
小结

对CSS3 动画先简单了解这么多,后续可能有新内容再补充。

参考资料
  • CSS3 动画

  • cubic-bezier贝塞尔曲线CSS3动画工具

  • Cubic-Bezier

  • Mozilla animation

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

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 ago By 尊渡假赌尊渡假赌尊渡假赌
R.E.P.O. Best Graphic Settings
1 months ago By 尊渡假赌尊渡假赌尊渡假赌
R.E.P.O. How to Fix Audio if You Can't Hear Anyone
1 months ago By 尊渡假赌尊渡假赌尊渡假赌
R.E.P.O. Chat Commands and How to Use Them
1 months ago By 尊渡假赌尊渡假赌尊渡假赌

Hot Tools

Notepad++7.3.1

Notepad++7.3.1

Easy-to-use and free code editor

SublimeText3 Chinese version

SublimeText3 Chinese version

Chinese version, very easy to use

Zend Studio 13.0.1

Zend Studio 13.0.1

Powerful PHP integrated development environment

Dreamweaver CS6

Dreamweaver CS6

Visual web development tools

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)

What is the purpose of the <progress> element? What is the purpose of the <progress> element? Mar 21, 2025 pm 12:34 PM

The article discusses the HTML &lt;progress&gt; element, its purpose, styling, and differences from the &lt;meter&gt; element. The main focus is on using &lt;progress&gt; for task completion and &lt;meter&gt; for stati

What are the best practices for cross-browser compatibility in HTML5? What are the best practices for cross-browser compatibility in HTML5? Mar 17, 2025 pm 12:20 PM

Article discusses best practices for ensuring HTML5 cross-browser compatibility, focusing on feature detection, progressive enhancement, and testing methods.

What is the purpose of the <datalist> element? What is the purpose of the <datalist> element? Mar 21, 2025 pm 12:33 PM

The article discusses the HTML &lt;datalist&gt; element, which enhances forms by providing autocomplete suggestions, improving user experience and reducing errors.Character count: 159

How do I use HTML5 form validation attributes to validate user input? How do I use HTML5 form validation attributes to validate user input? Mar 17, 2025 pm 12:27 PM

The article discusses using HTML5 form validation attributes like required, pattern, min, max, and length limits to validate user input directly in the browser.

What is the purpose of the <meter> element? What is the purpose of the <meter> element? Mar 21, 2025 pm 12:35 PM

The article discusses the HTML &lt;meter&gt; element, used for displaying scalar or fractional values within a range, and its common applications in web development. It differentiates &lt;meter&gt; from &lt;progress&gt; and ex

What is the viewport meta tag? Why is it important for responsive design? What is the viewport meta tag? Why is it important for responsive design? Mar 20, 2025 pm 05:56 PM

The article discusses the viewport meta tag, essential for responsive web design on mobile devices. It explains how proper use ensures optimal content scaling and user interaction, while misuse can lead to design and accessibility issues.

What is the purpose of the <iframe> tag? What are the security considerations when using it? What is the purpose of the <iframe> tag? What are the security considerations when using it? Mar 20, 2025 pm 06:05 PM

The article discusses the &lt;iframe&gt; tag's purpose in embedding external content into webpages, its common uses, security risks, and alternatives like object tags and APIs.

Is HTML easy to learn for beginners? Is HTML easy to learn for beginners? Apr 07, 2025 am 12:11 AM

HTML is suitable for beginners because it is simple and easy to learn and can quickly see results. 1) The learning curve of HTML is smooth and easy to get started. 2) Just master the basic tags to start creating web pages. 3) High flexibility and can be used in combination with CSS and JavaScript. 4) Rich learning resources and modern tools support the learning process.

See all articles