Animate.css开源CSS动画库 初学者使用教程

随着HTML5、CSS3的流行,浏览器可以完成一些简单动画了,但是经常自己手写不仅麻烦,而且伤脑筋,想不出好的效果啊,直到遇见了animate.css,感觉很不错哦,所以记录一下使用教程。

animate.css 是一个来自国外的 CSS3 动画库,它预设了抖动(shake)、闪烁(flash)、弹跳(bounce)、翻转(flip)、旋转(rotateIn/rotateOut)、淡入淡出(fadeIn/fadeOut)等多达 60 多种动画效果,几乎包含了所有常见的动画效果。
animate.css for github

Animate.css 动画展示

WOW.js 是一个依赖于Animate.css 让页面更炫酷的插件,有兴趣的可以去学习一下。WOW.js – 让页面滚动更有趣 初学者使用教程

安装

如果是npm项目,也可以这样安装:

npm install animate.css --save

或者使用yarn

yarn add animate.css

当夜也可以直接在HTML里引入

<head>
  <link rel="stylesheet" href="animate.min.css">
</head>

使用

给指定的元素加上指定的动画样式名
第一个animated是必须添加的样式名,第二个是指定的动画样式名。如果动画是无限播放的,可以添加 第三个class infinite

 <div class="animated bounce"></div>

给元素加上 class 后,刷新页面,就能看到动画效果了。animated 类似于全局变量,它定义了动画的持续时间;bounce 是动画具体的动画效果的名称,你可以选择任意的效果。

其他的补充说明

采用jquery可以不修改现有代码,动态添加动画

1.如果说想给某个元素动态添加动画样式,可以通过jquery来实现:

$('#yourElement').addClass('animated bounceOutLeft');

2.当动画效果执行完成后还可以通过以下代码添加事件

$('#yourElement').one('webkitAnimationEnd mozAnimationEnd MSAnimationEnd oanimationend animationend', doSomething);
//具体写法,注意添addClass和removeClass的内容,是同一个动画效果
$('.j-tab li').on('mouseenter', function () {
      $(this).addClass('animated rotateIn').one('webkitAnimationEnd mozAnimationEnd MSAnimationEnd oanimationend animationend', function(){
        $(this).removeClass('animated rotateIn');
      });
      ;
    });

对于这种用法(也是实际开发中最多的用法),官方给出了jQuery的封装:

//扩展$对象,添加方法animateCss 
$.fn.extend({
  animateCss: function (animationName) {
    var animationEnd = 'webkitAnimationEnd mozAnimationEnd MSAnimationEnd oanimationend animationend';
    $(this).addClass('animated ' + animationName).one(animationEnd, function() { 
      $(this).removeClass('animated ' + animationName);
    });
  }
});
//使用示例: $('#yourElement').animateCss('bounce');

3.你也可以通过 JavaScript 或 jQuery 给元素添加这些 class,比如:

$(function(){
    $('#yourElement').addClass('animated bounce');
});

4.有些动画效果最后会让元素不可见,比如淡出、向左滑动等等,可能你又需要将 class 删除,比如:

$(function(){
    $('#yourElement').addClass('animated bounce');
    setTimeout(function(){
        $('#yourElement').removeClass('bounce');
    }, 1000);
});

5.animate.css 的默认设置也许有些时候并不是我们想要的,所以你可以重新设置,比如:

#yourElement{
    animate-duration: 2s;    //动画持续时间
    animate-delay: 1s;    //动画延迟时间
    animate-iteration-count: 2;    //动画执行次数
}

6.不能为内联元素设置动画,始终设置块或内联块级元素的动画(网格和flex容器以及子级也是块级元素)。可以在设置内嵌级别元素动画时将元素设置为display: inline-block

7.Animate.css v4 带来了一些改进,改进的动画和新的动画,因此值得进行升级。但是为所有Animate.css类添加了前缀-默认为animate__,因此无法直接迁移。animate.min.css带有animate__前缀,animate.compat.css文件,该文件根本没有前缀,就像以前的版本(3.x及更低版本)一样。

源码解析

一,animated
设置了动画时长和动画执行前后元素应该怎样应用样式(animation-fill-mode)

  .animated {
    -webkit-animation-duration: 1s;
    animation-duration: 1s;
    -webkit-animation-fill-mode: both;
    animation-fill-mode: both;
  }

二,infinite
设置了动画次数(无限次)

  .infinite {
    -webkit-animation-iteration-count: infinite;
    animation-iteration-count: infinite;
  }

三,动画类名(如:bounce)
设置使用的动画,及一些动画的初始化属性。

  .bounce {
    -webkit-animation-name: bounce;
    animation-name: bounce;
    -webkit-transform-origin: center bottom;
    transform-origin: center bottom;
  } 
  @keyframes bounce {
    from, 20%, 53%, 80%, to {
      -webkit-animation-timing-function: cubic-bezier(0.215, 0.610, 0.355, 1.000);
      animation-timing-function: cubic-bezier(0.215, 0.610, 0.355, 1.000);
      -webkit-transform: translate3d(0,0,0);
      transform: translate3d(0,0,0);
    }
    40%, 43% {
      -webkit-animation-timing-function: cubic-bezier(0.755, 0.050, 0.855, 0.060);
      animation-timing-function: cubic-bezier(0.755, 0.050, 0.855, 0.060);
      -webkit-transform: translate3d(0, -30px, 0);
      transform: translate3d(0, -30px, 0);
    }
    70% {
      -webkit-animation-timing-function: cubic-bezier(0.755, 0.050, 0.855, 0.060);
      animation-timing-function: cubic-bezier(0.755, 0.050, 0.855, 0.060);
      -webkit-transform: translate3d(0, -15px, 0);
      transform: translate3d(0, -15px, 0);
    }
    90% {
      -webkit-transform: translate3d(0,-4px,0);
      transform: translate3d(0,-4px,0);
    }
  }

自定义选项
这个放在最后是因为看了源码之后很自然就无师自通懂得怎么自定义了,例如想用css来控制动画次数等。

  #yourElement {
    -vendor-animation-duration: 3s;
    -vendor-animation-delay: 2s;
    -vendor-animation-iteration-count: infinite;
  }
原文地址:https://www.cnblogs.com/jiaoshou/p/13216361.html