微信小程序开发之animation循环动画实现的让云朵飘效果
作者:第九程序 时间:2024-04-16 09:00:11
本文实例讲述了微信小程序开发之animation循环动画实现的让云朵飘效果。分享给大家供大家参考,具体如下:
微信小程序提供了实现动画的api——animation,但却不能循环播放,都是一次性的,动完就Over了,下面提供一个用微信小程序的animation来实现循环动画的玩具,抛砖引玉,希望大家能想出更好的方法来实现真正的循环。说是玩具是因为这个循环动画通过js脚本的setInterval来实现的,但'setInterval'在实际运行中会出现越来越严重的延迟,这是由于js的单线程运行模式所决定的(具体可以搜本关资料看),所以动画间隙并不是那么流畅,所以先玩玩吧,让我们来实现让云朵飘……
截图如下:
实现代码:
index.wxml
<view class="clouds">
<image animation="{{animationCloudData}}" class="yun1" src="../../img/yun1.png"></image>
</view>
index.js
onReady: function () {
// 页面渲染完成
// 实例化一个动画
var that = this;
var i = 0
var ii = 0
var animationData = wx.createAnimation({
duration: 1000, // 默认为400 动画持续时间,单位ms
timingFunction: 'ease-in-out',
//transformOrigin: '4px 91px'
});
var animationCloudData = wx.createAnimation({
duration: 1000, // 默认为400 动画持续时间,单位ms
timingFunction: 'ease-in-out',
//transformOrigin: '4px 91px'
});
// 顺序执行,当已经执行完上面的代码就会开启定时器
// 循环执行代码
//dotAnFun = setInterval(function () {});
/*setInterval(function () {
// 动画脚本定义
//animationData.rotate(6 * (++i)).step()
//animationData.scale(2, 2).rotate(45).step().scale(1, 1).step();
animationData.translateY(10).step({ duration: 500 }).translateY(-10).step({ duration: 500 });
// 更新数据
that.setData({
// 导出动画示例
animationData: animationData.export(),
//animationCloudData: animationCloudData.export(),
})
++i;
console.log(i);
}.bind(that), 2000);//循环时间 这里1000是1秒
*/
//动画的脚本定义必须每次都重新生成,不能放在循环外
animationCloudData.translateX(200).step({ duration: 5000 }).translateX(0).step({ duration: 5000 });
// 更新数据
that.setData({
// 导出动画示例
//animationData: animationData.export(),
animationCloudData: animationCloudData.export(),
})
setInterval(function () {
//动画的脚本定义必须每次都重新生成,不能放在循环外
animationCloudData.translateX(300).step({ duration: 5000 }).translateX(-100).step({ duration: 5000 });
// 更新数据
that.setData({
// 导出动画示例
//animationData: animationData.export(),
animationCloudData: animationCloudData.export(),
})
++ii;
console.log(ii);
}.bind(that),10000);//3000这里的设置如果小于动画step的持续时间的话会导致执行一半后出错
}
index.wxss
.clouds{
margin-top:320rpx;
}
.yun1{
width:320rpx;
height: 120rpx;
}
附:参考备用:
/*
var that = this;
// 页面渲染完成
//实例化一个动画
var animation = wx.createAnimation({
duration: 1000,
timingFunction: 'ease',
})
this.animation = animation
animation.scale(2, 2).rotate(45).step().scale(1,1).step();
//导出动画
this.setData({
animationData: animation.export()
})
var i = 0;
// 顺序执行,当已经执行完上面的代码就会开启定时器
/*setTimeout(function () {
that.setData({
animationData: animation.export()
});
i++;
console.log(i);
}, 1000);*/
/*setInterval(function () {
//循环执行代码
that.setData({
animationData: animation.export()
});
i++;
console.log(i);
}, 1000) //循环时间 这里是1秒
}*/
更多关于JavaScript
希望本文所述对大家JavaScript程序设计有所帮助。
标签:微信小程序,animation,动画
![](/images/zang.png)
![](/images/jiucuo.png)
猜你喜欢
Python检测数据类型的方法总结
2022-08-29 22:07:58
![](https://img.aspxhome.com/file/2023/1/65461_0s.png)
Go语言基于HTTP的内存缓存服务的实现
2024-05-21 10:25:12
Asp 防止网页频繁刷新一法
2008-04-07 13:03:00
Go语言CSP并发模型实现MPG
2024-05-22 17:46:48
![](https://img.aspxhome.com/file/2023/2/125092_0s.jpg)
js实现复选框的全选和取消全选效果
2024-05-02 17:40:02
oracle 重置序列从指定数字开始的方法详解
2023-07-05 02:40:04
Python中的协程(Coroutine)操作模块(greenlet、gevent)
2021-01-17 12:24:25
纯CSS制作的网页中的lightbox效果
2007-11-06 18:59:00
Vue element-ui表格内嵌进度条功能实现方法
2024-05-28 15:51:59
![](https://img.aspxhome.com/file/2023/7/123157_0s.png)
python迭代器实例简析
2023-07-26 07:40:29
js定时器怎么写?就是在特定时间执行某段程序
2024-04-22 12:54:00
webpack 打包压缩js和css的方法示例
2023-07-02 05:18:32
分析Python读取文件时的路径问题
2022-03-24 10:22:48
![](https://img.aspxhome.com/file/2023/8/67728_0s.png)
十条建议帮你提高Python编程效率
2021-07-18 02:55:54
![](https://img.aspxhome.com/file/2023/2/98852_0s.jpg)
pyqt5自定义信号实例解析
2022-01-15 15:15:15
![](https://img.aspxhome.com/file/2023/8/131538_0s.png)
PHP实现mysqli批量执行多条语句的方法示例
2024-05-05 09:31:35
MySQL学习笔记之数据定义表约束,分页方法总结
2024-01-13 13:46:38
利用pygame完成动画精灵和碰撞检测
2022-05-06 22:53:26
![](https://img.aspxhome.com/file/2023/7/70767_0s.png)
简单了解python 生成器 列表推导式 生成器表达式
2023-12-05 09:21:02
使用Python matplotlib作图时,设置横纵坐标轴数值以百分比(%)显示
2022-08-08 05:41:40
![](https://img.aspxhome.com/file/2023/3/133523_0s.jpg)