css3--动画

CSS3,我们可以创建动画,它可以取代许多网页动画图像,Flash动画,和JAVAScripts。

CSS3 @keyframes 规则

要创建CSS3动画,你将不得不了解@keyframes规则。

@keyframes规则是创建动画。 @keyframes规则内指定一个CSS样式和动画将逐步从目前的样式更改为新的样式。

@keyframes myfirst
{
    from {background: red;}
    to {background: yellow;}
}
 
@-webkit-keyframes myfirst /* Safari 与 Chrome */
{
    from {background: red;}
    to {background: yellow;}
}

CSS3 动画

当在 @keyframes 创建动画,把它绑定到一个选择器,否则动画不会有任何效果。

指定至少这两个CSS3的动画属性绑定向一个选择器:

  • 规定动画的名称
  • 规定动画的时长
<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>css3动画</title>
    <style type="text/css">
     * {
       margin: 0;
       padding: 0;
     } 
     
     div {
       width: 100px;
       height: 100px;
       background: red;
       animation: myfirst 5s; /*规定动画的名称:myfirst 规定动画的时长: 5s*/
       -webkit-animation: myfirst 5s;
     }
     
     @-webkit-keyframes myfirst {
       from {background: red}
       to {background: yellow}
     }
     
     @keyframes myfirst {
       form {background: red}
       to {background: yellow}
     }
     
    </style>
</head>
<body>
  <div></div>
</body>
</html>

 您必须定义动画的名称和动画的持续时间。如果省略的持续时间,动画将无法运行,因为默认值是0。

CSS3动画是什么?

动画是使元素从一种样式逐渐变化为另一种样式的效果。

您可以改变任意多的样式任意多的次数。

请用百分比来规定变化发生的时间,或用关键词 "from" 和 "to",等同于 0% 和 100%。

0% 是动画的开始,100% 是动画的完成。

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

当动画为 25% 及 50% 时改变背景色,然后当动画 100% 完成时再次改变:

<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8"> 
<title>css3 过渡</title>  
<style type="text/css">
  * {
    margin: 0;
    padding: 0;
  }

  div {
    width: 100px;
    height: 100px;
    background: red;
    -moz-animation: myfirst 5s; /* Firefox*/
    -webkit-animation: myfirst 5s; /* Safari and Chrome*/
    -o-animation: myfirst 5s; /* Opera */
    animation: myfirst 5s;
  }

  @-moz-keyframes myfirst { /* Firefox*/
    0% {background: red;}
    25% {background: yellow;}
    50% {background: blue;}
    100% {background: green;}
  }
  
  @-webkit-keyframes myfirst { /* Safari and Chrome*/
    0% {background: red;}
    25% {background: yellow;}
    50% {background: blue;}
    100% {background: green;}
  }

  @-o-keyframes myfirst { /* Opera */
    0% {background: red;}
    25% {background: yellow;}
    50% {background: blue;}
    100% {background: green;}
  }

  @keyframes myfirst { 
    0% {background: red;}
    25% {background: yellow;}
    50% {background: blue;}
    100% {background: green;}
  }
</style>
</head>
<body>
  <div></div>
</body>
</html>

改变背景色和位置:

<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8"> 
<title>css3 过渡</title>  
<style type="text/css">
  * {
    margin: 0;
    padding: 0;
  }

  div {
    position: relative;
    width: 100px;
    height: 100px;
    background: red;
    -moz-animation: myfirst 5s; /* Firefox*/
    -webkit-animation: myfirst 5s; /* Safari and Chrome*/
    -o-animation: myfirst 5s; /* Opera */
    animation: myfirst 5s;
  }

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

  @-o-keyframes myfirst { /* Opera */
    0% {background: red; left: 0; top: 0;}
    25% {background: yellow; left: 200px; top: 0;}
    50% {background: blue; left: 200px; top: 200px;}
    75% {background: green; left: 0; top: 200px;}
    100% {background: red; left: 0; top: 0;}
  }

  @keyframes myfirst { 
    0% {background: red; left: 0; top: 0;}
    25% {background: yellow; left: 200px; top: 0;}
    50% {background: blue; left: 200px; top: 200px;}
    75% {background: green; left: 0; top: 200px;}
    100% {background: red; left: 0; top: 0;}
  }
</style>
</head>
<body>
  <div></div>
</body>
</html>

CSS3的动画属性

下面的表格列出了 @keyframes 规则和所有动画属性:

属性描述CSS
@keyframes 规定动画。 3
animation 所有动画属性的简写属性,除了 animation-play-state 属性。 3
animation-name 规定 @keyframes 动画的名称。 3
animation-duration 规定动画完成一个周期所花费的秒或毫秒。默认是 0。 3
animation-timing-function 规定动画的速度曲线。默认是 "ease"。 3
animation-delay 规定动画何时开始。默认是 0。 3
animation-iteration-count 规定动画被播放的次数。默认是 1。 3
animation-direction 规定动画是否在下一周期逆向地播放。默认是 "normal"。 3
animation-play-state 规定动画是否正在运行或暂停。默认是 "running"。 3

将逐个属性再重新写一遍

@keyframes 规定动画从一个样式到另外一种样式的效果

@keyframes myfirst {
     from {background: red;}
     to {background: orange;}
  }

from代表0% to代表100% 从0%到100%期间背景颜色由红色变成橙色,注意必须指定动画一个周期的时间 (默认为0),所以不写动画时间就看不出效果

animation: myfirst 5s

设置时间为5s 这样动画到结束需要5s时间

 animaton

使用简写属性把 animation 绑定到一个<div> 元素:

<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8"> 
<title>css3 动画</title>  
<style type="text/css">
  * {
    margin: 0;
    padding: 0;
  }

  div {
    position: relative;
    width: 200px;
    height: 200px;
    background: red;
    -webkit-transition: mymove 5s infinite;
    animation: mymove 5s infinite;
  }

  @-webkit-keyframes mymove {
     from {left: 0;}
     to {left: 200px;}
  }

  @keyframes mymove {
     from {left: 0;}
     to {left: 200px;}
  }
</style>
</head>
<body>
  <div></div>
</body>
</html>

这个动画会重复执行下去

 

animation-name

 为 @keyframes 动画指定一个名称并且规定动画完成一个周期所花费的秒或毫秒

<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8"> 
<title>css3 动画</title>  
<style type="text/css">
  * {
    margin: 0;
    padding: 0;
  }

  div {
    position: relative;
    width: 100px;
    height: 100px;
    background: red;
    -webkit-animation-name: mymove; /* 指定名称 */
    -webkit-animation-duration: 5s;/*规定动画完成一个周期所花费的秒*/
    animation-name: mymove;
    animation-duration: 5s;
    
  }
  
  @-webkit-keyframes mymove {
    from {left: 0;}
    to {left: 200px;}
  }

  @keyframes mymove {
    from {left: 0;}
    to {left: 200px;}
  }

</style>
</head>
<body>
  <div></div>
</body>
</html>

animation-ttiming-function

从开始到结束以相同的速度播放动画:

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>css动画</title>
    <style type="text/css">
     * {
       margin: 0;
       padding: 0;
     } 
     
     div {
       width: 100px;
       height: 100px;
       background: red;
       position: relative;
       -webkit-animation: mymove 5s infinite; 
       -webkit-timing-function: linear;/*开始到结束都用相同的速度*/
       animation: mymove 5s infinite;
       animation-timing-function: linear;
     }
     
     @-webkit-keyframes mymove {
       from {left: 0}
       to {left: 200px}
     }
     
     @keyframes mymove {
       from {left: 0}
       to {left: 200px}
     }
    </style>
</head>
<body>
  <div></div>
</body>
</html>

 animation-delay

规定动画何时开始。默认是 0。

<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8"> 
<title>css3 动画</title>  
<style type="text/css">
  * {
    margin: 0;
    padding: 0;
  }

  div {
    position: relative;
    width: 100px;
    height: 100px;
    background: red;
    -webkit-animation: mymove 5s infinite;
    -webkit-animation-delay: 2s;
    animation-name: mymove 5s infinite;
    animation-delay: 2s; /*2s后进入动画*/
    
  }
  
  @-webkit-keyframes mymove {
    from {left: 0;}
    to {left: 200px;}
  }

  @keyframes mymove {
    from {left: 0;}
    to {left: 200px;}
  }

</style>
</head>
<body>
  <div></div>
</body>
</html>

 animation-iteration-count属性

定义应该播放多少次动画

<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8"> 
<title>css3 动画</title>  
<style type="text/css">
  * {
    margin: 0;
    padding: 0;
  }

  div {
    position: relative;
    width: 100px;
    height: 100px;
    background: red;
    -webkit-animation: mymove 5s ;
    -webkit-animation-iteration-count: 3;
    animation-name: mymove 5s;
    animation-iteration-count: 3; /*执行3次动画 infinite是无限播放*/
    
  }
  
  @-webkit-keyframes mymove {
    from {left: 0;}
    to {left: 200px;}
  }

  @keyframes mymove {
    from {left: 0;}
    to {left: 200px;}
  }

</style>
</head>
<body>
  <div></div>
</body>
</html>

 animation-direction: alternate;

先执行一遍动画,然后再反向执行一遍动画:

<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8"> 
<title>css3 过渡</title>  
<style type="text/css">
  * {
    margin: 0;
    padding: 0;
  }

  div {
    position: relative;
    width: 100px;
    height: 100px;
    background: red;
    -webkit-animation: mymove 5s infinite;
    -webkit-direction: alternate;
    animation-name: mymove 5s infinite;
    animation-direction: alternate;
    
  }
  
  @-webkit-keyframes mymove {
    from {left: 0; top: 0;}
    to {left: 200px; top: 40px;}
  }

  @keyframes mymove {
    from {left: 0; top: 0;}
    to {left: 200px; top: 40px;}
  }

</style>
</head>
<body>
  <div></div>
</body>
</html>

暂停动画:

animation-play-staye

<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8"> 
<title>css3 动画</title>  
<style type="text/css">
  * {
    margin: 0;
    padding: 0;
  }

  div {
    position: relative;
    width: 100px;
    height: 100px;
    background: red;
    -webkit-animation: mymove 5s;
    -webkit-animation-play-state: paused;
    animation-name: mymove 5s;
    animation-play-state: paused; /*暂停动画*/
    
  }
  
  @-webkit-keyframes mymove {
    from {left: 0; top: 0;}
    to {left: 200px; top: 40px;}
  }

  @keyframes mymove {
    from {left: 0; top: 0;}
    to {left: 200px; top: 40px;}
  }

</style>
</head>
<body>
  <div></div>
</body>
</html>
原文地址:https://www.cnblogs.com/qjuly/p/9053626.html