音效素材网提供各类素材,打造精品素材网站!

站内导航 站长工具 投稿中心 手机访问

音效素材

HTML5 transform三维立方体实现360无死角三维旋转效果
日期:2021-09-05 23:26:30   来源:脚本之家

为了更好得掌握transform的精髓,所以决定完成三维立方体的模型,可以实现360无死角的三维旋转效果。

但是旋转时判断每个面的视图顺序比较困难,仍未完美解决,希望有人能解答!

源码直接贡献啦:

复制代码
代码如下:

<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.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 < 3;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)) {
}

if((zI > 0 && zI <= 1) ||(zI > -4 && zI <= -3)) {
var zIndex_matrix_tmp = zIndex_matrix[0][1];
zIndex_matrix[0][1] = zIndex_matrix[1][0];
zIndex_matrix[1][0] = zIndex_matrix[2][1];
zIndex_matrix[2][1] = zIndex_matrix[1][2];
zIndex_matrix[1][2] = zIndex_matrix_tmp;
} else if((zI > 1 && zI <= 2) ||(zI > -3 && zI <= -2)) {
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][0];
zIndex_matrix[1][0] = zIndex_matrix[1][2];
zIndex_matrix[1][2] = zIndex_matrix_tmp;
} else if((zI > 2 && zI <= 3) ||(zI > -2 && zI <= -1)) {
var zIndex_matrix_tmp = zIndex_matrix[0][1];
zIndex_matrix[0][1] = zIndex_matrix[1][2];
zIndex_matrix[1][2] = zIndex_matrix[2][1];
zIndex_matrix[2][1] = zIndex_matrix[1][0];
zIndex_matrix[1][0] = zIndex_matrix_tmp;
} else if((zI > 3 && zI <= 4) ||(zI > -1 && zI <= 0)) {
}
//赋值zIndex
eval(zIndex_matrix[0][1]+"="+zIndex_top_default);
eval(zIndex_matrix[1][0]+"="+zIndex_left_default);
eval(zIndex_matrix[1][1]+"="+zIndex_front_default);
eval(zIndex_matrix[1][2]+"="+zIndex_right_default);
eval(zIndex_matrix[2][1]+"="+zIndex_bottom_default);
eval(zIndex_matrix_behind+"="+zIndex_behind_default);
//front
var transform_rotate_front = "perspective(500px) rotateX("+rotateX_front+
"deg) rotateY("+rotateY_front+
"deg) rotateZ("+rotateZ_front+"deg)";
div_front.style.webkitTransform = transform_rotate_front;
div_front.style.zIndex = zIndex_front;
//behind
var transform_rotate_behind = "perspective(500px) rotateX("+rotateX_behind+
"deg) rotateY("+rotateY_behind+
"deg) rotateZ("+rotateZ_behind+"deg)";
div_behind.style.webkitTransform = transform_rotate_behind;
div_behind.style.zIndex = zIndex_behind;
//left
var transform_rotate_left = "perspective(500px) rotateX("+rotateX_left+
"deg) rotateZ("+rotateZ_left+
"deg) rotateY("+rotateY_left+"deg)";
div_left.style.webkitTransform = transform_rotate_left;
div_left.style.zIndex = zIndex_left;
//right
var transform_rotate_right = "perspective(500px) rotateX("+rotateX_right+
"deg) rotateZ("+rotateZ_right+
"deg) rotateY("+rotateY_right+"deg)";
div_right.style.webkitTransform = transform_rotate_right;
div_right.style.zIndex = zIndex_right;
//top
var transform_rotate_top = "perspective(500px) rotateX("+rotateX_top+
"deg) rotateZ("+rotateZ_top+
"deg) rotateY("+rotateY_top+"deg)";
div_top.style.webkitTransform = transform_rotate_top;
div_top.style.zIndex = zIndex_top;
//bottom
var transform_rotate_bottom = "perspective(500px) rotateX("+rotateX_bottom+
"deg) rotateZ("+rotateZ_bottom+
"deg) rotateY("+rotateY_bottom+"deg)";
div_bottom.style.webkitTransform = transform_rotate_bottom;
div_bottom.style.zIndex = zIndex_bottom;
};
//重置长方体的长、宽、高
this.resize = function(new_long, new_width, new_height)
{
long = new_long;
width = new_width;
height = new_height;
this.refresh();
};
//重置长方体的位置
this.move = function(new_left,new_top) {
top = new_top;
left = new_left;
this.refresh();
};
}

function transform() {
cuboid.resize(parseInt(document.getElementById("long").value),
parseInt(document.getElementById("width").value),
parseInt(document.getElementById("height").value));
cuboid.move(parseInt(document.getElementById("left").value),
parseInt(document.getElementById("top").value));
cuboid.rotate(parseInt(document.getElementById("rotatex").value),
parseInt(document.getElementById("rotatey").value),
parseInt(document.getElementById("rotatez").value));
//cuboid.refresh();
}
</script>
<div style="position:absolute;border:1px solid #333;top:240px;left:100px;width:1000px;height: 360px;">
left:<input id="left" value="100"></input>px

top:<input id="top" value="50"></input>px

long:<input id="long" value="100"></input>px

width:<input id="width" value="60"></input>px

height:<input id="height" value="80"></input>px

rotateX: <input id="rotatex" value="0"></input>deg

rotateY: <input id="rotatey" value="0"></input>deg

rotateZ: <input id="rotatez" value="0"></input>deg

<input type="button" value="确定" onclick="transform()"></input>

<label id="status"></label>
</div>
<script>
var cuboid = new cuboidModel(parseInt(document.getElementById("left").value),
parseInt(document.getElementById("top").value),
parseInt(document.getElementById("long").value),
parseInt(document.getElementById("width").value),
parseInt(document.getElementById("height").value));
cuboid.init();
</script>

    您感兴趣的教程

    在docker中安装mysql详解

    本篇文章主要介绍了在docker中安装mysql详解,小编觉得挺不错的,现在分享给大家,也给大家做个参考。一起跟随小编...

    详解 安装 docker mysql

    win10中文输入法仅在桌面显示怎么办?

    win10中文输入法仅在桌面显示怎么办?

    win10系统使用搜狗,QQ输入法只有在显示桌面的时候才出来,在使用其他程序输入框里面却只能输入字母数字,win10中...

    win10 中文输入法

    一分钟掌握linux系统目录结构

    这篇文章主要介绍了linux系统目录结构,通过结构图和多张表格了解linux系统目录结构,感兴趣的小伙伴们可以参考一...

    结构 目录 系统 linux

    PHP程序员玩转Linux系列 Linux和Windows安装

    这篇文章主要为大家详细介绍了PHP程序员玩转Linux系列文章,Linux和Windows安装nginx教程,具有一定的参考价值,感兴趣...

    玩转 程序员 安装 系列 PHP

    win10怎么安装杜比音效Doby V4.1 win10安装杜

    第四代杜比®家庭影院®技术包含了一整套协同工作的技术,让PC 发出清晰的环绕声同时第四代杜比家庭影院技术...

    win10杜比音效

    纯CSS实现iOS风格打开关闭选择框功能

    这篇文章主要介绍了纯CSS实现iOS风格打开关闭选择框,本文通过实例代码给大家介绍的非常详细,对大家的学习或工作...

    css ios c

    Win7如何给C盘扩容 Win7系统电脑C盘扩容的办法

    Win7如何给C盘扩容 Win7系统电脑C盘扩容的

    Win7给电脑C盘扩容的办法大家知道吗?当系统分区C盘空间不足时,就需要给它扩容了,如果不管,C盘没有足够的空间...

    Win7 C盘 扩容

    百度推广竞品词的投放策略

    SEM是基于关键词搜索的营销活动。作为推广人员,我们所做的工作,就是打理成千上万的关键词,关注它们的质量度...

    百度推广 竞品词

    Visual Studio Code(vscode) git的使用教程

    这篇文章主要介绍了详解Visual Studio Code(vscode) git的使用,小编觉得挺不错的,现在分享给大家,也给大家做个参考。...

    教程 Studio Visual Code git

    七牛云储存创始人分享七牛的创立故事与

    这篇文章主要介绍了七牛云储存创始人分享七牛的创立故事与对Go语言的应用,七牛选用Go语言这门新兴的编程语言进行...

    七牛 Go语言

    Win10预览版Mobile 10547即将发布 9月19日上午

    微软副总裁Gabriel Aul的Twitter透露了 Win10 Mobile预览版10536即将发布,他表示该版本已进入内部慢速版阶段,发布时间目...

    Win10 预览版

    HTML标签meta总结,HTML5 head meta 属性整理

    移动前端开发中添加一些webkit专属的HTML5头部标签,帮助浏览器更好解析HTML代码,更好地将移动web前端页面表现出来...

    移动端html5模拟长按事件的实现方法

    这篇文章主要介绍了移动端html5模拟长按事件的实现方法的相关资料,小编觉得挺不错的,现在分享给大家,也给大家...

    移动端 html5 长按

    HTML常用meta大全(推荐)

    这篇文章主要介绍了HTML常用meta大全(推荐),文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参...

    cdr怎么把图片转换成位图? cdr图片转换为位图的教程

    cdr怎么把图片转换成位图? cdr图片转换为

    cdr怎么把图片转换成位图?cdr中插入的图片想要转换成位图,该怎么转换呢?下面我们就来看看cdr图片转换为位图的...

    cdr 图片 位图

    win10系统怎么录屏?win10系统自带录屏详细教程

    win10系统怎么录屏?win10系统自带录屏详细

    当我们是使用win10系统的时候,想要录制电脑上的画面,这时候有人会想到下个第三方软件,其实可以用电脑上的自带...

    win10 系统自带录屏 详细教程

    + 更多教程 +
    HTMLCSSDreamweaverFrontpage