本文实例讲述了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
}
使用方法:
随便创建一img标签
然后如下调用即可:
new SearchAnim({
elemId : "wait-icon",
delay : 20,
});
完整示例代码:
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html ns="http://www.w3.org/1999/xhtml">
<head>
< http-equiv="Content-Type" content="text/html; charset=utf-8" />
< >www.jb51.net JS旋转动画</ >
</head>
<img src="https://files.jb51.net/file_images/article/201807/201879100307926.jpg" id="wait-icon"/>
< >
//图片动画封装
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
}
new SearchAnim({
elemId : "wait-icon",
delay : 20,
});
</ >
<body>
</body>
</html>
使用本站HTML/CSS/JS在线运行测试工具:http://tools.jb51.net/code/HtmlJsRun,可得到如下测试运行效果:

更多关于 相关内容感兴趣的读者可查看本站专题:《 动画特效与技巧汇总》、《 页面元素操作技巧总结》、《 运动效果与技巧汇总》、《 图形绘制技巧总结》、《 切换特效与技巧总结》、《 错误与调试技巧总结》、《 数据结构与算法技巧总结》及《 数学运算用法总结》
希望本文所述对大家 程序设计有所帮助。
继续阅读与本文标签相同的文章
-
全网“传奇手游”相关搜索排名TOP100 全网“传奇手游”相关搜索排行榜
2026-05-15栏目: 教程
-
又一国产巨头崛起!19天狂建1367个5G基站,日韩始料不及
2026-05-15栏目: 教程
-
恩平“两线合一”改革试点全面完成家计入户调查,建立数据库
2026-05-15栏目: 教程
-
雷军微博抽奖送的蔚来ES6 10个月后终于兑现
2026-05-15栏目: 教程
-
丰巢回应用照片刷脸取件:已第一时间下线
2026-05-15栏目: 教程
