Vue3 Anime.js超級炫酷的網頁動畫庫詳解

簡介

Anime.js 是一個輕量級的 JavaScript 動畫庫,它提供了簡單而強大的 API 來創建各種復雜的動畫效果。以下是 Anime.js 的主要使用方法和特性:

安裝

npm install animejs

基本用法

<script setup>
import { ref, onMounted } from "vue";
import anime from "animejs";const rotations = ref(0);
const logoRef = ref(null);const handleClick = () => {rotations.value += 1;anime({targets: logoRef.value,rotate: rotations.value * 360,easing: "easeOutQuart",duration: 2000,});
};onMounted(() => {anime({targets: logoRef.value,scale: [{ value: 1.25, easing: "easeInOutCubic", duration: 200 },{ value: 1, easing: "spring(1, 80, 10, 0)" },],loop: true,direction: "alternate",loopDelay: 250,});
});
</script><template><div class="mt-10"><img ref="logoRef" src="@/assets/react.svg" class="logo" alt="React logo" /><buttonclass="px-4 py-2 bg-blue-500 text-white rounded-lg hover:bg-blue-600 transition-colors mt-4"@click="handleClick">旋轉</button></div>
</template>

定時器(timer)

<script setup>
import { ref, onMounted, onUnmounted } from "vue";const time = ref(0);
const count = ref(0);
let interval = null;onMounted(() => {let currentIteration = 0;interval = setInterval(() => {time.value = Date.now();currentIteration += 1;count.value = currentIteration;}, 1000 / 30);
});onUnmounted(() => {clearInterval(interval);
});
</script><template><div class="m-10 container mx-auto max-w-2xl"><div class="flex flex-row justify-center items-center gap-8"><divclass="relative w-64 h-24 rounded-lg overflow-hidden shadow-lg bg-[#6d402a] flex flex-col items-center justify-center"><span class="text-lg text-white mb-2">current time</span><spanclass="text-6xl font-mono font-bold tracking-widest text-[#ffa94d] lcd">{{ time }}</span></div><divclass="relative w-64 h-24 rounded-lg overflow-hidden shadow-lg bg-[#6d402a] flex flex-col items-center justify-center"><span class="text-lg text-white mb-2">callback fired</span><spanclass="text-6xl font-mono font-bold tracking-widest text-[#ffa94d] lcd">{{ count }}</span></div></div></div>
</template>

時間線(timeline)

<script setup>
import { onMounted } from "vue";
import anime from "animejs";onMounted(() => {const tl = anime.timeline({ duration: 750 });tl.add({ targets: ".square", translateX: "15rem" }).add({ targets: ".circle", translateX: "15rem" }).add({ targets: ".triangle", translateX: "15rem", rotate: "1turn" });
});
</script><template><div class="flex gap-4 mt-10"><div class="square w-16 h-16 bg-red-400 rounded"></div><div class="circle w-16 h-16 bg-green-400 rounded-full"></div><divclass="triangle"style="width: 0; height: 0; border-left: 32px solid transparent; border-right: 32px solid transparent; border-bottom: 64px solid #60a5fa;"></div></div>
</template>

動畫(animate)

<script setup>
import { ref } from "vue";
import anime from "animejs";const boxRef = ref(null);const handleAnimate = () => {anime({targets: boxRef.value,opacity: [0, 1],scale: [0.5, 1.2, 1],rotate: [0, 360],duration: 1200,easing: "easeInOutCubic",});
};
</script><template><div class="mt-10 flex flex-col items-center"><div ref="boxRef" class="w-24 h-24 bg-purple-400 rounded mb-4"></div><button@click="handleAnimate"class="px-4 py-2 bg-purple-600 text-white rounded">動畫</button></div>
</template>

拖動(drag)

<script setup>
import { ref, onMounted } from "vue";
import interact from "interactjs";const containerRef = ref(null);
const squareRef = ref(null);onMounted(() => {interact(squareRef.value).draggable({inertia: true,modifiers: [interact.modifiers.restrictRect({restriction: containerRef.value,endOnly: true,}),],listeners: {move(event) {const target = event.target;const x = (parseFloat(target.getAttribute("data-x")) || 0) + event.dx;const y = (parseFloat(target.getAttribute("data-y")) || 0) + event.dy;target.style.transform = `translate(${x}px, ${y}px)`;target.setAttribute("data-x", x);target.setAttribute("data-y", y);},},});
});
</script><template><divref="containerRef"class="mt-10 w-[400px] h-[200px] bg-gray-100 relative flex items-center justify-center"><divref="squareRef"class="square w-16 h-16 bg-yellow-400 rounded absolute top-0 left-0 cursor-move"></div></div>
</template>

滾動(scroll)

<script setup>
import { ref, onMounted, onUnmounted } from "vue";
import anime from "animejs";const lastProgress = ref(0);
let ticking = false;const onScroll = () => {if (!ticking) {window.requestAnimationFrame(() => {const scrollY = window.scrollY;const docHeight = document.body.scrollHeight - window.innerHeight;const progress = Math.min(scrollY / docHeight, 1);if (Math.abs(progress - lastProgress.value) > 0.001) {lastProgress.value = progress;// 1. 粉色盒子:分段動畫if (progress < 0.33) {anime({targets: ".scroll-box",translateY: progress * 600,scale: 1 + progress * 1.5,opacity: 1,rotate: 0,background: "#f472b6",duration: 400,easing: "easeOutCubic",});} else if (progress < 0.66) {anime({targets: ".scroll-box",translateY: 200 + (progress - 0.33) * 600,scale: 1.5,opacity: 1 - (progress - 0.33) * 1.5,rotate: (progress - 0.33) * 720,background: "#fbbf24",duration: 400,easing: "easeInOutCubic",});} else {anime({targets: ".scroll-box",translateY: 400 + (progress - 0.66) * 600,scale: 1.5 - (progress - 0.66) * 1.5,opacity: 0.5 - (progress - 0.66) * 1.5,rotate: 360,background: "#34d399",duration: 400,easing: "easeInCubic",});}// 2. 藍色盒子:左右來回移動+彈性anime({targets: ".scroll-box2",translateX: Math.sin(progress * Math.PI * 2) * 300,scale: 1 + Math.abs(Math.cos(progress * Math.PI)),rotate: progress * 360,background: "#60a5fa",duration: 500,easing: "easeOutElastic(1, .5)",});// 3. 進度條anime({targets: ".scroll-progress",scaleX: progress,duration: 200,easing: "linear",});// 4. 文字漸顯anime({targets: ".scroll-text",opacity: progress,translateY: 100 - progress * 100,duration: 400,easing: "easeOutCubic",});}ticking = false;});ticking = true;}
};onMounted(() => {window.addEventListener("scroll", onScroll);
});onUnmounted(() => {window.removeEventListener("scroll", onScroll);
});
</script><template><div class="h-[2500px] relative"><!-- 滾動進度條 --><div class="fixed top-0 left-0 w-full h-2 bg-gray-200 z-50"><divclass="scroll-progress origin-left h-full bg-pink-400 scale-x-0"></div></div><!-- 動畫盒子1 --><divclass="scroll-box w-32 h-32 bg-pink-400 fixed top-20 left-20 rounded-lg shadow-lg"></div><!-- 動畫盒子2 --><divclass="scroll-box2 w-32 h-32 bg-blue-400 fixed top-60 left-60 rounded-lg shadow-lg"></div><!-- 漸顯文字 --><divclass="scroll-text fixed top-[300px] left-1/2 -translate-x-1/2 w-[600px] text-3xl text-gray-700 opacity-0"><p>分段動畫、彈性、漸變、旋轉、縮放、透明度,全部聯動!</p><p class="mt-10 text-xl">繼續滾動,體驗更豐富的滾動動畫效果。</p></div><!-- 內容填充 --><divclass="absolute top-[700px] left-1/2 -translate-x-1/2 w-[600px] text-xl text-gray-700"><p>你可以繼續添加更多動畫元素,或根據滾動區間分段控制動畫效果。</p></div></div>
</template>

SVG 動畫效果

<script setup>
import { ref, onMounted } from "vue";
import anime from "animejs";const pathRef = ref(null);onMounted(() => {if (pathRef.value) {const length = pathRef.value.getTotalLength();pathRef.value.style.strokeDasharray = length;pathRef.value.style.strokeDashoffset = length;anime({targets: pathRef.value,strokeDashoffset: [length, 0],duration: 2000,easing: "easeOutCubic",});}
});
</script><template><div class="flex flex-col items-center mt-10"><svg width="320" height="120" viewBox="0 0 320 120"><pathref="pathRef"d="M20,60 Q160,10 300,60 T300,110"stroke="#f472b6"stroke-width="6"fill="none"/></svg><p class="mt-4 text-lg text-gray-700">SVG 路徑描邊動畫</p></div>
</template>

?Vue3 Anime.js超級炫酷的網頁動畫庫詳解 - 高質量源碼分享平臺-免費下載各類網站源碼與模板及前沿技術分享

本文來自互聯網用戶投稿,該文觀點僅代表作者本人,不代表本站立場。本站僅提供信息存儲空間服務,不擁有所有權,不承擔相關法律責任。
如若轉載,請注明出處:http://www.pswp.cn/bicheng/89590.shtml
繁體地址,請注明出處:http://hk.pswp.cn/bicheng/89590.shtml
英文地址,請注明出處:http://en.pswp.cn/bicheng/89590.shtml

如若內容造成侵權/違法違規/事實不符,請聯系多彩編程網進行投訴反饋email:809451989@qq.com,一經查實,立即刪除!

相關文章

苦練Python第18天:Python異常處理錦囊

苦練Python第18天&#xff1a;Python異常處理錦囊 原文鏈接&#xff1a;https://dev.to/therahul_gupta/day-18100-exception-handling-with-try-except-in-python-3m5a 作者&#xff1a;Rahul Gupta 譯者&#xff1a;倔強青銅三 前言 大家好&#xff0c;我是倔強青銅三。是一名…

JVM——如何對java的垃圾回收機制調優?

GC 調優的核心思路就是盡可能的使對象在年輕代被回收&#xff0c;減少對象進入老年代。 具體調優還是得看場景根據 GC 日志具體分析&#xff0c;常見的需要關注的指標是 Young GC 和 Full GC 觸發頻率、原因、晉升的速率、老年代內存占用量等等。 比如發現頻繁會產生 Ful GC&am…

正則表達式使用示例

下面以 Vue&#xff08;前端&#xff09;和 Spring Boot&#xff08;后端&#xff09;為例&#xff0c;展示正則表達式在前后端交互中的應用&#xff0c;以郵箱格式驗證為場景&#xff1a;1.前端<template><div class"register-container"><h3>用戶…

云端微光,AI啟航:低代碼開發的智造未來

文章目錄前言一、引言&#xff1a;技術浪潮中的個人視角初次體驗騰訊云開發 Copilot1.1 低代碼的時代機遇1.1.1 為什么低代碼如此重要&#xff1f;1.2 AI 的引入&#xff1a;革新的力量1.1.2 Copilot 的亮點1.3 初學者的視角1.3.1 Copilot 帶來的改變二、體驗記錄&#xff1a;云…

圖片上傳實現

圖片上傳change函數圖片上傳圖片上傳到服務器上傳的圖片在該頁面中顯示修改界面代碼最終實現效果change函數 這里我們先用輸入框控件來舉例&#xff1a; 姓名&#xff1a;<input typetext classname>下面我們來寫 js 語句&#xff0c;對控件進行綁事件來獲取輸入框內的…

【PTA數據結構 | C語言版】多叉堆的上下調整

本專欄持續輸出數據結構題目集&#xff0c;歡迎訂閱。 文章目錄題目代碼題目 請編寫程序&#xff0c;將 n 個已經滿足 d 叉最小堆順序約束的數據直接讀入最小堆&#xff1b;隨后將下一個讀入的數據 x 插入堆&#xff1b;再執行刪頂操作并輸出刪頂的元素&#xff1b;最后順次輸…

selenium后續!!

小項目案例:實現批量下載網頁中的資源根據15.3.2小節中的返回網頁內容可知,用戶只有獲取了網頁中的圖片url才可以將圖片下載到*在使用selenium庫渲染網頁后,可直接通過正則表達式過濾出指定的網頁圖片&#xff0c;從而實現批量下載接下來以此為思路來實現一個小項目案例。項目任…

深度解析Linux文件I/O三級緩沖體系:用戶緩沖區→標準I/O→內核頁緩存

在Linux文件I/O操作中&#xff0c;緩沖區的管理是一個核心概念&#xff0c;主要涉及用戶空間緩沖區和內核空間緩沖區。理解這兩者的區別和工作原理對于高效的文件操作至關重要。 目錄 一、什么是緩沖區 二、為什么要引入緩沖區機制 三、三級緩沖體系 1、三級緩沖體系全景圖…

【每日算法】專題十三_隊列 + 寬搜(bfs)

1. 算法思路 BFS 算法核心思路 BFS&#xff08;廣度優先搜索&#xff09;使用 隊列&#xff08;Queue&#xff09;按層級順序遍歷圖或樹的節點。以下是 C 實現的核心思路和代碼模板&#xff1a; 算法框架 #include <queue> #include <vector> #include <un…

【動手實驗】發送接收窗口對 TCP傳輸性能的影響

環境準備 服務器信息 兩臺騰訊云機器 t04&#xff08;172.19.0.4&#xff09;、t11&#xff08;172.19.0.11&#xff09;&#xff0c;系統為 Ubuntu 22.04&#xff0c;內核為 5.15.0-139-generic。默認 RT 在 0.16s 左右。 $ ping 172.19.0.4 PING 172.19.0.4 (172.19.0.4) …

28、鴻蒙Harmony Next開發:不依賴UI組件的全局氣泡提示 (openPopup)和不依賴UI組件的全局菜單 (openMenu)、Toast

目錄 不依賴UI組件的全局氣泡提示 (openPopup) 彈出氣泡 創建ComponentContent 綁定組件信息 設置彈出氣泡樣式 更新氣泡樣式 關閉氣泡 在HAR包中使用全局氣泡提示 不依賴UI組件的全局菜單 (openMenu) 彈出菜單 創建ComponentContent 綁定組件信息 設置彈出菜單樣…

讓老舊醫療設備“聽懂”新語言:CAN轉EtherCAT的醫療行業應用

在醫療影像設備的智能化升級中&#xff0c;通信協議的兼容性常成為工程師的“痛點”。例如&#xff0c;某醫院的移動式X射線機采用CAN協議控制機械臂&#xff0c;而主控系統基于EtherCAT架構。兩者協議差異導致數據延遲高達5ms&#xff0c;影像定位精度下降&#xff0c;甚至影響…

ubuntu基礎搭建

ubuntu上docker的搭建 https://vulhub.org/zh 網站最下面找到開始使用&#xff0c;有搭建的命令//安裝docker&#xff0c;連接失敗多試幾次 curl -fsSL https://get.docker.com | sh //驗證Docker是否正確安裝&#xff1a; docker version //還要驗證Docker Compose是否可用&am…

動態規劃 + DFS + 記憶化!Swift 解 LeetCode 329 的實戰筆記

文章目錄摘要描述題解答案題解代碼分析代碼解析示例測試及結果時間復雜度空間復雜度總結摘要 這篇文章帶你用 Swift 實戰一道非常經典的 DFS 記憶化搜索題目 —— LeetCode 329《矩陣中的最長遞增路徑》。看似一個簡單的“走格子”游戲&#xff0c;實則考察了搜索順序、剪枝策…

046_局部內部類與匿名內部類

一、局部內部類&#xff08;Local Inner Class&#xff09; 1.1 定義與基本概念 局部內部類是定義在方法、構造器或代碼塊內部的類&#xff0c;其作用域僅限于所在的局部范圍&#xff08;定義它的方法、構造器或代碼塊&#xff09;&#xff0c;超出該范圍則無法訪問。 它的核心…

Jenkins Pipeline 中使用 JsonSlurper 報錯:cannot find current thread

Jenkins Pipeline 中使用 JsonSlurper 報錯&#xff1a;cannot find current thread&#x1f31f; 背景? 問題重現&#x1f9e0; 原因解析&#xff1a;CPS 與非 CPS 安全方法沖突? 解決方案一&#xff1a;使用 NonCPS 注解&#xff08;經典方案&#xff09;? 解決方案二&…

Go 語言循環語句詳解

Go 語言循環語句詳解 在編程語言中&#xff0c;循環語句是實現重復執行某些代碼塊的關鍵元素。Go 語言作為現代編程語言之一&#xff0c;提供了多種循環結構來滿足不同的編程需求。本文將詳細講解 Go 語言中的循環語句&#xff0c;包括 for、while 和 goto 語句&#xff0c;幫助…

day30——零基礎學嵌入式之進程間通信1.0

一、進程間通信7種方式1.傳統的進程間通信方式&#xff08;1&#xff09;管道①無名管道&#xff1a;②有名管道&#xff1a;&#xff08;2&#xff09;③信號&#xff08;3&#xff09;system Ⅴ 》系統Ⅴ 進程間通信方式 inner Process Comunication④共享內存 &#xff…

408考研逐題詳解:2010年第33題——網絡體系結構

2010年第33題 下列選項中&#xff0c;不屬于網絡體系結構所描述的內容是&#xff08; &#xff09; A. 網絡的層次 \qquad B. 每層使用的協議 \qquad C. 協議的內部實現細節 \qquad D. 每層必須完成的功能 解析 本題屬于計算機網絡基礎知識的范疇&#xff0c;考查網絡體系結構…

VR 遠程系統的沉浸式協作體驗?

在傳統的遠程協作中&#xff0c;團隊成員往往通過二維的視頻畫面進行交流&#xff0c;這種方式雖然能實現基本的溝通&#xff0c;但缺乏真實感和互動性。而 VR 遠程系統的出現&#xff0c;徹底改變了這一局面。戴上 VR 設備&#xff0c;員工們仿佛置身于同一個真實的辦公室空間…