關於焦點輪播圖的總結


  目前來講,在各大網站都會使用到焦點輪播圖,因為它占用地方少,交互性好,是前端設計必須要掌握的技能之一。

  原理:使用三層嵌套,最里層裝載圖片,圖片需要浮動。最里層設置相對定位。然后再使用JavaScript設置一個定時器,每過一段時間便讓最里層的塊的left值改變。

而第二層則需要設置overflow:hidden;這個屬性,否則將會導致這個層被子層撐大。不美觀。

  

此圖便為實現效果圖。

  下面先講一講如何布局。

  

  首先布局分為三大塊,一塊為inner,包裹住所有的圖片;一塊為outer,決定展示的窗口;兩個a標簽為左右箭頭;pagination塊則包裹着下面的每一小塊span標簽;

wrap是最外面的塊,控制着整個輪播圖的位置。

  然后,設置相關的樣式。如圖:

 1 html,body,div,img,a,span{
 2     margin:0;
 3     padding:0;
 4 }
 5 a{
 6     text-decoration: none;
 7     color:black;
 8 }
 9 .wrap{
10     width:510px;
11     margin:20px auto;
12 }
13 #outer{
14     width:510px;
15     overflow: hidden;
16     position: relative;
17     left:0;
18     top:0;
19 }
20 #inner{
21     width:9999px;
22     position: relative;
23     left:0;
24     top:0;
25     overflow: hidden;
26 }
27 #inner img{
28     float:left;
29 }
30 #outer a{
31     position: absolute;
32     width:30px;
33     height:30px;
34     font-size: 30px;
35     text-align: center;
36     line-height: 30px;
37     top:50%;
38     margin-top: -15px;
39     background-color: #ccc;
40     opacity: 0;
41     filter: alpha(opacity=0);
42 }
43 #outer .active{
44     background-color: #9f9f9f;
45 }
46 #outer:hover a{
47     opacity: 0.6;
48     filter: alpha(opacity=60);
49 }
50 #outer .leftGo{
51     left:0;
52 }
53 #outer .rightGo{
54     right:0;
55 }
56 #pagination{
57     position: absolute;
58     bottom: 20px;
59     width:100%;
60     text-align: center;
61 }
62 #pagination span{
63     display: inline-block;
64     width:30px;
65     height:30px;
66     line-height: 30px;
67     border-radius: 50%;
68     background-color: #fbfbfb;
69     opacity: 0.6;
70     filter: alpha(opacity=60);
71     cursor: pointer;
72     -webkit-user-select: none;
73 }

 

 

 

  至此,相關的設置就完成了。

  但是在此過程中有一些問題需要注意:

  1.計時器在每次調用之前必須清除,否則當有多個事件觸發的時候計時器會疊加,從而會越走越快。

  2.一些重復的代碼沒必要重復寫,應該要封裝到函數里面去。

  3.當對多個相同的元素進行操作時,注意不要讓數組越界。 

  4.要考慮兼容性問題

  5.要注意代碼的格式化。

  

 1 var inner = document.getElementById("inner");
 2     var imgArr =inner.getElementsByTagName("img");
 3     var spanArr = document.getElementById("pagination").getElementsByTagName("span");
 4     var leftGo = document.getElementById("outer").getElementsByTagName("a")[0];
 5     var rightGo = document.getElementById("outer").getElementsByTagName("a")[1];
 6     var picWidth = imgArr[0].offsetWidth;//獲取第一站圖片的寬度
 7     var index = 0;
 8     var timer = null;
 9     var AutoTimer = null;
10 timer = setInterval(AutoGo,2000);//設置計時器
11     leftGo.onclick=function(){
12         Goleft();//點擊左邊的小箭頭
13     };
14     rightGo.onclick = function(){
15         Goright();//點擊右邊的小箭頭
16     };
17     inner.onmouseover=function(){
18         clearInterval(timer);//鼠標移入,清除計時器
19     }
20     inner.onmouseleave=function(){
21     timer = setInterval(AutoGo,2000);//鼠標移出,啟動計時器
22     }
23     function AutoGo(){
24         //自動輪播
25         var start =inner.offsetLeft;//距離左邊的邊框的長度
26         var end = - index * picWidth;//終點
27         var moveDistance = end - start;
28         var speed = 20;//要走的步數
29         var speedCount = 0;
30         clearInterval(AutoTimer);
31             //清除之前的計時器,否則會越走越快
32         clearInterval(timer);
33         AutoTimer = setInterval(function(){
34             speedCount++;
35             if(speedCount >=speed){
36                             //步數足夠
37                 clearInterval(AutoTimer);
38                 clearInterval(timer);
39                 timer = setInterval(Goright,1000);
40                 //再次啟動計時器
41             }
42             inner.style.left = moveDistance *     speedCount/speed +start+"px";
43 //每步要走的距離
44         },100)
45         for(var i = 0 ; i<spanArr.length;i++){
46                     //下標的樣式改變,以及點擊事件的綁定
47             spanArr[i].index = i;
48             spanArr[i].className="";
49             spanArr[index].className="active";
50             spanArr[i].onclick =function(){
51                 index=this.index;//傳遞當前點擊的位置
52                 AutoGo();
53             }
54         }
55     }
56     function Goleft(){
57         //往左走一步;
58         index--;
59         if(index<0){
60             index =imgArr.length-1;
61         }
62         AutoGo();
63         
64     }
65     
66     function    Goright(){
67         //往右走一步
68         index++;
69         if(index>imgArr.length-1){
70             index =0;
71         }
72         AutoGo();
73     }

 

 如若轉載,請說明出處,謝謝


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM