纯JS实现旋转图片3D展示效果
CSS:
<styletype="text/css">
#show{position:relative;margin:20pxauto;width:800px;}
.item{position:absolute;height:40px;width:60px;background:#999999;border:1pxsolid#eeeeee;cursor:pointer;}</style>
Html:
inputid="l"type="button"value="left"/><inputid="r"type="button"value="right"/> <divid="show"> <divclass="item">1</div> <divclass="item">2</div> <divclass="item">3</div> <divclass="item">4</div> <divclass="item">5</div> <divclass="item">6</div> <divclass="item">7</div> <divclass="item">8</div> <divclass="item">9</div> <divclass="item">0</div> <divclass="item">a</div> <divclass="item">b</div> <divclass="item">1</div> <divclass="item">2</div> <divclass="item">3</div> </div>
JS
<script>
varlen;
varshowerObj;
varlistObj;
varshowerWidth=800;
varshowerHeight=400;
varr;
varcR=0;
varccR=0;
vartimer=0;
window.onload=function(){
showerObj=document.getElementById("show");
listObj=showerObj.getElementsByTagName("div");
len=listObj.length;
r=Math.PI/180*360/len;
for(vari=0;i<len;i++){
varitem=listObj[i];
item.style.top=showerHeight/2+Math.sin(r*i)*showerWidth/2-20+"px";
item.style.left=showerWidth/2+Math.cos(r*i)*showerWidth/2-30+"px";
item.rotate=(r*i+2*Math.PI)%(2*Math.PI);
item.onclick=function(){
cR=Math.PI/2-this.rotate;
timer||(timer=setInterval(rotate,10));
}
}
varrX=showerObj.offsetLeft+showerWidth/2;
varry=showerObj.offsetTop+showerHeight/2;
varrotate=function(){
ccR=(ccR+2*Math.PI)%(2*Math.PI);
if(cR-ccR<0)cR=cR+2*Math.PI;
if(cR-ccR<Math.PI){
ccR=ccR+(cR-ccR)/19;
}else{
ccR=ccR-(2*Math.PI+ccR-cR)/19;
}
if(Math.abs((cR+2*Math.PI)%(2*Math.PI)-(ccR+2*Math.PI)%(2*Math.PI))<Math.PI/720){
ccR=cR;
clearInterval(timer);
timer=0;
}
for(vari=0;i<len;i++){
varitem=listObj[i];
varw,h;
varsinR=Math.sin(r*i+ccR);
varcosR=Math.cos(r*i+ccR);
w=60+0.6*60*sinR;
h=(40+0.6*40*sinR);
item.style.cssText+=";width:"+w+"px;height:"+h+"px;top:"+parseInt(showerHeight/2+sinR*showerWidth/2/3-w/2)+"px;left:"+parseInt(showerWidth/2+cosR*showerWidth/2-h/2)+"px;z-index:"+parseInt(showerHeight/2+sinR*showerWidth/2/3-w/2)+";";
}
}
document.getElementById("l").onclick=function(){
cR=(cR+r+2*Math.PI)%(2*Math.PI);
timer||(timer=setInterval(rotate,10));
}
document.getElementById("r").onclick=function(){
cR=(cR-r+2*Math.PI)%(2*Math.PI);
timer||(timer=setInterval(rotate,10));
}
rotate();
}
</script>
以上所述就是本文的全部内容了,希望大家能够喜欢。