JS uses arrays to create image switching effects for your reference. The specific content is as follows
Array element position transformation:
Split the content into an array, add the first one to the end, and delete the first one
<div id="box">1,2,3,4</div><input type="button" value='switch' 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]); // add the first one to the end and delete the first arr.shift(); oDiv.innerHTML=arr; } }</script>Simulation picture switching effect:
window.onload=function(){ var aDiv=document.getElementsByTagName('div'); var aInput=document.getElementsByTagName('input'); var arr=[];//Create an empty array to store attributes for(var i=0;i<aDiv.length;i++){ console.dir(getStyle(aDiv[i],'left'));//Get the pure final style//Add attributes as conforming arrays to arr, which can be used for multi-attribute arr.push([getStyle(aDiv[i],'left'),getStyle(aDiv[i],'top')]); } // console.dir(arr); aInput[0].onclick=function(){//Add the first one to the end and delete the first arr.push(arr[0]); arr.shift(); for(var i=0;i<aDiv.length;i++){//After operating the array, reassign aDiv[i].style.left=arr[i][0]; aDiv[i].style.top=arr[i][1]; } }; aInput[1].onclick=function(){//Add the last one to the front and delete the last 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){//Get the final style if(obj.currentStyle){ return obj.currentStyle[attr]; }else{ return getComputedStyle(obj,false)[attr]; } }}Simple renderings:
Example version:
Ideas:
If there are five pictures: the left values of Figures 1 to 5 are: 20px, 60px, 100px, 240px, and 380px;
After clicking the left toggle button, the corresponding values of Figure 1 to 5 left become: 60px, 100px, 240px, 380px, 20px;
--------------------------------------------------------------------------------------------------------------------------------
It is equivalent to moving the first element of this set of array to the end: 20px, 60px, 100px, 240px, 380px, 20px;
Then delete the first element to: 60px, 100px, 240px, 380px, 20px;
And so on:
Example layout:
<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>
Example style:
#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 code:
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(){//left 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(){//Right 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){//Get the value of the unit if(obj.currentStyle){ return obj.currentStyle[attr]; }else{ return getComputedStyle(obj,false)[attr]; } }}Reproduction image:
The above is all the content of this article. I hope it will be helpful to everyone's learning and I hope everyone will support Wulin.com more.