一个栗子上手 CSS3 动画

vllyklvnuw 7年前
   <p>最近杂七杂八的事情很多,很多知识都没来得及总结,是时候总结总结,开启新的篇章~</p>    <p>本篇文章不一一列举CSS3动画的属性,若需要了解API,可前往 MDN  。</p>    <p>在开始栗子前,我们先补补基础知识。</p>    <p>css3动画分类:</p>    <ul>     <li>补间动画 – 具有连贯性的动画</li>     <li>逐帧动画 – 使用steps过渡方式实现跳跃</li>    </ul>    <p>animation常用属性及场景:</p>    <pre>  <code class="language-css">animation: name duration timing-function delay iteration-count direction;  </code></pre>    <p>1. timing-function属性:  </p>    <ul>     <li>ease 规定慢速开始,然后变快,然后慢速结束的过渡效果。</li>     <li>ease-in 规定以慢速开始的过渡效果。</li>     <li>ease-out 规定以慢速结束的过渡效果。</li>     <li>ease-in-out 规定以慢速开始和结束的过渡效果。</li>     <li>linear 动画从头到尾的速度是相同的。</li>     <li>cubic-bezier(n,n,n,n) 在cubic-bezier函数中自己的值,n取值为0~1</li>     <li>steps()</li>    </ul>    <p>2. delay属性:用于将动画与其他动画的执行时机错开,将动画落到不同的时间点。这个属性很好用~</p>    <p>动画原则:</p>    <ol>     <li>运动一般有个惯性,所以要先快后有一个慢一点的反弹。</li>     <li>背景若使用多个星星闪烁,错位闪烁</li>    </ol>    <p>配合JS使用</p>    <pre>  <code class="language-css">slide.addEventListener("webkitAnimationEnd", function() {     console.log('eeee') //动画结束再调用  });  </code></pre>    <p>有些情况我们需要确保动画结束后再进行另外一些交互,可使用该事件监听。</p>    <p>实战演习:</p>    <p>假如我们需要实现一个这样简单的动画:</p>    <p style="text-align:center"><img src="https://simg.open-open.com/show/12605af996a572d35330801372ba50fd.gif"></p>    <p>仔细观察上面的动画,我们发现,它可以由以下3部分组成:</p>    <p>1. 入场动画——从右往左移动</p>    <p>2. 左右循环移动</p>    <p>3. 逐帧动画</p>    <p>实现方法:</p>    <p>使用3个dom元素, 最外层dom实现入场动画,第二层dom实现左右移动,第三层dom实现逐帧动画。</p>    <p>优点:调试方便,节省时间。</p>    <p>缺点:dom多。</p>    <p>1. dom结构</p>    <pre>  <code class="language-css"><div class="anima_entrance">      <div class="anima_move">          <div class="anima_sprite"></div>      </div>  </div>  </code></pre>    <p>2. 分析动画形成的时间轴:</p>    <p style="text-align:center"><img src="https://simg.open-open.com/show/40480eb682987c166e09eed79bd436c5.png"></p>    <p>入场动画持续0.6s,只播放一次,左右移动以及逐帧动画持续2s,循环播放,代码如下:</p>    <pre>  <code class="language-css">.anima_entrance {      animation: anima_entrance .6s ease-in-out both;  }     .anima_move {      animation: anima_move 2s linear .6s infinite both;  }     .anima_sprite {      animation: anima_sprite 2s step-end .6s infinite both;  }  </code></pre>    <p>3. 使用steps()实现逐帧动画:</p>    <p>使用下面这张雪碧图,通过改变background-position实现动画切换。</p>    <p style="text-align:center"><img src="https://simg.open-open.com/show/277025ede97f3682b4798cca86a88fb2.png"></p>    <p>蹬蹬蹬,效果如下面所示,是不是很失望</p>    <p style="text-align:center"><img src="https://simg.open-open.com/show/d9140b4dc97eaa5485fac7b7d228c099.gif"></p>    <p>原因:由于animation默认以ease方式过渡,它会在每个关键帧之间插入补间动画,所以动画效果是连贯性的。此时可以使用steps()取消补间动画。</p>    <p>贴一个图:</p>    <p style="text-align:center"><img src="https://simg.open-open.com/show/9da8ed23c88230445475e94a309dbff8.png"></p>    <ul>     <li>steps(1,start): 动画一开始就跳到 100% 直到 <strong>这一帧(不是整个周期)</strong> 结束   == step-start</li>     <li>steps(1,end): 保持 0% 的样式直到 <strong>这一帧(不是整个周期)</strong> 结束   == step-end</li>    </ul>    <p>接着,我们将timing-function改成 step-end,效果如下:</p>    <pre>  <code class="language-css">animation: sprite 2s step-end .6s infinite both;  </code></pre>    <p style="text-align:center"><img src="https://simg.open-open.com/show/4a416d6c04a81b87cb45d52bcda88155.gif"></p>    <p>出现想要的效果了哈~不错。</p>    <p>完整的css代码如下:</p>    <pre>  <code class="language-css">.anima_entrance {      position: absolute;      z-index: 3;      top: 5.1rem;      left: 4.05rem;      width: 12.9rem;      height: 19.1rem;      -webkit-animation: anima_entrance .6s ease-in-out both;      animation: anima_entrance .6s ease-in-out both;  }     .anima_move {      width: 218px;      height: 382px;      position: absolute;      z-index: 1;      top: 0;      left: 42px;      -webkit-animation: anima_move 2s linear .6s infinite both;      animation: anima_move 2s linear .6s infinite both;  }     .anima_sprite {      width: 218px;      height: 382px;      background: url(demo.png) no-repeat 0 0;      background-size: 25.8rem 19.1rem;      -webkit-animation: anima_sprite 2s step-end .6s infinite both;      animation: anima_sprite 2s step-end .6s infinite both;  }     @keyframes anima_entrance {      0% {          -webkit-transform: translate3d(18.75rem, 0, 0);          transform: translate3d(18.75rem, 0, 0);      }      100% {          -webkit-transform: translate3d(0, 0, 0);          transform: translate3d(0, 0, 0);      }  }     @keyframes anima_move {      0%, 16%, 42%, 74%, 100% {          -webkit-transform: translate3d(0, 0, 0);          transform: translate3d(0, 0, 0);      }      29% {          -webkit-transform: translate3d(-1rem, 0, 0);          transform: translate3d(-1rem, 0, 0);      }      87% {          -webkit-transform: translate3d(1rem, 0, 0);          transform: translate3d(1rem, 0, 0);      }  }     @keyframes anima_sprite {      0%, 16%, 42%, 58%, 74%, 100% {          background-position: -12.9rem 0;      }      8%, 50%, 66% {          background-position: 0 0;      }  }  </code></pre>    <p> </p>    <p>来自:http://web.jobbole.com/91237/</p>    <p> </p>