JS实现图片旋转动画效果封装与使用示例
程序员文章站
2023-11-09 21:55:04
本文实例讲述了js实现图片旋转动画效果封装与使用。分享给大家供大家参考,具体如下:
核心封装代码如下:
//图片动画封装
function searchani...
本文实例讲述了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 xmlns="http://www.w3.org/1999/xhtml"> <head> <meta http-equiv="content-type" content="text/html; charset=utf-8" /> <title>www.jb51.net js旋转动画</title> </head> <img src="https://files.jb51.net/file_images/article/201807/201879100307926.jpg" id="wait-icon"/> <script> //图片动画封装 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, }); </script> <body> </body> </html>
使用本站html/css/js在线运行测试工具:http://tools.jb51.net/code/htmljsrun,可得到如下测试运行效果:
更多关于javascript相关内容感兴趣的读者可查看本站专题:《javascript动画特效与技巧汇总》、《javascript页面元素操作技巧总结》、《javascript运动效果与技巧汇总》、《javascript图形绘制技巧总结》、《javascript切换特效与技巧总结》、《javascript错误与调试技巧总结》、《javascript数据结构与算法技巧总结》及《javascript数学运算用法总结》
希望本文所述对大家javascript程序设计有所帮助。