如上图所示,在许多的活动宣传html5中会经常需要用到这样的一个动画效果。特别是快到年底了,也许有同学正在为了公司的活动页面而忙碌,get到这样一个小技能说不定刚好对你有帮助哦。

在css3中,我们使用animation与keyframes结合,可以给元素添加各种各样的动画效果。具体的动画,在keyframes中定义,在animation中使用。例如可以定义一个从上飞入的动画效果。

@keyframes topIn {  from { transform: translateY(-50px) }  to { transform: translateY(0px) }}

并在目标元素中通过animation来使用动画。

<div class="target topIn"></div>.topIn {  animation: topIn 1s ease;}

这样,当元素第一次渲染进入DOM时,就会有一个从上到下的位移动画效果。当然,这种效果并不是我们想要的。往往我们还在在动画上加上一个透明度从0到1的渐变。

@keyframes topIn {  from {     transform: translateY(-50px);    opacity: 0;   }  to {     transform: translateY(0px);    opacity: 1;   }}

我们还希望能够控制元素的显示时机应该怎么办?简单一点的办法就是在需要动画效果展示时,才给目标元素添加控制动画的class样式。

btn.addEventListener(click, function() {  document.querySelector(.target).classList.add(topIn);}, !1);

但是这样做有一个问题。我相信实践过的朋友都已经发现过的。我们期望元素在入场之前,是处于看不见的状态。但是仅仅只是上面的做法,动画开始前元素是能够被看见的。那么应该怎么办?

我们可以很简单的想到,给元素添加 display: none 或者 visibility: hidden 。但是由于 display: none 之后,元素是不占位的。因此如果这样的话,会导致页面布局出现混乱。所以我们在开始之前,给元素添加一个新的class。

.aninode {  visibility: hidden;}

并且添加一个新的class让元素显示出来。

.animated .aninode {  visibility: visible;}

控制动画效果的class也在css上进行一些调整。

.animated .topIn {  animation: topIn 1s ease;}

这样做的好处是,我们只需要在class中添加一个 animated ,就能够达到我们的效果。实例demo完整代码如下:

<div class="container">  <div class="target aninode leftIn"></div>  <button class="btn show">show</button>  <button class="btn hide">hide</button></div>.container {  width: 100px;  margin: 0 auto;}.aninode {  visibility: hidden;}.animated .aninode {  visibility: visible;}.target {  width: 100px;  height: 100px;  background: orange;  border-radius: 4px;  margin: 20px 0;}.animated .topIn {  animation: topIn 1s ease;}.animated .leftIn {  animation: leftIn 1s ease;}.btn {  width: 100px;  height: 30px;  border: 1px solid #ccc;  outline: none;  transition: 0.1s;}.btn:active {  border: none;  background: orange;  color: #fff;}@keyframes topIn {  from {     transform: translateY(-50px);    opacity: 0;   }  to {     transform: translateY(0px);    opacity: 1;   }}@keyframes leftIn {  from {     transform: translateX(-50px);    opacity: 0;   }  to {     transform: translateX(0px);    opacity: 1;   }}var show = document.querySelector(.show);var hide = document.querySelector(.hide);var container = document.querySelector(.container);show.addEventListener(click, function() {  container.classList.add(animated);}, !1);hide.addEventListener(click, function() {  container.classList.remove(animated);}, !1);

Demo显示如下:

See the Pen <a href=https://codepen.io/yangbo5207/pen/NXKrPg/>NXKrPg</a> by Ormie (<a href=https://codepen.io/yangbo5207>@yangbo5207</a>) on <a href=https://codepen.io>CodePen</a>.

codepen demo 地址

但是这样离我们想要的效果好像还差一点点。继续思考。首先想要后面的元素比前一个元素晚一点出现,那么肯定是要控制延迟时间,我们就必须有许多设置延迟时间的class。

.delay200 {    animation-delay: 200ms;    animation-fill-mode: backwards!important;}.delay400 {    animation-delay: 400ms;    animation-fill-mode: backwards!important;}.delay600 {    animation-delay: 600ms;    animation-fill-mode: backwards!important;}.delay800 {    animation-delay: 800ms;    animation-fill-mode: backwards!important;}

animation-fill-mode: backwards!important; 的目的是为了元素在出现之前,保持透明度为0的状态。防止当添加 animated 之后元素直接出现了。

加 !important 是为了防止在新的class中使用animation简写时对 animation-fill-mode 的属性进行覆盖改写。如果此处不写 !important 的话,那么在 topIn 这样的动画class中就不能使用简写形式。

这样之后,我们只需要在css中添加上上述代码,并对html做一些改动,就能够实现我们想要的效果了。

See the Pen <a href=https://codepen.io/yangbo5207/pen/mpbEEE/>mpbEEE</a> by Ormie (<a href=https://codepen.io/yangbo5207>@yangbo5207</a>) on <a href=https://codepen.io>CodePen</a>.

codepen demo 地址

完整代码如下:

<div class="container">  <div class="targets aninode">      <div class="item leftIn">春晓</div>      <div class="item leftIn delay200">春眠不觉晓</div>      <div class="item leftIn delay400">处处蚊子咬</div>      <div class="item leftIn delay600">夜来风雨声</div>      <div class="item leftIn delay800"><此处请留下你们的才华></div>  </div>  <button class="btn show">show</button>  <button class="btn hide">hide</button></div>.container {  width: 200px;  margin: 0 auto;}.aninode {  visibility: hidden;}.animated .aninode {  visibility: visible;}.targets {  margin: 20px 0;}.targets .item {    border: 1px solid #ccc;    margin: 10px 0;    line-height: 2;    padding: 2px 6px;    border-radius: 4px;}.animated .topIn {  animation: topIn 1s ease;}.animated .leftIn {  animation-name: leftIn;  animation-duration: 1s;}.btn {  width: 100px;  height: 30px;  border: 1px solid #ccc;  outline: none;  transition: 0.1s;}.btn:active {  border: none;  background: orange;  color: #fff;}@keyframes topIn {  from { transform: translateY(-50px) }  to { transform: translateY(0px) }}@keyframes leftIn {  from {     transform: translateX(-50px);    opacity: 0;   }  to {     transform: translateX(0px);    opacity: 1;   }}.delay200 {    animation-delay: 200ms;    animation-fill-mode: backwards!important;}.delay400 {    animation-delay: 400ms;    animation-fill-mode: backwards!important;}.delay600 {    animation-delay: 600ms;    animation-fill-mode: backwards!important;}.delay800 {    animation-delay: 800ms;    animation-fill-mode: backwards!important;}var show = document.querySelector(.show);var hide = document.querySelector(.hide);var container = document.querySelector(.container);show.addEventListener(click, function() {  container.classList.add(animated);}, !1);hide.addEventListener(click, function() {  container.classList.remove(animated);}, !1);

我们发现js的逻辑并没有发生任何改变。仍然仅仅只是在合适的位置添加/删除animated。

彩蛋:

在实践中我们还会遇到一个比较麻烦的事儿。就是延迟class的编写。我们可能并不知道会使用到那些时差,有多少个元素会使用到,如果都用手来写的话,重复工作确实太过麻烦。因此我们可以使用js动态插入。代码如下:

const styleSheet = getSheet();var delay = 100;while (delay < 10000) {    styleSheet.insertRule(`.animated .delay${delay}{ animation-delay: ${delay}ms; animation-fill-mode: backwards; }`, styleSheet.cssRules.length);    delay += delay < 3000 ? 100 : 1000;}function getSheet() {    var sheets = document.styleSheets;    var len = sheets.length;    for(var i = 0; i <= len; i++) {        var sheet = sheets.item(i);        try {            if (sheet.cssRules) {                return sheet;            }        } catch(e) {}     }    var style = document.createElement(style);    style.type = "text/css";    document.getElementsByTagName(head)[0].appendChild(style);    return style.sheet;}

以上所述是小编给大家介绍的css3实现多个元素依次显示效果,希望对大家有所帮助,如果大家有任何疑问请给我留言,小编会及时回复大家的。在此也非常感谢大家对脚本之家网站的支持!

css3实现多个元素依次显示效果