1 Star 0 Fork 55

陈_鹏飞/frontend-javascript-particular

加入 Gitee
与超过 1200万 开发者一起发现、参与优秀开源项目,私有仓库也完全免费 :)
免费加入
文件
该仓库未声明开源许可证文件(LICENSE),使用请关注具体项目描述及其代码上游依赖。
克隆/下载
animate.js 2.16 KB
一键复制 编辑 原始数据 按行查看 历史
bangbangji 提交于 2021-08-04 15:05 +08:00 . init
if (!this.myPlugin) {
this.myPlugin = {};
}
/**
* 动画
* @param {object} option 配置对象
*/
this.myPlugin.Animate = function (option) {
//默认配置
var defaultOption = {
duration: 16, //默认间隔时间,单位毫秒
total: 1000, //默认总时间
begin: {}, //初始值
end: {} //终止值
};
this.option = myPlugin.mixin(defaultOption, option);
this.timer = null; //计时器的id
//运动总次数
this.number = Math.ceil(this.option.total / this.option.duration);
//当前运动次数
this.curNumber = 0;
//当前状态
this.curData = myPlugin.clone(this.option.begin);
//所有属性运动的总距离
this.distance = {};
//所有属性每次运动的距离
this.everyDistance = {};
for (var prop in this.option.begin) {
this.distance[prop] = this.option.end[prop] - this.option.begin[prop];
this.everyDistance[prop] = this.distance[prop] / this.number;
}
}
/**
* 开始动画
*/
this.myPlugin.Animate.prototype.start = function () {
if (this.timer || this.curNumber === this.number) {
return; //如果之前已经存在计时器,则不做任何处理
}
if (this.option.onstart) {
this.option.onstart.call(that);
}
var that = this;
this.timer = setInterval(function () {
//改变that.curData
that.curNumber++;//当前运动次数+1
for (var prop in that.curData) {
if (that.curNumber === that.number) {
//最后一次运动
that.curData[prop] = that.option.end[prop];
}
else {
that.curData[prop] += that.everyDistance[prop];
}
}
if (that.option.onmove) {
that.option.onmove.call(that);
}
if (that.curNumber === that.number) {
//等于了总次数
that.stop();
if (that.option.onover) {
that.option.onover.call(that);
}
}
}, this.option.duration);
}
/**
* 停止动画
*/
this.myPlugin.Animate.prototype.stop = function () {
clearInterval(this.timer);
this.timer = null;
}
Loading...
马建仓 AI 助手
尝试更多
代码解读
代码找茬
代码优化
1
https://gitee.com/CHEN____PENGFEI/frontend-javascript-particular.git
git@gitee.com:CHEN____PENGFEI/frontend-javascript-particular.git
CHEN____PENGFEI
frontend-javascript-particular
frontend-javascript-particular
master

搜索帮助