我们可以使用CSS动画动画转换从一个CSS样式配置到另一个。
动画由三部分组成:
要创建CSS动画序列,我们使用动画缩写属性或其他动画相关属性对元素进行样式化。
我们可以配置动画的时间和持续时间,以及动画序列应该如何进展的其他细节。
动画的实际外观是使用 @keyframes
规则完成的。
下表列出了 @keyframes
规则和所有动画属性:
此示例显示如何使用CSS动画来创建 H1
元素在页面上移动。
<!doctype html>
<html>
<head>
<style type="text/css">
h1 {
-moz-animation-duration: 3s;
-webkit-animation-duration: 3s;
-moz-animation-name: slidein;
-webkit-animation-name: slidein;
}
@-moz-keyframes slidein {
from {
margin-left:100%;
width:300%
}
to {
margin-left:0%;
width:100%;
}
}
@-webkit-keyframes slidein {
from {
margin-left:100%;
width:300%
}
to {
margin-left:0%;
width:100%;
}
}
</style>
</head>
<body>
<h1>Watch me move</h1>
</body>
</html>
此示例显示如何使用CSS动画来创建 H1
元素在页面上移动并放大文本大小。
<!doctype html>
<html>
<head>
<title>CSS animations: Example 2</title>
<style type="text/css">
h1 {
-moz-animation-duration: 3s;
-webkit-animation-duration: 3s;
-moz-animation-name: slidein;
-webkit-animation-name: slidein;
}
@-moz-keyframes slidein {
from {
margin-left:100%;
width:300%
}
75% {
font-size:300%;
margin-left:25%;
width:150%;
}
to {
margin-left:0%;
width:100%;
}
}
@-webkit-keyframes slidein {
from {
margin-left:100%;
width:300%
}
75% {
font-size:300%;
margin-left:25%;
width:150%;
}
to {
margin-left:0%;
width:100%;
}
}
</style>
</head>
<body>
<h1>Watch me move</h1>
</body>
</html>
为了使动画重复,请使用 animation-iteration-count
属性以指示重复动画的次数。
以下代码使用infinite
使动画重复无限:
<!doctype html>
<html>
<head>
<title>CSS animations: Example 3</title>
<style type="text/css">
h1 {
-moz-animation-duration: 3s;
-webkit-animation-duration: 3s;
-moz-animation-name: slidein;
-webkit-animation-name: slidein;
-moz-animation-iteration-count: infinite;
-webkit-animation-iteration-count: infinite;
}
@-moz-keyframes slidein {
from {
margin-left:100%;
width:300%
}
75% {
font-size:300%;
margin-left:25%;
width:150%;
}
to {
margin-left:0%;
width:100%;
}
}
@-webkit-keyframes slidein {
from {
margin-left:100%;
width:300%
}
75% {
font-size:300%;
margin-left:25%;
width:150%;
}
to {
margin-left:0%;
width:100%;
}
}
</style>
</head>
<body>
<h1>Watch me move</h1>
</body>
</html>
上面的代码呈现如下:
要在屏幕上来回移动,我们可以将 animation-direction
设置为 alternate
。
<!doctype html>
<html>
<head>
<title>CSS animations: Example 4</title>
<style type="text/css">
h1 {
-moz-animation-duration: 3s;
-webkit-animation-duration: 3s;
-moz-animation-name: slidein;
-webkit-animation-name: slidein;
-moz-animation-iteration-count: infinite;
-webkit-animation-iteration-count: infinite;
-moz-animation-direction: alternate;
-webkit-animation-direction: alternate;
}
@-moz-keyframes slidein {
from {
margin-left:100%;
width:300%
}
75% {
font-size:300%;
margin-left:25%;
width:150%;
}
to {
margin-left:0%;
width:100%;
}
}
@-webkit-keyframes slidein {
from {
margin-left:100%;
width:300%
}
75% {
font-size:300%;
margin-left:25%;
width:150%;
}
to {
margin-left:0%;
width:100%;
}
}
</style>
</head>
<body>
<h1>Watch me move</h1>
</body>
</html>
上面的代码呈现如下:
属性 | 描述 | 描述... |
---|---|---|
animation-delay | 动画开始时设置 | 3 |
animation-direction | 在交替循环上反向播放动画 | 3 |
animation-duration | 在一个周期中为动画设置持续时间(秒)或毫秒(ms) | 3 |
animation-fill-mode | 设置动画使用的值不播放 | 3 |
animation-iteration-count | 设置播放动画的次数 | 3 |
animation-name | 设置@keyframes动画的名称 | 3 |
animation-play-state | 运行或暂停动画 | 3 |
animation-timing-function | 设置动画的速度曲线 | 3 |
animation | 所有动画属性的速记属性 | 3 |
@keyframes | 创建动画的关键帧 | 3 |
都说用 DIV+CSS 布局来设计网站便于优化,容易被收录,那么你对 DIV+CSS 布局的用法是否了解?CSS 布局是网页html通过 DIV 标签 ...
footer 标签一般用于网页的底部布局,表示页脚,包含与文档相关的信息。实例文档的页脚:footer pPosted by: Hege Refsnes/p pti...
HTML tbody 标签char 属性用来指定 tbody 标签内的元素该以哪个字符对齐,请参考下述例子:实例把 tbody 元素中的内容与字符 "."...
HTML style 标签通过 type 属性设置HTML样式表的 MIME 类型,请参考下述示例:实例在 style 标签中使用 type 属性:style type="...
HTML5 新的表单元素HTML5 有以下新的表单元素:datalistkeygenoutput注意:不是所有的浏览器都支持HTML5 新的表单元素,但是你可以...
HTMLbig标签 -HTML5 不支持实例让文本比常规的字体大一号:!DOCTYPE htmlhtmlheadmetacharset="utf-8"title编程狮(.cn)/title/he...
SQL TOP、LIMIT 和 ROWNUM 子句SQL TOP 子句用于从一张数据表中取回前 N 个或者 X% 的记录。注意:所有的数据库系统都不支持 TOP...
SQL SUM 函数SUM函数用于找出表中记录在某字段处的总和。要理解 SUM 函数,请考虑 employee_tbl 表,表中记录如下所示:SQL SELE...