JS實現(xiàn)圖片旋轉動畫效果封裝與使用示例
本文實例講述了JS實現(xiàn)圖片旋轉動畫效果封裝與使用。分享給大家供大家參考,具體如下:
核心封裝代碼如下:
//圖片動畫封裝 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 }
使用方法:
隨便創(chuàng)建一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.dbjr.com.cn JS旋轉動畫</title> </head> <img src="http://img.jbzj.com/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數(shù)據結構與算法技巧總結》及《JavaScript數(shù)學運算用法總結》
希望本文所述對大家JavaScript程序設計有所幫助。
相關文章
js動態(tài)添加表格逐行添加、刪除、遍歷取值的實例代碼
最近做項目遇到這樣的需求,要求表格添加一行,表格刪除一行,表格遍歷取值等。下面小編給大家?guī)砹薺s動態(tài)添加表格逐行添加、刪除、遍歷取值的實例代碼,需要的朋友參考下2018-01-01在JavaScript中對字符串進行索引、拆分和操作的示例代碼
字符串是一個包含一個或多個字符的序列,可以由字母、數(shù)字或符號組成,在本教程中,我們將學習字符串原始值和String對象之間的區(qū)別,字符串的索引方式,如何訪問字符串中的字符,以及字符串常用的屬性和方法,需要的朋友可以參考下2024-06-06跟我學Node.js(四)---Node.js的模塊載入方式與機制
Node.js中模塊可以通過文件路徑或名字獲取模塊的引用。模塊的引用會映射到一個js文件路徑,除非它是一個Node內置模塊。Node的內置模塊公開了一些常用的API給開發(fā)者,并且它們在Node進程開始的時候就預加載了。2014-06-06Highslide.js是一款基于js實現(xiàn)的網頁中圖片展示插件
這篇文章主要介紹了Highslide.js是一款基于js實現(xiàn)的網頁中圖片預覽查看工具,需要的朋友可以參考下2007-05-05防止動態(tài)加載JavaScript引起的內存泄漏問題
利用Script標簽可以跨域加載并運行一段JavaScript腳本, 但Neil Fraser先前已指出,腳本運行后資源并沒被釋放,即使是Script標簽移除后。2009-10-10