本文实例讲述了JS实现图片旋转动画效果封装与使用。分享给大家供大家参考,具体如下:

核心封装代码如下:
//图片动画封装
function SearchAnim(opts) {
for(var i in SearchAnim.DEFAULTS) {
if (opts[i] === undefined) {
opts[i] = SearchAnim.DEFAULTS[i];
}
}
this.opts = opts;
this.timer = null;
this.elem = document.getElementById(opts.elemId);
this.startAnim();
}
SearchAnim.prototype.startAnim = function () {
this.stopAnim();
this.timer = setInterval(() => {
var startIndex = this.opts.startIndex;
if (startIndex == 360) {
this.opts.startIndex = 0;
}
this.elem.style.transform = "rotate("+ (startIndex) +"deg)";
this.opts.startIndex += 5;
}, this.opts.delay);
setTimeout(() => {
this.stopAnim();
}, this.opts.duration);
}
SearchAnim.prototype.stopAnim = function() {
if (this.timer != null) {
clearInterval(this.timer);
}
}
SearchAnim.DEFAULTS = {
duration : 60000,
delay : 200,
direction : true,
startIndex : 0,
endIndex : 360
}

售后响应及时
7×24小时客服热线
数据备份
更安全、更高效、更稳定
价格公道精准
项目经理精准报价不弄虚作假
合作无风险
重合同讲信誉,无效全额退款