靈活使用數(shù)組制作圖片切換js實(shí)現(xiàn)
js活用數(shù)組制作圖片切換效果,供大家參考,具體內(nèi)容如下
數(shù)組元素位置變換:
將內(nèi)容分割為數(shù)組,將第一個加到最后,刪掉第一個
<div id="box">1,2,3,4</div>
<input type="button" value='切換' id='input'>
<script>
window.onload=function(){
var oDiv=document.getElementById('box');
var oInput=document.getElementById('input');
oInput.onclick=function(){
var arr=oDiv.innerHTML.split(',');
// console.log(arr);
arr.push(arr[0]);//將第一個加到最后,刪掉第一個
arr.shift();
oDiv.innerHTML=arr;
}
}
</script>
模擬圖片切換效果:
window.onload=function(){
var aDiv=document.getElementsByTagName('div');
var aInput=document.getElementsByTagName('input');
var arr=[];//創(chuàng)建空數(shù)組用于存放屬性
for(var i=0;i<aDiv.length;i++){
console.dir(getStyle(aDiv[i],'left'));//獲取到純凈的最終樣式
//將屬性作為 符合數(shù)組 加入arr中,可用于多屬性
arr.push([getStyle(aDiv[i],'left'),getStyle(aDiv[i],'top')]);
}
// console.dir(arr);
aInput[0].onclick=function(){//將第一個加到最后,刪掉第一個
arr.push(arr[0]);
arr.shift();
for(var i=0;i<aDiv.length;i++){//操作完數(shù)組后重新賦值
aDiv[i].style.left=arr[i][0];
aDiv[i].style.top=arr[i][1];
}
};
aInput[1].onclick=function(){//將最后一個加到最前,刪最后
arr.unshift(arr[arr.length-1]);
arr.pop();
for(var i=0;i<aDiv.length;i++){
aDiv[i].style.left=arr[i][0];
aDiv[i].style.top=arr[i][1];
}
};
function getStyle(obj,attr){//獲取最終樣式
if(obj.currentStyle){
return obj.currentStyle[attr];
}else{
return getComputedStyle(obj,false)[attr];
}
}
}
簡陋效果圖:

實(shí)例版:
思路:
若有五張圖片:圖1~5的left值分別為:20px、60px、100px、240px、380px;
點(diǎn)擊左切換按鈕后,對應(yīng)的圖1~5left值變?yōu)椋?0px、100px、240px、380px、20px;
--------------------------------------------------------------------------------
相當(dāng)于這組數(shù)組第一個元素移到最后:20px、60px、100px、240px、380px、20px;
然后再把第一個元素刪除得:60px、100px、240px、380px、20px;
以此類推:
實(shí)例布局:
<div id="box"> <ul> <li class='pos_0'><img src="images/1.png" width='300'></li> <li class='pos_1'><img src="images/1.jpg" width='400'></li> <li class='pos_2'><img src="images/2.jpg" width='500'></li> <li class='pos_3'><img src="images/3.jpg" width='400'></li> <li class='pos_4'><img src="images/1.jpg" width='300'></li> </ul> <span class='dir dirl'></span> <span class='dir dirr'></span> </div>
實(shí)例樣式:
#box{width:700px;height:300px;position:relative;margin:20px auto;text-align: center;}
#box ul{list-style: none;}
#box ul li{position:absolute;}
#box ul li.pos_0{top:50px;left:20px;z-index:1;opacity:0.5;}
#box ul li.pos_1{top:20px;left:60px;z-index:2;opacity:0.8;}
#box ul li.pos_2{top:0px;left:100px;z-index:3;opacity:1;}
#box ul li.pos_3{top:20px;left:240px;z-index:2;opacity:0.8;}
#box ul li.pos_4{top:50px;left:380px;z-index:1;opacity:0.5;}
.dir{display: inline-block;width:45px;height:100px;background:url('images/button.png') no-repeat;
position:absolute;top:60px;z-index:4;}
.dirl{background-position: 0px 0;left:40px;}
.dirr{background-position: -55px 0;right:40px;}
JS代碼:
window.onload=function(){
var oPre=document.getElementsByClassName('dir')[0];
var oNext=document.getElementsByClassName('dir')[1];
var aLi=document.getElementsByTagName('li');
var arr=[];
for(var i=0;i<aLi.length;i++){
var oImg=aLi[i].getElementsByTagName('img')[0];
// console.log(getStyle(aLi[i],'left'));
// console.log(parseInt(getStyle(aLi[i],'opacity')*100));
// console.log(getStyle(aLi[i],'z-index'));
// console.log(oImg.width);
arr.push([
parseInt(getStyle(aLi[i],'top')),
parseInt(getStyle(aLi[i],'opacity')*100),
parseInt(getStyle(aLi[i],'z-index')),
oImg.width
]);
// console.log(arr[i][2]);
}
// console.dir(arr);
oPre.onclick=function(){//左
arr.push(arr[0]);
arr.shift();
for(var i=0;i<aLi.length;i++){
var oImg=aLi[i].getElementsByTagName('img')[0];
//console.log(arr[i][2]);
startMove(aLi[i],{
left:arr[i][0],
top:arr[i][1],
opacity:arr[i][2],
});
aLi[i].style.zIndex=arr[i][3];
startMove(oImg,{width:arr[i][4]});
}
};
oNext.onclick=function(){//右
arr.unshift(arr[arr.length-1]);
arr.pop();
for(var i=0;i<aLi.length;i++){
var oImg=aLi[i].getElementsByTagName('img')[0];
startMove(aLi[i],{
left:arr[i][0],
top:arr[i][1],
opacity:arr[i][2],
});
aLi[i].style.zIndex=arr[i][3];
startMove(oImg,{width:arr[i][4]});
}
};
function getStyle(obj,attr){//得到是帶單位的數(shù)值
if(obj.currentStyle){
return obj.currentStyle[attr];
}else{
return getComputedStyle(obj,false)[attr];
}
}
}
效果圖:

以上就是本文的全部內(nèi)容,希望對大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。
相關(guān)文章
JavaScript實(shí)現(xiàn)動態(tài)留言板
這篇文章主要為大家詳細(xì)介紹了JavaScript實(shí)現(xiàn)動態(tài)留言板,文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2020-03-03
javascript實(shí)現(xiàn)倒計(jì)時(shí)N秒后網(wǎng)頁自動跳轉(zhuǎn)代碼
這篇文章主要介紹了javascript實(shí)現(xiàn)倒計(jì)時(shí)N秒后網(wǎng)頁自動跳轉(zhuǎn)代碼,非常的實(shí)用,這里推薦給大家。2014-12-12
javascript實(shí)現(xiàn)日期按月份加減
JavaScript實(shí)現(xiàn)日期加減計(jì)算功能代碼實(shí)例,因?yàn)樵趈s中沒有類似C#中的AddDays方法,所以要想實(shí)現(xiàn)日期加減的話,就需要自己寫函數(shù)來實(shí)現(xiàn)。這里分享給大家,有需要的小伙伴可以參考下2015-05-05
JavaScript與ActionScript3兩者的同性與差異性
接觸JavaScript和ActionScript3也有近5年的時(shí)間了,它們都是應(yīng)用比較廣泛的腳本語言.接下來通過本文給大家介紹JavaScript與ActionScript3兩者的同性與差異性,感興趣的朋友一起學(xué)習(xí)吧2016-09-09
JavaScript調(diào)用客戶端的可執(zhí)行文件(示例代碼)
這篇文章主要是對JavaScript調(diào)用客戶端的可執(zhí)行文件(示例代碼)進(jìn)行了介紹,需要的朋友可以過來參考下,希望對大家有所幫助2013-11-11
Bootstrap中的Dropdown下拉菜單更改為懸停(hover)觸發(fā)
在使用bootstrap制作響應(yīng)式導(dǎo)航條時(shí),dropdown組件用的比較多,dropdown默認(rèn)鼠標(biāo)左鍵單擊才展開,如果使用鼠標(biāo)放上去(hover)就展開則會省去點(diǎn)擊時(shí)間,這樣能提高效率,下面小編給大家解答下實(shí)現(xiàn)思路2016-08-08
javascript表單驗(yàn)證使用示例(javascript驗(yàn)證郵箱)
JavaScript可用來在數(shù)據(jù)被送往服務(wù)器前對HTML表單中的這些輸入數(shù)據(jù)進(jìn)行驗(yàn)證2014-01-01

