首页 技术 正文
技术 2022年11月18日
0 收藏 699 点赞 4,253 浏览 2935 个字

在上次博文中已经讲了transition,其实animation与transition功能相同,都是通过改变元素
的属性来实现动画效果的。但是它们也有区别:transition是只能通过改变指定属性的开始值

与结束值,然后在这两个属性值之间进行平滑过渡的方式来实现动画效果。animation却可以通

过定义多个关键帧以及每个关键帧中元素的属性值来实现更为复杂的动画效果。

语法

animation: name duration timing-function delay iteration-count direction;
描述
animation-name 规定需要绑定到选择器的 keyframe 名称。
animation-duration 规定完成动画所花费的时间,以秒或毫秒计。
animation-timing-function 规定动画的速度曲线。
animation-delay 规定在动画开始之前的延迟。
animation-iteration-count 规定动画应该播放的次数。(infinite无限轮播)
animation-direction 规定是否应该轮流反向播放动画。

有理论不实践,那是纸上谈兵。下面让我们动手写一个简单的示例吧。

<!DOCTYPE HTML>
<html lang="en-US">
<head>
<meta charset="UTF-8">
<title> animation </title>
<style type="text/css">
/**/
div {margin:20px 0;}
#animate_1 {position:absolute; background-color:red;top:100px;width:500px;}
@-webkit-keyframes mycolor {
0% {
background-color:red;
}
40% {
background-color:darkblue;
}
70% {
background-color:yellow;
}
100% {
background-color:red;
} }
#animate_1:hover{
-webkit-animation: mycolor 5s linear infinite
} </style>
</head>
<body>
<div id="animate_1">animation_1</div>
</body>
</html>

看到了吧,上面的背景色变换,只需要通过animation的几个关键帧来实现动画的效果

当然animation可以同时改变多个属性值来实现比较复杂的动画效果。

<!DOCTYPE HTML>
<html lang="en-US">
<head>
<meta charset="UTF-8">
<title> animation </title>
<style type="text/css">
/**/
div {margin:20px 0;}
#animate_1 {position:absolute; background-color:red;top:100px;width:500px;}
@-webkit-keyframes mycolor {
0% {
background-color:red;
-webkit-transform: rotate(0deg);
}
40% {
background-color:darkblue;
-webkit-transform: rotate(30deg);
}
70% {
background-color:yellow;
-webkit-transform: rotate(-30deg);
}
100% {
background-color:red;
-webkit-transform:rotate(0deg);
} }
#animate_1:hover{
-webkit-animation: mycolor 5s linear infinite
} </style>
</head>
<body>
<div id="animate_1">animation_1</div>
</body>
</html>

效果是不是很酷,只要通过css就可以实现背景变色及元素变换角度。在过去那是实现这个效果
可就疯狂的写一大堆的javascript代码了。

下面说下实现动画的方法

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

语法

animation-timing-function: value;
描述
linear 动画从头到尾的速度是相同的。
ease 默认。动画以低速开始,然后加快,在结束前变慢。
ease-in 动画以低速开始。
动画以低速开始。 动画以低速结束。
ease-in-out 动画以低速开始和结束。
cubic-bezier(n,n,n,n)

在 cubic-bezier 函数中设置自己的值。可能的值是从 0 到 1 的数值。

<!DOCTYPE HTML>
<html lang="en-US">
<head>
<meta charset="UTF-8">
<title> animation </title>
<style type="text/css">
/**/
div {margin:20px 0;}
@-webkit-keyframes myanimate_2 {
0% {
width:100px;
height:100px;
}
100% {
width:500px;
height:500px;
}
}
#animate_2 {
width:500px;
height:500px;
background-color:red;
-webkit-animation:myanimate_2 5s ease-out;
} @-webkit-keyframes fadein {
0% {
opacity:0;
background-color:white;
}
100% {
opacity:1;
background-color:white;
}
}
</style>
</head>
<body>
<div id="animate_2">animation_2</div>
</body>
</html>

最后一个例子实现一个网站经常用到的动画效果–网页的淡入效果。

<!DOCTYPE HTML>
<html lang="en-US">
<head>
<meta charset="UTF-8">
<title> animation </title>
<style type="text/css">
/**/
div {margin:20px 0;} @-webkit-keyframes fadein {
0% {
opacity:0;
background-color:white;
}
100% {
opacity:1;
background-color:white;
}
}
body {
-webkit-animation:fadein 5s linear 1;
}
</style>
</head>
<body>
<div id="animate_1">animation_1</div>
<div id="animate_2">animation_2</div>
</body>
</html>

上面的例子只是一些很基本的动画效果示例。大家对css3的动画效果感兴趣的可以结合我前一次的分享的transition一起实现更加有趣的动画效果。期待大家动手试试吧!

相关推荐
python开发_常用的python模块及安装方法
adodb:我们领导推荐的数据库连接组件bsddb3:BerkeleyDB的连接组件Cheetah-1.0:我比较喜欢这个版本的cheeta…
日期:2022-11-24 点赞:878 阅读:9,084
Educational Codeforces Round 11 C. Hard Process 二分
C. Hard Process题目连接:http://www.codeforces.com/contest/660/problem/CDes…
日期:2022-11-24 点赞:807 阅读:5,559
下载Ubuntn 17.04 内核源代码
zengkefu@server1:/usr/src$ uname -aLinux server1 4.10.0-19-generic #21…
日期:2022-11-24 点赞:569 阅读:6,408
可用Active Desktop Calendar V7.86 注册码序列号
可用Active Desktop Calendar V7.86 注册码序列号Name: www.greendown.cn Code: &nb…
日期:2022-11-24 点赞:733 阅读:6,181
Android调用系统相机、自定义相机、处理大图片
Android调用系统相机和自定义相机实例本博文主要是介绍了android上使用相机进行拍照并显示的两种方式,并且由于涉及到要把拍到的照片显…
日期:2022-11-24 点赞:512 阅读:7,818
Struts的使用
一、Struts2的获取  Struts的官方网站为:http://struts.apache.org/  下载完Struts2的jar包,…
日期:2022-11-24 点赞:671 阅读:4,901