1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171
| <div id="pxs_container" class="pxs_container"> <div class="pxs_bg"> <div class="pxs_bg1"></div> <div class="pxs_bg2"></div> <div class="pxs_bg3"></div> </div> <div class="pxs_slider_wrapper"> <ul class="pxs_slider"> <li><img src="images/ParallaxScrolling/big1.jpg" alt="First Image" /></li> <li><img src="images/ParallaxScrolling/big2.jpg" alt="Second Image" /></li> <li><img src="images/ParallaxScrolling/big3.jpg" alt="Third Image" /></li> <li><img src="images/ParallaxScrolling/big4.jpg" alt="Forth Image" /></li> <li><img src="images/ParallaxScrolling/big5.jpg" alt="Fifth Image" /></li> <li><img src="images/ParallaxScrolling/big6.jpg" alt="Sixth Image" /></li> </ul> <div class="pxs_navigation"> <span class="pxs_next"></span> <span class="pxs_prev"></span> </div>
<ul class="pxs_thumbnails"> <li><img src="images/ParallaxScrolling/sm1.jpg" alt="First Image" /></li> <li><img src="images/ParallaxScrolling/sm2.jpg" alt="Second Image" /></li> <li><img src="images/ParallaxScrolling/sm3.jpg" alt="Third Image" /></li> <li><img src="images/ParallaxScrolling/sm4.jpg" alt="Forth Image" /></li> <li><img src="images/ParallaxScrolling/sm5.jpg" alt="Fifth Image" /></li> <li><img src="images/ParallaxScrolling/sm6.jpg" alt="Sixth Image" /></li> </ul> </div> </div>
/* 视差滚动 */
function oParallaxScrolling(){ var oPS=document.getElementById("pxs_container"); var oPSAll=getByClass(oPS,"pxs_slider_wrapper")[0];
//背景 var bg1=getByClass(oPS,"pxs_bg1")[0]; var bg2=getByClass(oPS,"pxs_bg2")[0]; var bg3=getByClass(oPS,"pxs_bg3")[0];
//大图片 var bigP=getByClass(oPS,"pxs_slider")[0]; var bigPLi=bigP.getElementsByTagName("li"); var bigPImg=bigP.getElementsByTagName("img");
//小图片 var smallP=getByClass(oPS,"pxs_thumbnails")[0]; var smallPLi=smallP.getElementsByTagName("li"); var smallPImg=smallP.getElementsByTagName("img");
//左右按钮 var pre=getByClass(oPS,"pxs_prev")[0]; var nex=getByClass(oPS,"pxs_next")[0];
//获取浏览器窗口的宽高 var ww=window.innerWidth || document.documentElement.clientWidth || document.body.clientWidth; var wh=window.innerHeight || document.documentElement.clientHeight || document.body.clientHeight;
var L=bigPLi.length; //图片的个数 setStyle(bigP, 'width', L*ww + 'px');
//将窗口设置为可见的 oPSAll.style.display='block'; //浏览器的尺寸小于768,隐藏该模块 /* if(ww<768){ oPS.style.display='none'; }*/
//初始化大图位置 for(var i=0; i<L; i++){ setStyle(bigPLi[i], 'width', ww + 'px'); setStyle(bigPImg[i], 'left', ww/2 - bigPImg[i].offsetWidth/2 + 'px'); }
//初始化左右按钮位置 setStyle(pre, 'left', ww/2 - bigPImg[0].offsetWidth/2 - 10 + 'px'); setStyle(nex, 'left', ww/2 - bigPImg[0].offsetWidth/2 + bigPImg[0].offsetWidth - 20 + 'px');
//初始化小图位置 setStyle(smallP,'width',bigPImg[0].offsetWidth + 'px'); setStyle(smallP,'left', ww/2 - bigPImg[0].offsetWidth/2 + 'px');
var angle ; for(var i=0; i<L; i++){ setStyle(smallPLi[i], 'left', smallPLi[i].offsetWidth * i + (bigPImg[0].offsetWidth - L*smallPLi[0].offsetWidth -80)/2 + 80/L*i + 'px'); angle = Math.random()*60 - 30; setStyle(smallPImg[i],'transform','rotate(' + angle + 'deg)'); }
//效果 var now = 0; //记录照片的播放次序 //大图、小图、背景在切换时的变化行为 function imageTab(){ /* 速度:大图 > 背景3 > 背景2 > 背景1,实现视差滚动 背景图的运动,通过控制目标值来控制速度。 注意,这里一定要加Math.floor(),即目标值为整数,这样才不会出现抖动。 */ startmove(bigP , {left: -now*ww} ); startmove(bg3 , {left: -now * Math.floor(bg3.offsetWidth/8) } ); startmove(bg2 , {left: -now * Math.floor(bg2.offsetWidth/10)} ); startmove(bg1 , {left: -now * Math.floor(bg1.offsetWidth/12) } ); //小图随大图走 for(var j=0; j<L ;j++){ startmove( smallPLi[j] , {opacity:70 , marginTop:0} ); } startmove( smallPLi[now] , {opacity:100 , marginTop:-20} ); };
for(var i=0; i<L; i++){ smallPLi[i].index = i;
//小图移入、移除、点击效果 smallPLi[i].onmouseover = function(){ startmove( smallPLi[this.index] , {opacity:100 , marginTop:-20} ); }; smallPLi[i].onmouseout = function(){ startmove( smallPLi[this.index] , {opacity:70 , marginTop:0} ); }; smallPLi[i].onclick = function(){ now = this.index; imageTab(); }; //左按钮 pre.onclick = function(){ now--; if(now == -1){ now = 0; //防止左边出界 return; } imageTab(); }; //右按钮 nex.onclick = function(){ now++; if(now == 6){ now = 5; //防止右边出界 return; } imageTab(); }; }
//自动播放 function autoPlay(){ if(now == 5){ now = -1; //自动播放时,右边最后一个运动完切换到第一个 } now++; imageTab(); }; var timer = setInterval( autoPlay , 5000); oPS.onmouseover = function(){ clearInterval(timer); }; oPS.onmouseout = function (){ timer = setInterval( autoPlay , 5000); };
};
oParallaxScrolling();
|