CSS3 动画 animation(二)

7 篇文章 0 订阅
7 篇文章 0 订阅

这是一个比较简单的例子

先看一个简单的例子

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>CSS3 动画</title>
    <style>
        div {
	    width: 200px;
	    height: 200px;
	    background-color:red;
	    position: relative;

	    animation-name: an;			    /*  动画名称       */
	    animation-duration:5s;		    /*  运动时间为5s   */
	    animation-timing-function:linear;	    /*  运动速度曲线   */
	    animation-delay:0;			    /*  动画延除时间   */
	    animation-iteration-count:infinite;     /*  动画执行次数   */
	    animation-direction:alternate;          /*  动画逆向运动,这个是奇正,偶反的   */
        }
        @keyframes an {
	    0%  {background-color: red;left:0;top:0;}
	    35% {background-color: blue;left:300px;top:0;}
	    55% {background-color: yellow;left:300px;top: 300px;}
	    75% {background-color: pink;left:0;top:300px;}
	    100% {background-color: red;left:0;top:0;}
        }
        @-webkit-keyframes an {
	    0%  {background-color: red;left:0;top:0;}
	    35% {background-color: blue;left:300px;top:0;}
	    55% {background-color: yellow;left:300px;top: 300px;}
	    75% {background-color: pink;left:0;top:300px;}
	    100% {background-color: red;left:0;top:0;}
        }
</style>
</head>
<body>
    <div></div>
</body>
</html>

上次在@keyframes 中只是用了fromto作为开始与结束,这次用的是百分比,这个是最常用 的了,可以很方便地控制进度。

其实它们还有简写属性的:

animation一共有八个属性,其中有七个是可以简写成animation的。

另外一个不能简写的属性是animation-play-state属性

以上例子可以简写成 ,如下:

6行的代码一行就搞定了。

<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>CSS3 动画</title>
<style>
    div {
	width: 200px;
	height: 200px;
	background-color:red;
	position: relative;

	animation: an 5s linear 1s infinite alternate;  /* 这是简写的 */ 
    }
    @keyframes an {
	0%  {background-color: red;left:0;top:0;}
	35% {background-color: blue;left:300px;top:0;}
	55% {background-color: yellow;left:300px;top: 300px;}
	75% {background-color: pink;left:0;top:300px;}
	100% {background-color: red;left:0;top:0;}
    }
    @-webkit-keyframes an {
	0%  {background-color: red;left:0;top:0;}
	35% {background-color: blue;left:300px;top:0;}
	55% {background-color: yellow;left:300px;top: 300px;}
	75% {background-color: pink;left:0;top:300px;}
	100% {background-color: red;left:0;top:0;}
    }
</style>
</head>
<body>
    <div></div>
</body>
</html>

当然他们还是有默认值的:

上面动画中的用到的属性的默认值分别,如下:

animation-duration:0                   即是动画开始到结束默认是立刻结束的

animation-timing-function:ease   即是开始时慢,然后加快,在要结束前又变慢 ,慢---->快---->慢

animation-delay:0                       即是立刻运动         

animation-iteration-count:1         即是动画只运动一次

animation-direction:normal         即是动画正常运动

 

 

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值