首頁  >  文章  >  web前端  >  写多个物件css3循环动画案例原理_html/css_WEB-ITnose

写多个物件css3循环动画案例原理_html/css_WEB-ITnose

WBOY
WBOY原創
2016-06-24 11:46:561117瀏覽

  在写h5页面中,我们可能经常遇到要写很多css3动画,有没有同学遇到多个物件同样的动画效果,循环保存步调一致呢,我就经常碰到,之前一直不知道其中的原理,只是简单的迁移改改,可是遇到稍微复杂多一点的就hold不住了,只能硬着头皮去分析其中的原理。那么接下来,让我们先看看案例,然后了解之中的原理

css3动画循环案例

 案例一:loading动画效果

html代码:

<div class="spinner">  <div class="rect1"></div>  <div class="rect2"></div>  <div class="rect3"></div>  <div class="rect4"></div>  <div class="rect5"></div></div>

css样式:

<style>    .spinner {  margin: 100px;  width: 50px;  height: 60px;  text-align: center;  font-size: 10px;} .spinner > div {  background-color: #67CF22;  height: 100%;  width: 6px;  display: inline-block;     -webkit-animation: stretchdelay 1.2s infinite ease-in-out;  animation: stretchdelay 1.2s infinite ease-in-out;} .spinner .rect2 {  -webkit-animation-delay: -1.1s;  animation-delay: -1.1s;} .spinner .rect3 {  -webkit-animation-delay: -1.0s;  animation-delay: -1.0s;} .spinner .rect4 {  -webkit-animation-delay: -0.9s;  animation-delay: -0.9s;} .spinner .rect5 {  -webkit-animation-delay: -0.8s;  animation-delay: -0.8s;} @-webkit-keyframes stretchdelay {  0%, 40%, 100% { -webkit-transform: scaleY(0.4) }    20% { -webkit-transform: scaleY(1.0) }} @keyframes stretchdelay {  0%, 40%, 100% {     transform: scaleY(0.4);    -webkit-transform: scaleY(0.4);  }  20% {     transform: scaleY(1.0);    -webkit-transform: scaleY(1.0);  }}</style>

案例二:圆形放大或缩小的loading效果

html代码:

<div class="spinner">  <div class="double-bounce1"></div>  <div class="double-bounce2"></div></div>

css样式:

.spinner {  width: 60px;  height: 60px;   position: relative;  margin: 100px;} .double-bounce1, .double-bounce2 {  width: 100%;  height: 100%;  border-radius: 50%;  background-color: #67CF22;  opacity: 0.6;  position: absolute;  top: 0;  left: 0;     -webkit-animation: bounce 2.0s infinite ease-in-out;  animation: bounce 2.0s infinite ease-in-out;} .double-bounce2 {  -webkit-animation-delay: -1.0s;  animation-delay: -1.0s;} @-webkit-keyframes bounce {  0%, 100% { -webkit-transform: scale(0.0) }  50% { -webkit-transform: scale(1.0) }} @keyframes bounce {  0%, 100% {     transform: scale(0.0);    -webkit-transform: scale(0.0);  } 50% {     transform: scale(1.0);    -webkit-transform: scale(1.0);  }}

  

  看了上面的两个案例,我们知道了什么是多个物件循环动画,也可以知道,通过animation-delay延迟,我们可以让多个物体保持步调一致的运动效果,但是看了动画和延迟却还是不知道怎么设置,可能你会想象物体运行的帧,确实我分析的时候也是这么想的。

 

css3动画循环原理

  要讲清楚这个,我们得先知道几个概率(参数),animation动画持续时间t1,每个物体延迟时间差t2,物体个数n

  t2 = t1/n

  平均关键帧的百分比为 100%/n

  把以上的这几个点,理解清楚了之后,其实我们应该就清楚了怎么去写多个物体循环了,举个例子:

html代码:

      <ul>          <li class="on1"></li>          <li class="on2"></li>          <li class="on3"></li>          <li class="on4"></li>          <li class="on5"></li>          <li class="on6"></li>      </ul>

css代码:

        ul{list-style:none;padding:0;margin:0;}        ul>li{            width:50px;            height:50px;            margin-bottom:20px;            background:orange;            -webkit-animation:scale 3s linear infinite;        }        .on2{            -webkit-animation-delay:0.5s;        }        .on3{            -webkit-animation-delay:1s;        }        .on4{            -webkit-animation-delay:1.5s;        }        .on5{            -webkit-animation-delay:2s;        }        .on6{            -webkit-animation-delay:2.5s;        }        @-webkit-keyframes scale{            0%,33.4%{width:50px;height:50px;}            16.7%{width:80px;height:80px;}        }

  这个例子其实是6个物体循环的动画,我们控制在每次100%/16的点做不同物体变化效果就行了,即16.7%、33.4%、50.1%、66.8%、83.5、100.2%(这里由于不整除,多出了一部分,就当近似于)

  而且,我们想一开始就看到元素开始动,那么我们就要将这16.7%前置,则得出了

      @-webkit-keyframes scale{            0%,33.4%{width:50px;height:50px;}            16.7%{width:80px;height:80px;}        }

  最后可能也不是讲的很清楚,还请不理解的多做几个例子,多想想就通了,共勉。

 

  留几个有关css3动画写的比较好的文章:

    经验分享:多屏复杂动画CSS技巧三则

    涨姿势!CSS3动画帧数科学计算法

    【原】移动web动画设计的一点心得??css3实现跑步

  

 

 

陳述:
本文內容由網友自願投稿,版權歸原作者所有。本站不承擔相應的法律責任。如發現涉嫌抄襲或侵權的內容,請聯絡admin@php.cn