纯JS实现旋转图片3D展示效果

网络编程 2021-07-04 21:47www.168986.cn编程入门
本文给大家分享的是纯js实现的类似flash里的图片环绕旋转效果,非常炫酷,有需要的小伙伴可以参考下。

CSS:

<style type="text/css">
#show{position:relative;margin:20px auto;width:800px;}
.item{position:absolute;height:40px;width:60px;background:#999999;border:1px solid #eeeeee;cursor:pointer;}</style>

Html:

input id="l" type="button" value="left" /> <input id="r" type="button" value="right" />
<div id="show">
<div class="item">1</div>
<div class="item">2</div>
<div class="item">3</div>
<div class="item">4</div>
<div class="item">5</div>
<div class="item">6</div>
<div class="item">7</div>
<div class="item">8</div>
<div class="item">9</div>
<div class="item">0</div>
<div class="item">a</div>
<div class="item">b</div>
<div class="item">1</div>
<div class="item">2</div>
<div class="item">3</div>
</div>

JS

<script>
var len;
var showerObj;
var listObj;
var showerWidth=800;
var showerHeight=400;
var r;
var cR=0;
var R=0;
var timer=0;
window.onload=function(){
 showerObj=document.getElementById("show");
 listObj=showerObj.getElementsByTagName("div");
 len=listObj.length;
 r=Math.PI/180360/len;
 for(var i=0;i<len;i++){
 var item=listObj[i];
 item.style.=showerHeight/2+Math.sin(ri)showerWidth/2-20+"px";
 item.style.left=showerWidth/2+Math.cos(ri)showerWidth/2-30+"px";
 item.rotate=(ri+2Math.PI)%(2Math.PI);
 item.onclick=function(){
  cR=Math.PI/2-this.rotate;
  timer || (timer=setInterval(rotate,10));
  
 }
 
 }
 var rX=showerObj.offsetLeft+showerWidth/2;
 var ry=showerObj.offsetTop+showerHeight/2;
 
 var rotate=function(){
 R=(R+2Math.PI)%(2Math.PI);
 if(cR-R<0) cR=cR+2Math.PI;
 if(cR-R<Math.PI){
  R=R+(cR-R)/19;
 }else{
  R=R-(2Math.PI+R-cR)/19;
 
 }
 
 if(Math.abs((cR+2Math.PI)%(2Math.PI)-(R+2Math.PI)%(2Math.PI))<Math.PI/720){
  R=cR;
  clearInterval(timer);
  timer=0;
 }
 
 for(var i=0;i<len;i++){
  var item=listObj[i];
  var w,h;
  var sinR=Math.sin(ri+R);
  var cosR=Math.cos(ri+R);
  w=60+0.660sinR;
  h=(40+0.640sinR);
  item.style.cssText +=";width:"+w+"px;height:"+h+"px;:"+parseInt(showerHeight/2+sinRshowerWidth/2/3-w/2)+"px;left:"+parseInt(showerWidth/2+cosRshowerWidth/2-h/2)+"px;z-index:"+parseInt(showerHeight/2+sinRshowerWidth/2/3-w/2)+";";
 
 }
 }
 
 document.getElementById("l").onclick=function(){
 cR=(cR+r+2Math.PI)%(2Math.PI);
 timer || (timer=setInterval(rotate,10));
 }
 document.getElementById("r").onclick=function(){
 cR=(cR-r+2Math.PI)%(2Math.PI);
 timer || (timer=setInterval(rotate,10));
 }
 rotate();
}
</script>

以上所述就是本文的全部内容了,希望大家能够喜欢。

Copyright © 2016-2025 www.168986.cn 狼蚁网络 版权所有 Power by