JS小外掛程式之2——cycle元素輪播

來源:互聯網
上載者:User

JS小外掛程式之2——cycle元素輪播
1 <!DOCTYPE html>  2 <html>  3 <head>  4 <meta http-equiv="Content-Type" Content="text/html; charset=utf-8;">  5 <title> cycle demo </title>  6 <meta name="author" content="rainna" />  7 <meta name="keywords" content="rainna's js lib" />  8 <meta name="description" content="cycle demo" />  9 </head> 10 <style> 11 *{margin:0;padding:0;} 12 li{list-style:none;} 13  14 .cycleBox{position:relative;width:600px;height:450px;margin:100px auto;} 15 .cycleBox .elemsWrap li,.cycleBox .elemsWrap img{position:absolute;left:0;top:0;width:100%;height:100%;} 16 .cycleBox .iconsWrap{position:absolute;right:15px;bottom:15px;} 17 .cycleBox .iconsWrap li{display:inline-block;margin:0 0 0 10px;cursor:pointer;color:#fff;} 18  19 .cycleBox .elemsWrap li{display:none;} 20 </style> 21 <body> 22 <div class="cycleBox" id="cycleWrap"> 23     <ul class="elemsWrap" id="elemsWrap"> 24         <li> 25             <div class="pic"><img src=http://www.bkjia.com/uploads/allimg/140803/043Q51V0-0.jpg"></div> 26             <div class="txt">小兩口的95平米簡約婚房</div> 27         </li> 28         <li> 29             <div class="pic"><img src=http://www.bkjia.com/uploads/allimg/140803/043Q51K4-1.jpg"></div> 30             <div class="txt">本案是一套建築面積95平米的兩室小戶型。</div> 31         </li> 32         <li> 33             <div class="pic"><img src=http://www.bkjia.com/uploads/allimg/140803/043Q512N-2.jpg"></div> 34             <div class="txt">在色彩的搭配上以低飽和度的淺咖色為主搭配少量的黑色與白色來增加層次,最後配以高飽和度的裝飾畫作為跳色。</div> 35         </li> 36         <li> 37             <div class="pic"><img src=http://www.bkjia.com/uploads/allimg/140803/043Q5Mc-3.jpg"></div> 38             <div class="txt">加強空間的儲納功能以及生活功能分區鬼鬼在靠近客廳視窗的地方設計了一個儲納地台以及整體衣櫃。</div> 39         </li> 40         <li> 41             <div class="pic"><img src=http://www.bkjia.com/uploads/allimg/140803/043Q52J9-4.jpg"></div> 42             <div class="txt">小兩口的95平米簡約婚房</div> 43         </li> 44     </ul> 45     <ul class="iconsWrap" id="iconsWrap"></ul> 46 </div> 47  48 <script> 49 var cycle = function(){ 50     //公用函數 51     function T$(id){ 52         return document.getElementById(id); 53     } 54     function T$$(root,tag){ 55         return (root||document).getElementsByTagName(tag); 56     } 57     function $extend(object1,object2){ 58         for(var p in object2){ 59             object1[p] = object2[p]; 60         } 61         return object1; 62     } 63      64     //預設參數 65     var defaultOptions = { 66         index:0,           //第一個顯示元素的索引,預設為0,顯示第一個元素 67         time: 1000      //切換時間      68     }; 69      70     //主類函數   cid為輪播總容器ID,eid為元素容器ID,iid為icons容器ID,options為重寫的預設參數對象 71     var cycleShow = function(cid,eid,iid,options){ 72         var self = this; 73         if(!(self instanceof cycleShow)){ 74             return new cycleShow(eid,iid,options); 75         }         76         self.container = T$(cid);  77         self.elemsContainer = T$(eid);      78         self.iconsContainer = T$(iid); 79         self.options = $extend(defaultOptions,options||{}); 80         self.elems = T$$(self.elemsContainer,'li'); 81     }; 82     cycleShow.prototype = { 83         constructor: cycleShow, 84         moveTo: function(currIndex){ 85             var self = this; 86             currIndex = currIndex%self.elems.length; 87             if(!self.currIndex) self.currIndex = 0; 88             self.elems[self.currIndex].style.display = 'none'; 89             self.icons[self.currIndex].style.color = '#fff'; 90             self.elems[currIndex].style.display = 'block';     91             self.icons[currIndex].style.color = '#f00'; 92             self.currIndex = currIndex; 93         }, 94         run: function(currIndex){ 95             var self = this; 96             if(!!self.timer) clearTimeout(self.timer); 97             self.timer = setTimeout(function(){ 98                 self.run(currIndex); 99             },self.options.time);100 101             self.moveTo(currIndex++);102         },103         stop:function(){104             var self = this;105             if(!!self.timer) clearTimeout(self.timer);106         },107         init:function(){108             var self = this,109                 frag = document.createDocumentFragment();110             for(var i=0,l=self.elems.length;i<l;i++){111                 var node = document.createElement('li');112                 node.innerHTML = i+1;113                 frag.appendChild(node);114             }115             self.iconsContainer.appendChild(frag);116             117             self.icons = T$$(self.iconsContainer,'li');118             for(var i=0,l=self.icons.length;i<l;i++){119                 (function(i){120                     self.icons[i].onclick = function(){121                         self.stop();122                         self.moveTo(i);123                     };124                 }(i));125             }126             127             self.container.onmouseover = function(){            128                 self.stop();129             }130             self.container.onmouseout = function(){131                 self.run(self.currIndex);132             }133             134             self.run(self.options.index);135         }136     };137     138     return {139         onShowCycle:function(cid,eid,iid,options){140             var st = new cycleShow(cid,eid,iid,options);141             st.init();142         }143     }144     145 }();146 147 cycle.onShowCycle('cycleWrap','elemsWrap','iconsWrap');148 </script>149 </body>150 </html>

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在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.