animate.css详解:轻松实现网页动画效果

前言

在网页设计中,动画效果不仅仅是视觉上的装饰,更是提升用户体验的重要元素。animate.css 作为一个轻量级的 CSS 动画库,提供了丰富的预设动画效果,本文将探讨 animate.css 使用方法以及在实际项目中的应用案例,帮助你更好地掌握这一强大的工具,让你的页面更具个性。


animate.css 是什么?

animate.css 是一个用于实现 CSS 动画效果的库,它提供了一系列预定义的动画类,可以轻松地将动画效果应用到网页元素上。这个库的主要优点是简单易用,开发者只需在 HTML 元素中添加相应的类名,就能实现各种炫酷的动画效果,而无需编写复杂的 CSS 代码。

一、安装

  • 使用 npm 安装

    npm install animate.css --save
    
  • 使用 yarn 安装

    yarn add animate.css
    

二、引入

  • 全局引入

    import Vue from 'vue';
    import 'animate.css';
    
  • 局部引入

    import 'animate.css';
    

三、使用

注意: 在使用 animate.css 时,必须将类 animate__animated 添加到元素,以及任何 动画名称(不要忘记 animate__ 前缀!)。

animate__animated 类在 animate.css 中起着核心的初始化作用。它为元素启用了动画相关的基础样式与关键的 CSS 属性,为后续具体动画的呈现奠定基础。若缺少 animate__animated 类,即便添加了具体动画类名,如 animate__fadeIn,动画也无法生效。

animate__ 前缀对于具体动画类名同样不可或缺。animate.css 通过这样统一的前缀约定,将自身的动画类名与项目中可能存在的其他 CSS 类名区分开来,避免命名冲突。同时,这种规范的命名方式也方便开发者识别和管理动画相关样式。若去掉 animate__ 前缀,animate.css 将无法识别并应用对应的动画效果。

3.1 基础使用

需要添加动画的元素添加 animate__animated 类以及你想要的具体动画类。例如,使用 animate__bounce 动画效果就是: class="animate__animated animate__bounce"

<template><div class="animation"><el-button type="primary" @click="isShow = true">点击</el-button><div class="content"><div v-if="isShow" class="animate__animated animate__bounce">Animate.css</div><div v-if="isShow" class="animate__animated animate__flash">Animate.css</div><div v-if="isShow" class="animate__animated animate__pulse">Animate.css</div><div v-if="isShow" class="animate__animated animate__rubberBand">Animate.css</div><div v-if="isShow" class="animate__animated animate__shakeX">Animate.css</div><div v-if="isShow" class="animate__animated animate__shakeY">Animate.css</div><div v-if="isShow" class="animate__animated animate__headShake">Animate.css</div><div v-if="isShow" class="animate__animated animate__swing">Animate.css</div><div v-if="isShow" class="animate__animated animate__tada">Animate.css</div><div v-if="isShow" class="animate__animated animate__wobble">Animate.css</div><div v-if="isShow" class="animate__animated animate__jello">Animate.css</div><div v-if="isShow" class="animate__animated animate__heartBeat">Animate.css</div></div></div>
</template><script>
import "animate.css";
export default {data() {return {isShow: false,};},
};
</script><style scoped>
.animation {padding: 16px;
}
.content {display: flex;
}
div {font-weight: bold;color: cornflowerblue;margin: 20px 48px 0 0;font-size: 16px;
}
</style>

实现效果

在这里插入图片描述


3.2 自定义动画

–animate-duration

--animate-durationanimate.css 中用于自定义动画持续时间的 CSS 变量。通过设置这个属性,可以灵活地调整动画的速度,以适应不同的设计需求。

<template><div><el-button type="primary" @click="isShow = true">点击</el-button><div class="animation"><div class="content contentOne"><div v-if="isShow" class="animate__animated animate__bounce">Animate.css</div></div><div class="content"><div v-if="isShow" class="animate__animated animate__bounce">Animate.css</div></div></div></div>
</template><script>
import "animate.css";
export default {data() {return {isShow: false,};},
};
</script>
<style scoped>
.animation {display: flex;
}
div {font-weight: bold;color: cornflowerblue;margin: 20px 48px 0 0;font-size: 16px;
}
.contentOne .animate__bounce {--animate-duration: 3s; /* 持续时间改为3秒 */
}
</style>

实现效果
在这里插入图片描述

animate-delay

animate-delayanimate.css 中用于设置动画延迟时间的 CSS 属性。通过这个属性,可以控制动画开始前的等待时间,从而实现更复杂的动画效果。

行内定义

<div class="animation"><div class="content"><!-- animate__delay-1s 设置延迟时间为1秒 --><div v-if="isShow" class="animate__animated animate__bounce animate__delay-1s">Animate.css</div></div><div class="content"><div v-if="isShow" class="animate__animated animate__bounce">Animate.css</div></div>
</div>

style 中定义

<style scoped>
.animate__bounce {animation-delay: 1s; /* 设置延迟时间为1秒 */
}
</style>

实现效果
在这里插入图片描述


3.3 动画迭代次数

animate__repeat-animate.css 库提供的一个类,用于控制动画的重复次数,例如 animate__repeat-2 可以让动画重复播放 2 次。你可以将这个类名和其他动画类名一起添加到元素的 class 属性中。

<div class="animation"><div class="content"><!-- animate__repeat-2 动画循环2次 --><div v-if="isShow" class="animate__animated animate__bounce animate__repeat-2">Animate.css</div></div><div class="content"><div v-if="isShow" class="animate__animated animate__bounce">Animate.css</div></div>
</div>

实现效果
在这里插入图片描述


3.4 使用 @keyframes

这段代码实现了一个简单的交互界面,包含一个按钮和一个盒子。初始时盒子是隐藏的,点击按钮后盒子显示,并带有动画效果。代码中结合使用了 animate.css 库和自定义的 @keyframes 动画。

<template><div id="app"><button @click="showBox = true">显示盒子</button><div v-if="showBox" class="custom-box animate__animated"></div></div>
</template><script>
import "animate.css";
export default {data() {return {showBox: false,};},
};
</script><style scoped>
#app {display: flex;flex-direction: column;align-items: center;justify-content: center;
}button {padding: 10px 20px;font-size: 16px;background-color: #007bff;color: white;border: none;border-radius: 4px;cursor: pointer;transition: background-color 0.3s ease;
}
/* 使用 @keyframes 定义自定义动画 */
@keyframes customFadeIn {0% {opacity: 0;transform: scale(0.8);}100% {opacity: 1;transform: scale(1);}
}.custom-box {width: 200px;height: 200px;background-color: #28a745;margin-top: 20px;border-radius: 8px;animation: customFadeIn 0.5s;
}
</style>

实现效果

Animate.css


3.5 与 Javascript 结合使用

  • 模板部分

    包含标题、任务输入框与添加按钮,输入框支持回车添加。
    v-for 遍历任务数组展示任务列表,每个任务后有删除按钮。

  • 脚本部分

    数据有 newTask(输入内容)、tasks(任务列表)、nextTaskId(生成唯一 ID)。
    addTask 方法:输入非空时添加任务,清空输入框,DOM 更新后添加淡入向下动画,结束后移除动画类。
    removeTask 方法:触发淡出向上动画,动画结束后从数组移除任务。

  • 样式部分

    为容器、输入框、按钮、任务项等设置样式,添加了交互效果,如按钮悬停时的样式变化。

<template><div class="cartoon"><h1>任务列表</h1><div class="task-form"><input v-model="newTask" placeholder="输入任务内容" @keyup.enter="addTask" /><button @click="addTask">添加任务</button></div><div class="task-list"><div v-for="(task, index) in tasks" :key="task.id" :ref="`task-${task.id}`" class="task-item"><span class="task-text">{{ task.text }}</span><button @click="removeTask(index)">删除</button></div></div></div>
</template><script>
import "animate.css";
export default {data() {return {newTask: "", // 输入框的内容tasks: [], // 任务列表nextTaskId: 1, // 用于生成唯一 ID};},methods: {// 添加任务addTask() {if (this.newTask.trim() === "") return; // 防止空内容const newTask = {id: this.nextTaskId++,text: this.newTask.trim(),};this.tasks.push(newTask);this.newTask = ""; // 清空输入框// 触发添加动画this.$nextTick(() => {const taskElement = this.$refs[`task-${newTask.id}`][0];taskElement.classList.add("animate__animated", "animate__fadeInDown");taskElement.addEventListener("animationend", () => {taskElement.classList.remove("animate__animated", "animate__fadeInDown");});});},// 删除任务removeTask(index) {const task = this.tasks[index];// 触发删除动画const taskElement = this.$refs[`task-${task.id}`][0];taskElement.classList.add("animate__animated", "animate__fadeOutUp");taskElement.addEventListener("animationend", () => {this.tasks.splice(index, 1); // 动画结束后移除任务});},},
};
</script><style scoped>
.cartoon {margin: 20px;padding: 20px;border-radius: 10px;box-shadow: 0 4px 10px rgba(0, 0, 0, 0.1);
}h1 {text-align: center;
}.task-form {display: flex;gap: 10px;margin: 20px 0;
}.task-form input {flex: 1;padding: 12px;border: 1px solid rgba(0, 0, 0, 0.1);border-radius: 8px;font-size: 16px;outline: none;background: rgba(255, 255, 255, 0.9);transition: all 0.3s ease;
}
.task-form input:focus {border-color: #42b983;box-shadow: 0 0 8px rgba(66, 185, 131, 0.3);
}
.task-form button {padding: 12px 20px;background: linear-gradient(135deg, #42b983, #3aa876);color: white;border: none;border-radius: 8px;cursor: pointer;font-size: 16px;display: flex;align-items: center;gap: 8px;transition: all 0.3s ease;
}
.task-form button:hover {background: linear-gradient(135deg, #3aa876, #42b983);transform: translateY(-2px);box-shadow: 0 4px 12px rgba(66, 185, 131, 0.3);
}.task-item {padding: 10px;margin-bottom: 10px;background-color: #f9f9f9;border: 1px solid #ddd;border-radius: 5px;display: flex;justify-content: space-between;align-items: center;
}.task-item button {background-color: #ff4d4d;color: white;border: none;padding: 5px 10px;border-radius: 5px;cursor: pointer;
}.task-item button:hover {background-color: #ff1a1a;
}.task-text {margin-right: 16px;flex: 1;white-space: nowrap;overflow: hidden;text-overflow: ellipsis;
}
</style>

实现效果

在这里插入图片描述


animate.css 动画没有效果

在这里插入图片描述

原因: 电脑设置中关闭了动画效果。

某些操作系统有全局的动画效果开关,如果将其关闭,可能会影响网页动画的显示。可通过 『控制面板 ---- 轻松使用 ---- 轻松使用设置中心 ---- 使计算机更易于查看』,查看是否勾选了 『关闭所有不必要的动画』

1. 以 win11 为例,打开控制面板,找到『轻松使用设置中心』;

在这里插入图片描述

2. 找到『使计算机更易于查看』;

在这里插入图片描述

3. 将『关闭所有不必要的动画』取消勾选并应用保存即可。

在这里插入图片描述

设置后效果

在这里插入图片描述

本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处:http://www.pswp.cn/diannao/86667.shtml

如若内容造成侵权/违法违规/事实不符,请联系多彩编程网进行投诉反馈email:809451989@qq.com,一经查实,立即删除!

相关文章

【多智能体】基于嵌套进化算法的多代理工作流

&#x1f60a;你好&#xff0c;我是小航&#xff0c;一个正在变秃、变强的文艺倾年。 &#x1f514;本专栏《人工智能》旨在记录最新的科研前沿&#xff0c;包括大模型、具身智能、智能体等相关领域&#xff0c;期待与你一同探索、学习、进步&#xff0c;一起卷起来叭&#xff…

电源知多少?LDO VS DCDC((下)

首先补充几个上一节没有提到的知识&#xff0c;我们通常说的DCDC同步整流是指什么&#xff1f; 同步是指采用通态电阻极低的专用功率MOS来取代整流二极管以降低整流损耗&#xff0c;&#xff0c;但是同步整流有以下两点需要注意&#xff1a;1、MOS在导通之后的压降比较低&…

数组方法_push()/pop()/数组方法_shift()/unshift()

push 方法用于在数组的末端添加一个或多个元素&#xff0c;并返回添加新元 素后的数组长度。注意&#xff0c;该方法会改变原数组 var arr [];arr.push("颤三") // 1arr.push(itbaizhan) // 2arr.push(true, {}) // 4arr // [颤三 , itbaizhan, true, {}] pop 方法用…

脑机新手指南(八):OpenBCI_GUI:从环境搭建到数据可视化(下)

一、数据处理与分析实战 &#xff08;一&#xff09;实时滤波与参数调整 基础滤波操作 60Hz 工频滤波&#xff1a;勾选界面右侧 “60Hz” 复选框&#xff0c;可有效抑制电网干扰&#xff08;适用于北美地区&#xff0c;欧洲用户可调整为 50Hz&#xff09;。 平滑处理&…

多头与空头:市场博弈的两面

在金融市场中&#xff0c;多头&#xff08;Bull&#xff09;和空头&#xff08;Bear&#xff09;代表两种截然相反的投资策略&#xff0c;它们的博弈构成了市场价格波动的核心动力。 1. 概念对比&#xff1a;看涨与看跌的本质区别 多头&#xff08;Bull&#xff09;&#xff0…

Excel 发现此工作表中有一处或多处公式引用错误。请检查公式中的单元格引用、区域名称、已定义名称以及到其他工作簿的链接是否均正确无误。弹窗

Excel 提示“发现此工作表中有一处或多处公式引用错误”通常表示公式中存在无效引用。以下是系统化的检查步骤&#xff0c;帮助你定位和修复问题&#xff1a; 1. 检查单元格引用&#xff1a; 无效单元格引用&#xff1a;检查公式中的单元格地址&#xff08;如 A1、B10&…

变量 varablie 声明- Rust 变量 let mut 声明与 C/C++ 变量声明对比分析

一、变量声明设计&#xff1a;let 与 mut 的哲学解析 Rust 采用 let 声明变量并通过 mut 显式标记可变性&#xff0c;这种设计体现了语言的核心哲学。以下是深度解析&#xff1a; 1.1 设计理念剖析 安全优先原则&#xff1a;默认不可变强制开发者明确声明意图 let x 5; …

【指针】(适合考研、专升本)

指针 &与*是两个作用相反的运算符。 二级指针只能保存一级指针变量的地址和指向指针数组&#xff0c;其余情况不考虑。 int *p[2];int a12;int b15;*p&a;*(p1)&b;printf("%d\n%d\n",**p,**(p1));int **rp;printf("%d\n",**r); 普遍变量…

电路图识图基础知识-行程开关自动往返运行控制电路详解(二十三)

行程开关自动往返运行控制电路详解 在机床设备运行中&#xff0c;部分工作台需在特定距离内自动往复循环&#xff0c;行程开关自动往返运行控制电路可实现该功能&#xff0c;通过行程开关自动控制电动机正反转&#xff0c;保障工作台有序运动&#xff0c;以下展开详细解析。 …

SpringBoot学习day1-SpringBoot的简介与搭建

springboot回顾springspringbootspringboot搭建&#xff08;新闻为例&#xff09;springboot中的配置文件spring集成jdbc,mybatis,阿里巴巴数据源**SpringBoot 集成日志功能**(了解)常用日志组件日志级别 springboot统一异常处理 springboot 回顾spring spring是一个轻量级的…

【牛客小白月赛117】E题——种类数小结

1 初步想法 1.1 前置知识&#xff1a;vector数组的去重操作 unique()将不重复的元素放在数组前面&#xff0c;重复元素移到后面&#xff0c;qs获取不重复元素的后一个位置&#xff0c;之后用erase()函数去除重复元素。 qsunique(a.begin()1,a.begin()k1); a.erase(qs,a.end(…

linux之kylin系统nginx的安装

一、nginx的作用 1.可做高性能的web服务器 直接处理静态资源&#xff08;HTML/CSS/图片等&#xff09;&#xff0c;响应速度远超传统服务器类似apache支持高并发连接 2.反向代理服务器 隐藏后端服务器IP地址&#xff0c;提高安全性 3.负载均衡服务器 支持多种策略分发流量…

MatAnyone本地部署,视频分割处理,绿幕抠像(WIN/MAC)

大家好&#xff0c;今天要和大家分享的项目是MatAnyone&#xff0c;与上一篇分享的SAM2LONG类似&#xff0c;不过上次的分享没有提到如何在 MAC 上部署&#xff0c;后来有小伙伴私信说希望能出一个 MAC 版本的。那正好看到MatAnyone这个项目顺手就写下来。该项目基于SAM2同样可…

记录下blog的成长过程

2025-06-11 新人榜83 2025-06-09 新人榜87 北京市原力月榜 80

C语言学习20250611

指针 指针类型 int p;》普通的整形变量int *p;》p先与*结合&#xff0c;表示p为指针&#xff0c;该指针指向的内容的数据类型为整型int p[3];》p为一个由整型数据组成的数组int *p[3];》因为[]比*优先级高&#xff0c;p先与方括号结合&#xff0c;所以p为一个数组&#xff0c…

【AI智能体】Dify 从部署到使用操作详解

目录 一、前言 二、Dify 介绍 2.1 Dify 是什么 2.2 Dify 核心特性 2.2.1 多模型支持 2.2.2 可视化编排工作流 2.2.3 低代码/无代码开发 2.3 Dify 适用场景 2.4 Dify 与Coze的对比 2.4.1 定位与目标用户 2.4.2 核心功能对比 2.4.3 开发体验与成本 2.4.4 适用场景对比…

Java爬虫库的选择与实战代码

如果你的项目正在Java中考虑引入爬虫能力&#xff0c;无论是做数据分析、信息聚合&#xff0c;还是竞品监测&#xff0c;选对库确实能大幅提升开发效率和运行效果。结合当前主流库的特点与适用场景&#xff0c;我整理了一份更贴近实战的对比分析&#xff0c;并附上可直接运行的…

详细解释aruco::markdetection _detectInitialCandidates函数

_detectInitialCandidates 是 OpenCV 的 ArUco 模块中一个非常关键的函数&#xff0c;它负责检测图像中的候选 ArUco 标记。该函数的主要目标是&#xff1a; 使用多个尺度&#xff08;scale&#xff09;对输入图像进行自适应阈值处理&#xff1b;在每个尺度下提取轮廓并筛选出…

Android 开发中配置 USB 配件模式(Accessory Mode) 配件过滤器的配置

在 Android 开发中配置 USB 配件模式&#xff08;Accessory Mode&#xff09; 的配件过滤器&#xff08;accessory_filter.xml&#xff09;&#xff0c;需要以下步骤&#xff1a; 1. 创建配件过滤器文件 在项目的 res/xml/ 目录下创建 accessory_filter.xml 文件&#xff08;若…

FreeRTOS互斥量

目录 1.使用场合2.函数2.1 创建2.1.1 动态创建2.1.2 静态创建 2.2 删除2.3 释放&#xff08;Give&#xff09;2.4 获取&#xff08;Take&#xff09;2.5 ISR 版本注意事项 3.常规使用流程4.和二进制信号量的对比5.递归锁5.1 死锁5.2 概念5.2.1 问题5.2.2 解决方案&#xff1a;递…