
animated 类是 animate.css 动画库的核心组成部分,它本身不定义具体动画,而是作为激活其他动画效果(如弹跳、摇晃、淡出)的基础类。通过结合特定的动画类名,并常与 javascript/jquery 配合使用,开发者可以轻松为网页元素添加丰富的预设动画效果,极大地提升用户体验和界面互动性。
在现代前端开发中,为网页元素添加动态效果是提升用户体验的重要手段。其中,animated 类经常出现在各种动画代码示例中,它并非直接来自 Bootstrap 或 jQuery,而是 Animate.css 这一流行的 CSS 动画库的关键组成部分。理解其作用和用法,对于开发者而言至关重要。
Animate.css 是一个即用型、跨浏览器的 CSS 动画库,它提供了一系列预设的动画效果,如弹跳(bounce)、摇晃(shake)、淡入淡出(fadeIn/fadeOut)等。要使用这些动画,通常需要将两个类名添加到目标 HTML 元素上:
如果没有 animated 类,即使添加了 bounce 或 shake 等动画类,元素也不会播放动画,因为缺少了激活和控制动画的基础设置。
要在项目中使用 Animate.css,首先需要将其引入到你的 HTML 页面中。最常见的方式是通过 CDN(内容分发网络)链接:
立即学习“前端免费学习笔记(深入)”;
<!DOCTYPE html>
<html lang="zh-CN">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Animate.css 应用示例</title>
<!-- 引入 Animate.css 样式表 -->
<link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/animate.css/4.1.1/animate.min.css"/>
<!-- 如果使用 jQuery 动态添加动画,也需要引入 jQuery -->
<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.5.1/jquery.min.js"></script>
<style>
.container {
margin-top: 50px;
text-align: center;
}
.interactive-box {
padding: 20px;
border: 2px solid #007bff;
background-color: #e0f7fa;
margin: 20px auto;
width: 200px;
height: 100px;
display: flex;
align-items: center;
justify-content: center;
font-size: 1.2em;
color: #0056b3;
cursor: pointer;
box-shadow: 0 4px 8px rgba(0,0,0,0.1);
}
button {
padding: 10px 20px;
font-size: 16px;
cursor: pointer;
margin: 10px;
background-color: #28a745;
color: white;
border: none;
border-radius: 5px;
}
button:hover {
opacity: 0.9;
}
</style>
</head>
<body>
<div class="container">
<h1>Animate.css 动态效果演示</h1>
<button id="bounceButton">点击我弹跳</button>
<button id="shakeButton">点击我摇晃</button>
<button id="fadeOutButton">点击我淡出</button>
<div id="targetBox1" class="interactive-box">
我是一个可弹跳的盒子
</div>
<div id="targetBox2" class="interactive-box">
我是一个可摇晃的盒子
</div>
<div id="targetBox3" class="interactive-box" style="background-color: #ffc107; color: #856404;">
我是一个可淡出的盒子
</div>
</div>
<script>
$(document).ready(function() {
// 辅助函数:移除动画类并重新添加以触发动画
function triggerAnimation(element, animationClass) {
element.removeClass('animated ' + animationClass);
// 强制浏览器重绘,确保动画可以重复播放
void element[0].offsetWidth;
element.addClass('animated ' + animationClass);
}
$("#bounceButton").on("click", function() {
triggerAnimation($("#targetBox1"), "bounce");
});
$("#shakeButton").on("click", function() {
triggerAnimation($("#targetBox2"), "shake");
});
$("#fadeOutButton").on("click", function() {
// 淡出动画通常只播放一次,播放后元素会隐藏
$("#targetBox3").removeClass('animated fadeOut');
void $("#targetBox3")[0].offsetWidth;
$("#targetBox3").addClass('animated fadeOut');
// 如果需要再次显示,可以移除 fadeOut 类
// setTimeout(function() {
// $("#targetBox3").removeClass('animated fadeOut').show();
// }, 2000); // 2秒后重新显示
});
// 示例:页面加载后自动为某个元素添加动画
// $("#targetBox1").addClass("animated bounce");
});
</script>
</body>
</html>在实际应用中,我们经常需要根据用户交互(如点击按钮)来动态触发动画。这通常通过 JavaScript 或 jQuery 来实现,其核心思想是向目标元素添加 animated 类和具体的动画类。
例如,原始问题中提供的代码片段:
$("button").addClass("animated bounce");
$(".well").addClass("animated shake");
$("#target3").addClass("fadeOut"); // 注意:这里缺少 animated 类,动画可能不会生效这段代码使用 jQuery 的 addClass() 方法来为选定的元素添加 CSS 类。需要注意的是,$("#target3").addClass("fadeOut"); 这一行如果 animated 类没有预先存在,动画将不会播放。正确的做法应该是:
$("#target3").addClass("animated fadeOut");$('#myElement').on('animationend', function() {
$(this).removeClass('animated bounce');
});animated 类是 Animate.css 动画库的基石,它与具体的动画类(如 bounce、shake、fadeOut)协同工作,为网页元素带来丰富的动态效果。通过引入 Animate.css 库,并结合 JavaScript/jQuery 动态添加这些类,开发者可以轻松地实现各种交互式和视觉吸引力强的用户界面。掌握其用法和注意事项,将有助于开发者更高效、更优雅地为项目添加动画。
以上就是深入理解 animated 类:Animate.css 动画库的应用指南的详细内容,更多请关注php中文网其它相关文章!
每个人都需要一台速度更快、更稳定的 PC。随着时间的推移,垃圾文件、旧注册表数据和不必要的后台进程会占用资源并降低性能。幸运的是,许多工具可以让 Windows 保持平稳运行。
Copyright 2014-2025 https://www.php.cn/ All Rights Reserved | php.cn | 湘ICP备2023035733号