css3 transform及原生js实现鼠标拖动3D立方体旋转

所属分类: 网页制作 / CSS 阅读数: 1073
收藏 0 赞 0 分享

本文通过原生JS,点击事件,鼠标按下、鼠标抬起和鼠标移动事件,实现3D立方体的拖动旋转,并将旋转角度实时的反应至界面上显示。
 
实现原理:通过获取鼠标点击屏幕时的坐标和鼠标移动时的坐标,来获得鼠标在X轴、Y轴移动的距离,将距离实时赋值给transform属性。
 
从而通过改变transform:rotate属性值来达到3D立方体旋转的效果:
 
HTML代码块:

XML/HTML Code复制内容到剪贴板
  1. <body>  
  2.     <input type="button" class="open" value="点击散开"/>  
  3.     <input type="text" class="xNum" value="0"/>//X轴旋转角度   
  4.     <input type="text" class="yNum" value="0"/>//Y轴旋转角度   
  5.     <input type="text" class="zNum"/>  
  6.     <div class="big_box">  
  7.         <div class="box">  
  8.             <span>1</span>  
  9.             <span>2</span>  
  10.             <span>3</span>  
  11.             <span>4</span>  
  12.             <span>5</span>  
  13.             <span>6</span>  
  14.         </div>  
  15.     </div>  
  16. </body>   

CSS代码块:

CSS Code复制内容到剪贴板
  1. <style>   
  2.  body{cursorurl("img/openhand1.png"),auto;}   
  3.      .big_box{   
  4.             width500px;   
  5.             height500px;   
  6.             margin200px auto;   
  7.         }   
  8.   
  9.         .box{   
  10.             -webkit-transform-style: preserve-3d;   
  11.             -moz-transform-style: preserve-3d;   
  12.             -ms-transform-style: preserve-3d;   
  13.             transform-style: preserve-3d;   
  14.             transform-origin:100px 100px 00px;   
  15.             positionrelative;   
  16.             transform: rotatex(0deg) rotatey(0deg) rotatez(0deg)scale3d(0.7,0.7,0.7);   
  17.         }   
  18.         .box span{   
  19.             transition: all 1s linear;   
  20.   
  21.         }   
  22.         span{   
  23.             displayblock;   
  24.             positionabsolute;   
  25.             width200px;   
  26.             height200px;   
  27.             box-sizing: border-box;   
  28.             border:1px solid #999;   
  29.             /*opacity: 0.7;*/  
  30.             text-aligncenter;   
  31.             line-height200px;   
  32.             font-size60px;   
  33.             font-weight: 700;   
  34.             border-radius: 12%;   
  35.   
  36.         }   
  37.         .box span:nth-child(1){   
  38.             background-color: deepskyblue;   
  39.             transform-origin: left;   
  40.             transform: rotatey(-90deg) translatex(-100px);//左   
  41.         }   
  42.         .box span:nth-child(2){   
  43.             background-colorred;   
  44.             transform-origin: rightright;   
  45.             transform: rotatey(90deg) translatex(100px) ;//右   
  46.   
  47.         }   
  48.   
  49.         .box span:nth-child(3){   
  50.             background-colorgreen;   
  51.             transform-origin: top;   
  52.             transform: rotatex(90deg) translatey(-100px) ;//上   
  53.   
  54.         }   
  55.         .box span:nth-child(4){   
  56.             background-color#6633FF;   
  57.             transform-origin: bottombottom;   
  58.             transform: rotatex(-90deg) translatey(100px);//下   
  59.         }   
  60.         .box span:nth-child(5){   
  61.             background-color: gold;   
  62.             transform: translatez(-100px);//后   
  63.         }   
  64.         .box span:nth-child(6){   
  65.   
  66.             background-color#122b40;   
  67.             transform: translatez(100px);//前   
  68.         }   
  69.         .box:hover span{   
  70.   
  71.             opacity: 0.3;   
  72.         }   
  73.         .box:hover{   
  74.             animation-play-state:paused;//设置动画暂停   
  75.         }   
  76. </style>    

JS代码块:

JavaScript Code复制内容到剪贴板
  1. <script>   
  2.     move();   
  3.   
  4.     clickBox();   
  5.   
  6.     //鼠标按下且移动时触发,   
  7.   
  8.     function move(){   
  9.         var body = document.querySelector("body");   
  10.         var box = document.querySelector(".box");   
  11.         var xNum =document.querySelector(".xNum");   
  12.         var yNum =document.querySelector(".yNum");   
  13.         var x = 0,y = 0,z = 0;   
  14.         var xx = 0,yy = 0;   
  15.         var xArr = [],yArr = [];   
  16.         window.onmousedown = function (e) {//鼠标按下事件   
  17.             body.style.cursor = 'url("img/closedhand1.png"),auto';   
  18.             xArr[0] = e.clientX/2;//获取鼠标点击屏幕时的坐标   
  19.             yArr[0] = e.clientY/2;   
  20.             window.onmousemove = function (e) {//鼠标移动事件————当鼠标按下且移动时触发   
  21.                 xArr[1] = e.clientX/2;//获取鼠标移动时第一个点的坐标   
  22.                 yArr[1] = e.clientY/2;   
  23.                 yy += xArr[1] - xArr[0];//获得鼠标移动的距离   
  24.                 xx += yArr[1] - yArr[0];   
  25.                 xNum.value = xx+"°";//将所获得距离数字赋值给input显示旋转角度   
  26.                 yNum.value = yy+"°";   
  27.                 //将旋转角度写入transform中   
  28.                 box.style.transform = "rotatex("+xx+"deg) rotatey("+yy+"deg) rotatez(0deg)scale3d(0.7,0.7,0.7)";   
  29.                 xArr[0] = e.clientX/2;   
  30.                 yArr[0] = e.clientY/2;   
  31.             }   
  32.   
  33.         };   
  34.         window.onmouseup = function () {//鼠标抬起事件————用于清除鼠标移动事件,   
  35.             body.style.cursor = 'url("img/openhand1.png"),auto';   
  36.             window.onmousemove = null;   
  37.         }   
  38.     }   
  39.     //点击事件、负责立方体盒子的六面伸展   
  40.     function clickBox(){   
  41.         var btn = document.querySelector(".open");   
  42.         var box = document.querySelector(".box");   
  43.         var son = box.children;   
  44.         var value = 0;   
  45.         //存储立方体散开时的transform参数   
  46.         var arr0 = ["rotatey(-90deg) translatex(-100px)","rotatey(90deg) translatex(100px)","rotatex(90deg) translatey(-100px)",<br>"rotatex(-90deg) translatey(100px)","translatez(-100px)","translatez(100px)"];   
  47.         //存储立方体合并时的transform参数   
  48.         var arr1 = ["rotatey(-90deg) translatex(-100px)translatez(100px)","rotatey(90deg) translatex(100px)translatez(100px)",<br>"rotatex(90deg) translatey(-100px)translatez(100px)","rotatex(-90deg) translatey(100px)translatez(100px)","translatez(-200px)","translatez(200px)"];   
  49.         btn.onclick = function(){   
  50.             if(value == 0){   
  51.                 value = 1;   
  52.                 btn.value = "点击合并";   
  53.                 for(var i=0;i<arr1.length;i++){   
  54.                     son[i].style.transform = arr1[i];   
  55.                     console.log(son[i])   
  56.                 }   
  57.             }else if(value == 1){   
  58.                 value = 0;   
  59.                 btn.value = "点击散开";   
  60.                 for(var j=0;j<arr0.length;j++){   
  61.                     son[j].style.transform = arr0[j];   
  62.                     console.log(j);   
  63.   
  64.                 }   
  65.             }   
  66.         };   
  67.     }   
  68. </script>    
  69.   

以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持脚本之家。

更多精彩内容其他人还在看

CSS入门教程:计算CSS盒模型宽和高

 出处:当我们布局一个网页的时候,经常会遇到这样的一种情况,那就是最终网页成型的宽度或是高度会超出我们预先的计算,其实就就是所谓的CSS的盒模型造成的。 #test{margin:10px;padding:10px;width:100px;height:100px;}
收藏 0 赞 0 分享

在IE流览器中正确显示PNG透明图片

  png图片有很好的品质。阴影效果也不会有杂边,很流畅。如果插入网页的话可以给网站内容增色不少!更重要的是在不增加图片容量大小的情况下提高了页面的图片的质量。对于有复杂背景,如:在有颜色过度背景上插入不规则边框的图片带来极大很便利!   但目前IE中对于插入
收藏 0 赞 0 分享

CSS教程:DIV底部放置文字

  css对文字的布局上没有靠容器底部对齐的参数,目前使用的一个不错的方法也比较好.就是用position属性来解决,看下面的代码,用position的相对和绝对定位功能也轻松的实现了,文字靠近div低部对齐,并且靠近的距离还可以精确到像素,自己可以调节,是不是很不错呢?
收藏 0 赞 0 分享

如何用CSS让文字居于div的底部

  这个问题是别人提出的,因为css对文字的布局上没有靠容器底部对齐的参数,(或许有但是我没有发现)不过目前我使用的一个不错的方法也比较好.就是用position属性来解决,看下面的代码,我用position的相对和绝对定位功能也轻松的实现了,文字靠近div低部对齐,并且靠近
收藏 0 赞 0 分享

从A页面连接到B页面后并直接把B页面的隐藏层显示

  这个效果实现的是,在B页面里有两个层,一个显示层,我们暂定名c层,一个是隐藏层,我们暂定名d层,单独进B页面的时候,c层显示,d层隐藏,然而从A页面连接到B页面的时候,则是让d层显示,c层隐藏,我觉得这个效果对网页设计者以后会有很大帮助,现在把代码发出来,
收藏 0 赞 0 分享

CSS样式表定义标签li前面样式

定义LI前面的小点样式 view plaincopy to clipboardprint? 语法: list-style-type : disc | circle | square | decimal | lower-roman | upper-roman | lowe
收藏 0 赞 0 分享

符合标准的div css制作的弹出菜单

本文介绍了五款符合标准的div css制作的弹出菜单,而且不含有js的. NO.1最基本的:二级dropdown弹出菜单 <!DOCTYPE html PUB
收藏 0 赞 0 分享

CSS实现在文章每段后面加入带连接的隐藏文字

代码主要理解3个参数:createElement、createTextNode、appendChild。这3个js参数分别是创建元素、创建字符、追加节点。代码原理:循环页面段落标签<p>,创建连接元素<a>,创建要显示的连接字符,用SetAttribute
收藏 0 赞 0 分享

CSS:浏览器特定选择器介绍

当你想在一个浏览器里改变样式而不像在其他浏览器中改变时,这些选择器很有用。 IE6以下 *html{} IE 7 以下 *:first-child html {} * html {} 只对IE 7 *:first-child html {} 只对IE 7
收藏 0 赞 0 分享

WEB标准学习,认识两种网页声明的含义

即网页标准推出来以后,我们时常会看到两种不同的网页的声明,一个是Dhtml,一个是Xhtml。如下所示: <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "ht
收藏 0 赞 0 分享
查看更多