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
繁體地址,請注明出處:http://hk.pswp.cn/diannao/86667.shtml
英文地址,請注明出處:http://en.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;遞…