使用模組化管理工具seajs實現簡單動畫效果

來源:互聯網
上載者:User

標籤:art   數組   cmd   簡單   ceil   oct   css   使用   定義函數   

今天使用模組化的管理工具seajs實現了一個簡單的動畫效果。

seajs具有簡單友好的模組定義規範。seajs遵循CMD規範,可以像nodejs一樣編寫代碼。

seajs具有自然直觀的程式碼群組織方式。依賴的自動載入,配置簡潔清晰。

通過學習,發現seajs的使用是具有一個標準的格式的,如下define(function(require,exports,module){ //定義的代碼塊 });其中回調的參數名和順序都是不可改變的。

這次小動畫實現的檔案結構:

word.html代碼如下:

 1 <!DOCTYPE html> 2 <html lang="en"> 3 <head> 4     <meta charset="UTF-8"> 5     <title>Document</title> 6     <style> 7         img{ 8             opacity: 0.5; 9             transition-duration: 0.8s;10         }11     </style>12     <script src=‘./sea.js‘></script>13     <script>14         //使用定義好的js模組15         seajs.use(‘./game/contral.js‘,function(obj){});16     </script>17 </head>18 <body>19     <div id="container">20         <img src="./img/h.png" alt="">21         <img src="./img/h.png" alt="">22         <img src="./img/h.png" alt="">23         <img src="./img/h.png" alt="">24         <img src="./img/h.png" alt="">25         <img src="./img/h.png" alt="">26     </div>27 </body>28 </html>

rotate.js代碼如下:

 1 define(function(require,exports,module){ 2     //引入jq,在這裡需要將jq做稍微的改動, 3     //將jq的代碼也用seajs文法模組化 4     var $=require(‘../jquery.js‘); 5     //定義函數,通過傳入容器id使所有的容器下的圖片運動起啦 6     var RotateWord = function(id){ 7         this.container = $(id); 8         this.icons = this.container.children(); //所有的圖片 9         //定義一個方法的數組用來存放方法10         this.arr = [];11     }12 13     module.exports =RotateWord;14     //定義一個隨機數15     var random = function(num){16         return Math.random()*num;17     }18 19     //給RotateWord添加方法20     RotateWord.prototype={21         //初始化圖片的方法22         _init:function(){23             var arr = this.arr;24             $(this.icons).each(function(index,item){25                 var obj = $(item);  //將普通對象轉化為jq對象26                 var time;27                 obj.hover(function(){28                     obj.css(‘opacity‘,1).css(‘transform‘,‘rotate(0deg)‘);29 30                 },31                 function(){32                     obj.css(‘opacity‘,0.5);33                     time && clearTimeout(time);34                     time=setTimeout(rotateFn,Math.ceil(random(5000)));35 36                 });37                 //定義一個旋轉的方法38                 function rotateFn(){39                    obj.css(‘transform‘,‘rotate(‘+random(360)+‘deg)‘);40                 }41                 //將每一個元素的方法儲存起來42                 arr[index]=rotateFn;43             });44         },45         _rotate:function(){46             //讓元素在預設的情況下有一定的旋轉47             $(this.arr).each(function(n,fn){48                 setTimeout(fn,Math.ceil(random(3000)));49             });50         },51         render:function(){52             //開啟動畫效果53             this._init();54             this._rotate();55         }56     }57 })

control.js(用於管理各個分模組,這個動畫效果案例只有一個分模組頁面)代碼如下:

1 define(function(require,exports,module){2     //匯入定義好的模組3     var RotateWord =require(‘./rotate.js‘);4     //建立對象,調用方法,實現動畫效果5     var obj = new RotateWord(‘#container‘);6     obj.render();7 })

 

使用模組化管理工具seajs實現簡單動畫效果

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.