UNIAPP中NVUE页面 动画

UNIAPP中NVUE页面 动画

需要调用原生动画模块

javascript 复制代码
const animation = uni.requireNativePlugin('animation')

这里模拟一个刷新动画

html 复制代码
<image ref="refreshIcon" class="refresh" src="/static/icon/refresh.png" @click="getWeeklyOverviewFun()"></image>
javascript 复制代码
rotateAngle: 0, // 记录当前旋转角度
animationTimer: null,
isReshSportIcon: false,
				
async getWeeklyOverviewFun(from) {
	if (this.isReshSportIcon) return
	this.isReshSportIcon = true;
	this.startRotateAnimation()
	try {
		const res = await 获取服务端信息
	} catch (error) {
		console.error("失败:", error);
	} finally {
		this.stopRotateAnimation();
		this.isReshSportIcon = false;
	}
}
startRotateAnimation() {
	const rotateStep = () => {
			if (!this.isReshSportIcon) return
			this.rotateAngle += 360

			// 执行原生旋转动画
			animation.transition(this.$refs.refreshIcon, {
				styles: {
							transform: `rotate(${this.rotateAngle}deg)`,
							transformOrigin: 'center center'
				},
				duration: 1000, // 旋转一圈的时间(毫秒)
				timingFunction: 'linear' // 匀速旋转
			}, () => {
				// 动画完成后递归调用,实现无限循环
				rotateStep()
			})
	}
	rotateStep()
},
stopRotateAnimation() {
	this.isReshSportIcon = false;
	// 计算当前角度到0度的最短路径
	const currentAngle = this.rotateAngle % 360
	const targetAngle = this.rotateAngle + (360 - currentAngle)

	// 平滑转到0度后停止
	animation.transition(this.$refs.refreshIcon, {
			styles: {
				transform: `rotate(${targetAngle}deg)`,
				transformOrigin: 'center center'
			},
			duration: 300, // 归位动画时间
			timingFunction: 'ease-out'
	}, () => {
			this.rotateAngle = 0
	})
}
相关推荐
kyriewen9 分钟前
前端错误监控最全指南:捕获 JS 异常、Promise 拒绝、资源加载失败,附上报代码
前端·javascript·监控
狗哥哥21 分钟前
船队运营可视化技术方案
前端
大家的林语冰24 分钟前
ESLint 近期动态大全,新版本正式发布,antfu 大佬推荐的插件也更新了!
前端·javascript·前端工程化
只会cv的前端攻城狮26 分钟前
DSL 领域模型架构设计:消灭 CRUD 重复工作
前端·架构
码事漫谈1 小时前
时序数据库2026盘点:国产数据库如何以“融合多模”走出差异化之路?
前端·后端
道友可好1 小时前
让 AI 自己验收,等于让学生自己批卷
前端·人工智能·后端
yingyima1 小时前
Go 语言正则表达式速查手册:30 分钟掌握核心语法与实战技巧
前端
大蝴蝶博努奇a2 小时前
使用ChatGPT 解决各类代码报错
前端
胡志辉2 小时前
深入浅出 call、apply、bind
前端·javascript·后端
iccb10132 小时前
5年,一个程序员是如何把私有化在线客服系统做到第一名的
前端·后端·github