复制代码代码如下: style .cuboid_side_div{ position:absolute; border:1px solid #333; -webkit-transition:ease all 1s; } /style script /** * 本版本存在以下问题: * 三维旋转的zIndex计算有问题 * 还欠缺多种模型,常见的包括:线、面、椎体、球体、椭球体等。 */ function cuboidModel(left_init,top_init,long_init,width_init,height_init) { //////////////////////////////////////// //初始化私有变量 /////////////////////////////////////// //初始化长方体位置、大小 var left = left_init; var top = top_init; var long = long_init; var width = width_init; var height = height_init; //初始化变换角度,默认为0 var rotateX = 0; var rotateY = 0; var rotateZ = 0; var zIndex = 0; //定义长方体六个面的div对象 var div_front; var div_behind; var div_left; var div_right; var div_top; var div_bottom;
//////////////////////////////////////// //初始化长方体 /////////////////////////////////////// //根据初始位置构造六个面。 this.init = function() { //创建front div div_front = document.createElement("div"); div_front.html' target='_blank'>className = "cuboid_side_div"; div_front.innerHTML = "div front"; div_front.style.backgroundColor="#f1b2b2"; document.body.appendChild(div_front); //创建behind div div_behind = document.createElement("div"); div_behind.className = "cuboid_side_div"; div_behind.innerHTML = "div behind"; div_behind.style.backgroundColor="#bd91eb"; document.body.appendChild(div_behind); //创建left div div_left = document.createElement("div"); div_left.className = "cuboid_side_div"; div_left.innerHTML = "div left"; div_left.style.backgroundColor="#64a3c3"; document.body.appendChild(div_left); //创建right div div_right = document.createElement("div"); div_right.className = "cuboid_side_div"; div_right.innerHTML = "div right"; div_right.style.backgroundColor="#78e797"; document.body.appendChild(div_right); //创建top div div_top = document.createElement("div"); div_top.className = "cuboid_side_div"; div_top.innerHTML = "div top"; div_top.style.backgroundColor="#e7db78"; document.body.appendChild(div_top); //创建bottom div div_bottom = document.createElement("div"); div_bottom.className = "cuboid_side_div"; div_bottom.innerHTML = "div bottom"; div_bottom.style.backgroundColor="#e79c78"; document.body.appendChild(div_bottom); this.refresh(); }; //重绘 this.refresh = function() { //定义div_front样式 div_front.style.left = left+"px"; div_front.style.top = top+"px"; div_front.style.width = long +"px"; div_front.style.height = height +"px"; div_front.style.webkitTransformOrigin = "50% 50% "+((-1)*width / 2)+"px"; //定义div_behind样式 div_behind.style.left = left+"px"; div_behind.style.top = top+"px"; div_behind.style.width = div_front.style.width; div_behind.style.height = div_front.style.height; div_behind.style.webkitTransformOrigin = "50% 50% "+((-1)*width / 2)+"px"; //定义div_left样式 div_left.style.left = left+((long - height) / 2)+"px"; div_left.style.top = top + ((height - width) / 2)+"px"; div_left.style.width = height +"px"; div_left.style.height = width +"px"; div_left.style.webkitTransformOrigin = "50% 50% "+((-1) * long /2 )+"px"; //定义div_right样式 div_right.style.left = div_left.style.left; div_right.style.top = div_left.style.top; div_right.style.width = div_left.style.width; div_right.style.height = div_left.style.height; div_right.style.webkitTransformOrigin = "50% 50% "+((-1) * long /2 )+"px"; //定义div_top样式 div_top.style.left = left+"px"; div_top.style.top = top+((height - width)/ 2)+"px"; div_top.style.width = long +"px"; div_top.style.height = width +"px"; div_top.style.webkitTransformOrigin = "50% 50% "+((-1) * height /2 )+"px"; //定义div_bottom样式 div_bottom.style.left = div_top.style.left; div_bottom.style.top = div_top.style.top; div_bottom.style.width = div_top.style.width; div_bottom.style.height = div_top.style.height; div_bottom.style.webkitTransformOrigin = "50% 50% "+((-1) * height /2 )+"px"; this.rotate(rotateX,rotateY,rotateZ); }; //旋转立方体 this.rotate = function(x,y,z) { rotateX = x; rotateY = y; rotateZ = z; var rotateX_front = rotateX; var rotateY_front = rotateY; var rotateZ_front = rotateZ; //判断各个面旋转角度 var rotateX_behind = rotateX_front+180; var rotateY_behind = rotateY_front * (-1); var rotateZ_behind = rotateZ_front * (-1); var rotateX_top = rotateX_front+90; var rotateY_top = rotateZ_front; var rotateZ_top = rotateY_front * (-1); var rotateX_bottom = rotateX_front-90; var rotateY_bottom = rotateZ_front * (-1); var rotateZ_bottom = rotateY_front; var rotateX_left = rotateX_front + 90; var rotateY_left = rotateZ_front - 90; var rotateZ_left = rotateY_front * (-1); var rotateX_right = rotateX_front + 90; var rotateY_right = rotateZ_front + 90; var rotateZ_right = rotateY_front * (-1); //判断各个面的z轴显示顺序 var zIndex_front_default = -1; var zIndex_behind_default = -6; var zIndex_top_default = -5; var zIndex_bottom_default = -2; var zIndex_left_default = -4; var zIndex_right_default = -3; var xI = (rotateX_front / 90) % 4; var yI = (rotateY_front / 90) % 4; var zI = (rotateZ_front / 90) % 4; var zIndex_matrix = new Array(); for(var i = 0; i i++) { zIndex_matrix.push(new Array()); } zIndex_matrix = [["","zIndex_top",""], ["zIndex_left","zIndex_front","zIndex_right"], ["","zIndex_bottom",""]]; var zIndex_matrix_behind = "zIndex_behind"; //计算zIndex if((xI = 0 xI 1) ||(xI = -4 xI -3)) { } else if((xI = 1 xI 2) ||(xI = -3 xI -2)) { var zIndex_matrix_tmp = zIndex_matrix[0][1]; zIndex_matrix[0][1] = zIndex_matrix[1][1]; zIndex_matrix[1][1] = zIndex_matrix[1][2]; zIndex_matrix[1][2] = zIndex_matrix_behind; zIndex_matrix_behind = zIndex_matrix_tmp; } else if((xI = 2 xI 3) ||(xI = -2 xI -1)) { var zIndex_matrix_tmp = zIndex_matrix[0][1]; zIndex_matrix[0][1] = zIndex_matrix[2][1]; zIndex_matrix[2][1] = zIndex_matrix_tmp; zIndex_matrix_tmp = zIndex_matrix[1][1]; zIndex_matrix[1][1] = zIndex_matrix_behind; zIndex_matrix_behind = zIndex_matrix_tmp; } else if((xI = 3 xI 4) ||(xI = -1 xI 0)) { var zIndex_matrix_tmp = zIndex_matrix[0][1]; zIndex_matrix[0][1] = zIndex_matrix_behind; zIndex_matrix_behind = zIndex_matrix[2][1]; zIndex_matrix[2][1] = zIndex_matrix[1][1]; zIndex_matrix[1][1] = zIndex_matrix_tmp; } if((yI 0 yI = 1) ||(yI -4 yI = -3)) { var zIndex_matrix_tmp = zIndex_matrix[1][0]; zIndex_matrix[1][0] = zIndex_matrix_behind; zIndex_matrix_behind = zIndex_matrix[1][2]; zIndex_matrix[1][2] = zIndex_matrix[1][1]; zIndex_matrix[1][1] = zIndex_matrix_tmp; } else if((yI 1 yI = 2) ||(yI -3 yI = -2)) { var zIndex_matrix_tmp = zIndex_matrix[1][0]; zIndex_matrix[1][0] = zIndex_matrix[1][2]; zIndex_matrix[1][2] = zIndex_matrix_tmp; zIndex_matrix_tmp = zIndex_matrix[1][1]; zIndex_matrix[1][1] = zIndex_matrix_behind; zIndex_matrix_behind = zIndex_matrix_tmp; } else if((yI 2 yI = 3) ||(yI -2 yI = -1)) { var zIndex_matrix_tmp = zIndex_matrix[1][0]; zIndex_matrix[1][0] = zIndex_matrix[1][1]; zIndex_matrix[1][1] = zIndex_matrix[1][2]; zIndex_matrix[1][2] = zIndex_matrix_behind; zIndex_matrix_behind = zIndex_matrix_tmp; } else if((yI 3 yI = 4) ||(yI -1 yI = 0)) { }