欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

css3 transform及原生js實現(xiàn)鼠標(biāo)拖動3D立方體旋轉(zhuǎn)

  發(fā)布時間:2016-06-20 11:14:50   作者:佚名   我要評論
這篇文章主要為大家詳細(xì)介紹了css3 transform及原生js實現(xiàn)鼠標(biāo)拖動3D立方體旋轉(zhuǎn)的相關(guān)資料,感興趣的小伙伴們可以參考一下

本文通過原生JS,點(diǎn)擊事件,鼠標(biāo)按下、鼠標(biāo)抬起和鼠標(biāo)移動事件,實現(xiàn)3D立方體的拖動旋轉(zhuǎn),并將旋轉(zhuǎn)角度實時的反應(yīng)至界面上顯示。
 
實現(xiàn)原理:通過獲取鼠標(biāo)點(diǎn)擊屏幕時的坐標(biāo)和鼠標(biāo)移動時的坐標(biāo),來獲得鼠標(biāo)在X軸、Y軸移動的距離,將距離實時賦值給transform屬性。
 
從而通過改變transform:rotate屬性值來達(dá)到3D立方體旋轉(zhuǎn)的效果:
 
HTML代碼塊:

XML/HTML Code復(fù)制內(nèi)容到剪貼板
  1. <body>  
  2.     <input type="button" class="open" value="點(diǎn)擊散開"/>  
  3.     <input type="text" class="xNum" value="0"/>//X軸旋轉(zhuǎn)角度   
  4.     <input type="text" class="yNum" value="0"/>//Y軸旋轉(zhuǎn)角度   
  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復(fù)制內(nèi)容到剪貼板
  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;//設(shè)置動畫暫停   
  75.         }   
  76. </style>    

JS代碼塊:

JavaScript Code復(fù)制內(nèi)容到剪貼板
  1. <script>   
  2.     move();   
  3.   
  4.     clickBox();   
  5.   
  6.     //鼠標(biāo)按下且移動時觸發(fā),   
  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) {//鼠標(biāo)按下事件   
  17.             body.style.cursor = 'url("img/closedhand1.png"),auto';   
  18.             xArr[0] = e.clientX/2;//獲取鼠標(biāo)點(diǎn)擊屏幕時的坐標(biāo)   
  19.             yArr[0] = e.clientY/2;   
  20.             window.onmousemove = function (e) {//鼠標(biāo)移動事件————當(dāng)鼠標(biāo)按下且移動時觸發(fā)   
  21.                 xArr[1] = e.clientX/2;//獲取鼠標(biāo)移動時第一個點(diǎn)的坐標(biāo)   
  22.                 yArr[1] = e.clientY/2;   
  23.                 yy += xArr[1] - xArr[0];//獲得鼠標(biāo)移動的距離   
  24.                 xx += yArr[1] - yArr[0];   
  25.                 xNum.value = xx+"°";//將所獲得距離數(shù)字賦值給input顯示旋轉(zhuǎn)角度   
  26.                 yNum.value = yy+"°";   
  27.                 //將旋轉(zhuǎn)角度寫入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 () {//鼠標(biāo)抬起事件————用于清除鼠標(biāo)移動事件,   
  35.             body.style.cursor = 'url("img/openhand1.png"),auto';   
  36.             window.onmousemove = null;   
  37.         }   
  38.     }   
  39.     //點(diǎn)擊事件、負(fù)責(zé)立方體盒子的六面伸展   
  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參數(shù)   
  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參數(shù)   
  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 = "點(diǎn)擊合并";   
  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 = "點(diǎn)擊散開";   
  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.   

以上就是本文的全部內(nèi)容,希望對大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。

相關(guān)文章

最新評論