3D轮播-仿QQ音乐轮播修改

 1 <div class="box">
 2                     <div class="list">
 3                         <ul>
 4                             <li class="p5">
 5                                 <img src="images/5-1.png" alt="" />
 6                             </li>
 7                             <li class="p4">
 8                                 <img src="images/4-1.png" alt="" />
 9                             </li>
10                             <li class="p3">
11                                 <img src="images/3-1.png" alt="" />
12                             </li>
13                             <li class="p2">
14                                 <img src="images/2-1.png" alt="" />
15                             </li>
16                             <li class="p1">
17                                 <img src="images/1-1.png" alt="" />
18                             </li>
19                         </ul>
20                     </div>
21                     <a href="javascript:;" class="prev btn"><</a>
22                     <a href="javascript:;" class="next btn">></a>
23                 </div>
24             </div>

css代码

 1 .box {
 2   height: 5.55rem;
 3   width: 100%;
 4   position: relative;
 5 }
 6 .list {
 7   width: 100%;
 8   height: 5.55rem;
 9   overflow: hidden;
10   position: absolute;
11   left: 50%;
12   margin-left: -50%;
13 }
14 .list ul {
15   height: 5.55rem;
16 }
17 .list li {
18   width: 8.05rem;
19   height: 5.55rem;
20   position: absolute;
21   top: 0;
22   left: 0;
23   list-style: none;
24   transition: all 0.3s ease-out;
25 }
26 .list li img {
27   width: 8.05rem;
28   height: 5.55rem;
29   border: none;
30   float: left;
31 }
32 .p1 {
33   transform: translate3d(-117px, 0, 0) scale(0.81);
34 }
35 .p2 {
36   transform: translate3d(30px, 0, 0) scale(0.95);
37   transform-origin: 0 50%;
38   opacity: 0.8;
39   z-index: 2;
40 }
41 .p3 {
42   transform: translate3d(117px, 0, 0) scale(1);
43   z-index: 3;
44   opacity: 1;
45 }
46 .p4 {
47   transform: translate3d(190px, 0, 0) scale(0.95);
48   transform-origin: 100% 50%;
49   opacity: 0.8;
50   z-index: 2;
51 }
52 .p5 {
53   transform: translate3d(351px, 0, 0) scale(0.81);
54 }
55 .btn {
56   position: absolute;
57   top: 50%;
58   margin-top: -0.416666rem;
59   width:0.333333rem;
60   height:0.416666rem;
61   line-height: 0.416666rem;
62   font-size: 0.25rem;
63   color: white;
64   text-decoration: none;
65   text-align: center;
66   background: rgba(0, 0, 0, 0.5);
67   cursor: pointer;
68   z-index: 10;
69 }
70 .next {
71   right: 0;
72 }

js代码--需要先引入jq文件

  1 //根据各自的需要注释或者取消注释使用
  2 
  3 var $a=$(".buttons a");
  4 var $s=$(".buttons span");
  5 var cArr=["p5","p4","p3","p2","p1"];
  6 var index=0;
  7 $(".next").click(
  8     function(){
  9     nextimg();
 10     }
 11 )
 12 $(".prev").click(
 13     function(){
 14     previmg();
 15     }
 16 )
 17 //上一张
 18 function previmg(){
 19     cArr.unshift(cArr[4]);
 20     cArr.pop();
 21     //i是元素的索引,从0开始
 22     //e为当前处理的元素
 23     //each循环,当前处理的元素移除所有的class,然后添加数组索引i的class
 24     $("li").each(function(i,e){
 25         $(e).removeClass().addClass(cArr[i]);
 26     })
 27     index--;
 28     if (index<0) {
 29         index=4;
 30     }
 31     show();
 32 }
 33 
 34 //下一张
 35 function nextimg(){
 36     cArr.push(cArr[0]);
 37     cArr.shift();
 38     $("li").each(function(i,e){
 39         $(e).removeClass().addClass(cArr[i]);
 40     })
 41     index++;
 42     if (index>4) {
 43         index=0;
 44     }
 45     show();
 46 }
 47 
 48 //通过底下按钮点击切换
 49 $a.each(function(){
 50     $(this).click(function(){
 51         var myindex=$(this).index();
 52         var b=myindex-index;
 53         if(b==0){
 54             return;
 55         }
 56         else if(b>0) {
 57             /*
 58              * splice(0,b)的意思是从索引0开始,取出数量为b的数组
 59              * 因为每次点击之后数组都被改变了,所以当前显示的这个照片的索引才是0
 60              * 所以取出从索引0到b的数组,就是从原本的这个照片到需要点击的照片的数组
 61              * 这时候原本的数组也将这部分数组进行移除了
 62              * 再把移除的数组添加的原本的数组的后面
 63              */
 64             var newarr=cArr.splice(0,b);
 65             cArr=$.merge(cArr,newarr);
 66             $("li").each(function(i,e){
 67             $(e).removeClass().addClass(cArr[i]);
 68             })
 69             index=myindex;
 70             show();
 71         }
 72         else if(b<0){
 73             /*
 74              * 因为b<0,所以取数组的时候是倒序来取的,也就是说我们可以先把数组的顺序颠倒一下
 75              * 而b现在是负值,所以取出索引0到-b即为需要取出的数组
 76              * 也就是从原本的照片到需要点击的照片的数组
 77              * 然后将原本的数组跟取出的数组进行拼接
 78              * 再次倒序,使原本的倒序变为正序
 79              */
 80             cArr.reverse();
 81             var oldarr=cArr.splice(0,-b)
 82             cArr=$.merge(cArr,oldarr);
 83             cArr.reverse();
 84             $("li").each(function(i,e){
 85             $(e).removeClass().addClass(cArr[i]);
 86             })
 87             index=myindex;
 88             show();
 89         }
 90     })
 91 })
 92 
 93 //改变底下按钮的背景色
 94 function show(){
 95         $($s).eq(index).addClass("blue").parent().siblings().children().removeClass("blue");
 96 }
 97 
 98 //点击class为p2的元素触发上一张照片的函数
 99 //$(document).on("click",".p2",function(){
100 //    previmg();
101 //    return false;//返回一个false值,让a标签不跳转
102 //});
103 
104 //点击class为p4的元素触发下一张照片的函数
105 //$(document).on("click",".p4",function(){
106 //    nextimg();
107 //    return false;
108 //});
109 
110 //            鼠标移入box时清除定时器
111 $(".box").mouseover(function(){
112     clearInterval(timer);
113 })
114 
115 //            鼠标移出box时开始定时器
116 $(".box").mouseleave(function(){
117     timer=setInterval(nextimg,4000);
118 })
119 
120 //            进入页面自动开始定时器
121 timer=setInterval(nextimg,4000);

效果图

 

posted on 2017-11-20 12:06  漫步潜行者  阅读(838)  评论(4)    收藏  举报

导航