当前位置: 首页 > news >正文

uniapp uni.createAnimation为文件交换位置动画效果

需求:

采用uni.createAnimation,为文件列表调换位置添加动画效果

效果图

在这里插入图片描述

代码

<view><view class="section-title">修改前</view><view class="file-section" :animation="animationData1">业务内容代码。。。</view><view class="section-title">修改后</view><view class="file-section" :animation="animationData2">业务内容代码。。。</view><button @click="swap">交换位置</button>
</view>
const animationData1 = ref(null);
const animationData2 = ref(null);
const isAnimating = ref(false);
const fileList = ref([]); //文件列表// 创建动画实例
function createAnimation() {const animation1 = uni.createAnimation({duration: 300,timingFunction: 'linear',delay: 0});const animation2 = uni.createAnimation({duration: 300,timingFunction: 'linear',delay: 0});return { animation1, animation2 };
}function swap() {isAnimating.value = true;// 获取元素位置信息const query = uni.createSelectorQuery();query.selectAll('.file-section').boundingClientRect(data => {if (!data || data.length < 2) {isAnimating.value = false;return;}const [section1Rect, section2Rect] = data;const { animation1, animation2 } = createAnimation();// 计算相对位移const moveY1 = section2Rect.top - section1Rect.top;const moveY2 = section1Rect.top - section2Rect.top;// 应用动画animation1.translateY(moveY1).step();animation2.translateY(moveY2).step();animationData1.value = animation1.export();animationData2.value = animation2.export();// 动画结束后交换数据setTimeout(() => {// 交换数据fileList = fileList.reverse()// 重置动画状态animation1.translateY(0).step({ duration: 0 });animation2.translateY(0).step({ duration: 0 });animationData1.value = animation1.export();animationData2.value = animation2.export();isAnimating.value = false;}, 1000);}).exec();
}onMounted(() => {// 初始化动画数据const { animation1, animation2 } = createAnimation();animationData1.value = animation1.export();animationData2.value = animation2.export();
});

相关文章:

  • Uniapp实现多选下拉框
  • Linux C学习路线全概括及知识点笔记3-网络编程
  • uniapp音乐播放createInnerAudioContext
  • # include<heαd.h>和# include″heαd.h″的区别
  • Argocd 运维与最佳实践
  • 【uniapp】小程序中input输入框的placeholder-class不生效
  • 【C++】来学习使用set和map吧
  • matlab分布式电源微电网潮流
  • DAS-U250高性能分布式光纤声波传感器
  • Spring @Scope, @Lazy, @DependsOn, @Required, @Lookup
  • 机器学习核心概念速览
  • Springboot项目的目录结构
  • AI赋能Automa二次开发
  • Spring @Qualifier,@Primary
  • 分布式ID实现方案实战示例总结
  • 【Zephyr 系列 18】分布式传感网络系统设计:从 BLE Mesh 到边缘网关的数据闭环
  • 看安科瑞分布式光伏解决方案如何破解光伏痛点?
  • 机器学习赋能的智能光子学器件系统研究与应用
  • 基于生成对抗网络(GAN)的图像生成与编辑:原理、应用与实践
  • Idea 2025 commit 关闭侧边栏 开启探框
  • 泉州网站公司/网络广告营销案例有哪些
  • 上海做壁画的网站/收录是什么意思
  • 不会PS怎么建网站/推广优化
  • 扬中潘杰简历/培训推广 seo
  • 网站建设与维护总结/站长工具站长
  • 常用的网页制作工具有什么/官方进一步优化