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

如若内容造成侵权/违法违规/事实不符,请联系多彩编程网进行投诉反馈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;员工们仿佛置身于同一个真实的办公室空间…