经典动画库 animate.css 的应用_animate.min.css-程序员宅基地

技术标签: css  前端  html  HTML5_CSS3  

一、animate.css

animate.css::Animate.css 就像嗑水那么简单的CSS动画。

官网:Redirecting to Animate.css

Animate.css是一个纯CSS动画库,其核心技术使用了 CSS3 里的 @keyframesanimation

  • 不兼容IE10以下的 IE 浏览器。其它各大浏览器只要不是太旧的版本都能兼容。现在微软官方已经抛弃了 IE 浏览器,目前主流浏览器都已经支持 Animate.css,所以说兼容性还是蛮强的

  • 官方给出了70多种动画特效,还在持续增加中。这些动画其实大多数都不是很难,就是不愿意去写。所以这个 CSS 库真的很适合懒人(所有人)。

获取 animate.cssGitHub - animate-css/animate.css: A cross-browser library of CSS animations. As easy to use as an easy thing.

  • docs :说明文档,全英文。该文档是使用说明,需要在服务器下,才能运行有效。HBuilder 或者 Webstorm 开发模拟服务器皆可以。

  • animate.css:动画样式文件,非压缩版

  • animate.min.css:动画样式文件,压缩版。

  • animate.compat.css:动画样式文件,高压缩版。

二、基础用法

step1. 引入 animate.css

(1)一般应用

在 HTML 页面中,引入 animate.css 即可。

<!-- 引入 -->
<link rel="stylesheet" href="css/animate.css/animate.min.css">

 (2)CDN

也可以使用CDN 上的文件,如:

<link href="https://cdn.bootcdn.net/ajax/libs/animate.css/4.1.1/animate.min.css" rel="stylesheet">

(3)方式三:SCSS

 
把 animate.min.css 拷贝到自己的 scss 目录,修改名字为 _animate.min.scss。

在主 scss 文件中,利用 @import 导入即可。如:

app.scss

@charset "utf-8";

@import "public";   // 公用样式
@import "animate.min";  // _animate.min.scss
@import "style";    // 自己的样式文件

step2. 完成标签静态样式

页面动画编写的原则:先静而后动

先完成静态的样式,再考虑动画。

<div class="box"></div>
.box{
  width: 200px;
  height: 200px;
  background: #f30;
  margin-left: auto;
  margin-right: auto;
  margin-top: 100px;
}

step3. 添加动画

(1)给标签添加基础类 animate__animated

老版本(v3及以下)的 animate.css,添加的基础类是 animated

新版本(v4 及以上),通通添加了前缀 animate__

<div class="box animate__animated">

</div>

(2)添加指定的动画类

<div class="box animate__animated animate__bounce">

</div>

 动画名称在官网上可以查看:

 但是,类名前面不要忘了添加 animate__

(3)设置动画延迟

<div class="box animate__animated animate__bounce   animate__delay-2s">
</div>

animate.css 提供了从 1s5s 的延迟类。

  • animate__delay-1s

  • animate__delay-2s

  • animate__delay-3s

  • animate__delay-4s

  • animate__delay-5s

可以自定义类,利用 animation-delay 属性去修改动画延迟时间。  

<div class="box  animate__animated  animate__backInUp myAniDelay500ms">
</div>
.myAniDelay500ms{
    animation-delay: 500ms;
}

4)控制动画持续时间

 <div class="animate__animated animate__bounce animate__faster">
 </div>

animate.css 提供的 animate__animated 类,默认的动画时间是 1s,可以调整动画时间。

  • animate__slow 2s

  • animate__slower 3s

  • animate__fast 800ms

  • animate__faster 500ms

可以自定义类,利用 animation-duration 属性去修改动画持续时间。

<div class="box  animate__animated  animate__backInUp myAniDelay500ms">
</div>
.myAniDuration500ms{
    animation-duration: 500ms;
}

(5)控制动画次数

<div class="animate__animated animate__bounce animate__repeat-2">
</div>

 animate.css 默认动画执行次数为 1。它提供了动画次数的类:

  • animate__repeat-1 1

  • animate__repeat-2 2

  • animate__repeat-3 3

  • animate__infinite infinite,无限次数。

可以自定义类,利用 animation-iteration-count 属性去修改动画延迟时间。

<div class="box  animate__animated  animate__backInUp  myAniTimes10">
</div>
.myAniTimes10{
  animation-iteration-count: 10;
}

 三、使用JavaScript控制动画

  • 基础应用,给标签增加 animate.css 类:

 const element = document.querySelector('.my-element');
 element.classList.add('animate__animated', 'animate__bounceOutLeft');
  • 可以监听动画事件:

const element = document.querySelector('.my-element');
element.classList.add('animate__animated', 'animate__bounceOutLeft');

element.addEventListener('animationend', () => {
  // do something
});

CSS 动画播放时,会发生以下三个 JS 事件:

  1. animationstart - CSS 动画开始后触发

  2. animationiteration - CSS 动画重复播放时触发

  3. animationend - CSS 动画完成后触发。这个事件用的较多。

在这三个事件函数中,均可以使用 event.animationName 属性,获取是在执行哪个动画。

  • 可以修改动画属性

const element = document.querySelector('.my-element');
element.style.setProperty("animation-iteration-count", 1);
  •  可以结合 Promise 自动给标签添加动画和结束动画后进行操作。
const animateCSS = (element, animation, prefix = 'animate__') =>
// We create a Promise and return it
new Promise((resolve, reject) => {
    const animationName = `${prefix}${animation}`;
    const node = document.querySelector(element);

    node.classList.add(`${prefix}animated`, animationName);

    // When the animation ends, we clean the classes and resolve the Promise
    function handleAnimationEnd(event) {
        event.stopPropagation();
        node.classList.remove(`${prefix}animated`, animationName);
        resolve('Animation ended');
    }
    node.addEventListener('animationend', handleAnimationEnd, {once: true});
});

像这样来使用它:

animateCSS('.my-element', 'bounce');

// or
animateCSS('.my-element', 'bounce').then((message) => {
  // Do something after the animation
});

 

版权声明:本文为博主原创文章,遵循 CC 4.0 BY-SA 版权协议,转载请附上原文出处链接和本声明。
本文链接:https://blog.csdn.net/weixin_42703239/article/details/127873024

智能推荐

http隧道 java_使用java语言实现http隧道技术-程序员宅基地

文章浏览阅读119次。该楼层疑似违规已被系统折叠隐藏此楼查看此楼/***Getaparametervalue**@paramkeyString*@paramdefString*@returnString*/publicStringgetParameter(Stringkey,Stringdef){returnisStandalone?System.getProperty(ke..._java http隧道

Keepalived高可用+邮件告警_keepalived sendmail-程序员宅基地

文章浏览阅读913次。IP主机名备注192.168.117.14keepalived-master主节点192.168.117.15keepalived-slaver备节点192.168.117.100VIP1.主备节点均安装keepalived# yum install -y keepalived httpd2.主备节点均修改keepalived日志存放路径..._keepalived sendmail

SPFILE 错误导致数据库无法启动(ORA-01565)_ora01565 ora27046-程序员宅基地

文章浏览阅读469次。--==========================================--SPFILE错误导致数据库无法启动(ORA-01565)--========================================== SPFILE错误导致数据库无法启动 SQL> startup ORA-01078: failurein proce_ora01565 ora27046

功能测试基础知识(1)-程序员宅基地

文章浏览阅读6.1k次,点赞2次,收藏54次。功能测试基础知识总结_功能测试

postgresql 中文排序_pg中文排序-程序员宅基地

文章浏览阅读3.2k次,点赞3次,收藏2次。pg 中文首字母排序_pg中文排序

[Mysql] CONVERT函数_mysql convert-程序员宅基地

文章浏览阅读3.1w次,点赞23次,收藏109次。本文主要讲解CONVERT函数_mysql convert

随便推点

HTML5与微信开发(2)-视频播放事件及API属性_微信开发者工具视频快进-程序员宅基地

文章浏览阅读8.6k次,点赞2次,收藏2次。HTML5 的视频播放事件想必大家已经期待很久了吧,在HTML4.1、4.0之前我们如果在网页上播放视频无外乎两种方法: 第一种:安装FLASH插件或者微软发布的插件 第二种:在本地安装播放器,在线播放组件之类的 因为并不是所有的浏览器都安装了FLASH插件,就算安装也不一定所有的都能安装成功。像苹果系统就是默认禁用FLASH的,安卓虽然一开始的时候支持FLASH,但是在安卓4.0以后也开始不_微信开发者工具视频快进

JedisConnectionException Connection Reset_jedisconnectionexception: java.net.socketexception-程序员宅基地

文章浏览阅读5.4k次,点赞3次,收藏4次。在使用redis的过程常见错误总结1.JedisConnectionException Connection Reset参考这边文章:Connection reset原因分析和解决方案https://blog.csdn.net/cwclw/article/details/527971311.1问题描述Exception in thread "main" redis.clients...._jedisconnectionexception: java.net.socketexception: connection reset

Lua5.3版GC机制理解_lua5.3 gc-程序员宅基地

文章浏览阅读8.3k次,点赞8次,收藏42次。目录1.Lua垃圾回收算法原理简述2.Lua垃圾回收中的三种颜色3.Lua垃圾回收详细过程4.步骤源码详解4.1新建对象阶段4.2触发条件4.3 GC函数状态机4.4标记阶段4.5清除阶段5.总结参考资料lua垃圾回收(Garbage Collect)是lua中一个比较重要的部分。由于lua源码版本变迁,目前大多数有关这个方面的文章都还是基于lua5.1版本,有一定的滞后性。因此本文通过参考当前..._lua5.3 gc

手机能打开的表白代码_能远程打开,各种手机电脑进行监控操作,最新黑科技...-程序员宅基地

文章浏览阅读511次。最近家中的潮人,老妈闲着没事干,开始学玩电脑,引起他的各种好奇心。如看看新闻,上上微信或做做其他的事情。但意料之中的是电脑上会莫名出现各种问题?不翼而飞的图标?照片又不见了?文件被删了,卡机或者黑屏,无声音了,等等问题。常常让她束手无策,求助于我,可惜在电话中说不清,往往只能苦等我回家后才能解决,那种开心乐趣一下子消失了。想想,这样也不是办法啊, 于是,我潜心寻找了两款优秀的远程控制软件。两款软件...

成功Ubuntu18.04 ROS melodic安装Cartograhper+Ceres1.13.0,以及错误总结_ros18.04 安装ca-程序员宅基地

文章浏览阅读1.8k次。二.初始化工作空间三.设置下载地址四.下载功能包此处可能会报错,请看:rosdep update遇到ERROR: error loading sources list: The read operation timed out问题_DD᭄ꦿng的博客-程序员宅基地接下来一次安装所有功能包,注意对应ROS版本 五.编译功能包isolated:单独编译各个功能包,每个功能包之间不产生依赖。编译过程时间比较长,可能需要几分钟时间。此处可能会报错:缺少absl依赖包_ros18.04 安装ca

Harbor2.2.1配置(trivy扫描器、镜像签名)_init error: db error: failed to download vulnerabi-程序员宅基地

文章浏览阅读4.1k次,点赞3次,收藏7次。Haobor2.2.1配置(trivy扫描器、镜像签名)docker-compose下载https://github.com/docker/compose/releases安装cp docker-compose /usr/local/binchmod +x /usr/local/bin/docker-composeharbor下载https://github.com/goharbor/harbor/releases解压tar xf xxx.tgx配置harbor根下建立:mkd_init error: db error: failed to download vulnerability db: database download

推荐文章

热门文章

相关标签