css3 transform及原生js實現(xiàn)鼠標(biāo)拖動3D立方體旋轉(zhuǎ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代碼塊:
- <body>
- <input type="button" class="open" value="點(diǎn)擊散開"/>
- <input type="text" class="xNum" value="0"/>//X軸旋轉(zhuǎn)角度
- <input type="text" class="yNum" value="0"/>//Y軸旋轉(zhuǎn)角度
- <input type="text" class="zNum"/>
- <div class="big_box">
- <div class="box">
- <span>1</span>
- <span>2</span>
- <span>3</span>
- <span>4</span>
- <span>5</span>
- <span>6</span>
- </div>
- </div>
- </body>
CSS代碼塊:
- <style>
- body{cursor: url("img/openhand1.png"),auto;}
- .big_box{
- width: 500px;
- height: 500px;
- margin: 200px auto;
- }
- .box{
- -webkit-transform-style: preserve-3d;
- -moz-transform-style: preserve-3d;
- -ms-transform-style: preserve-3d;
- transform-style: preserve-3d;
- transform-origin:100px 100px 00px;
- position: relative;
- transform: rotatex(0deg) rotatey(0deg) rotatez(0deg)scale3d(0.7,0.7,0.7);
- }
- .box span{
- transition: all 1s linear;
- }
- span{
- display: block;
- position: absolute;
- width: 200px;
- height: 200px;
- box-sizing: border-box;
- border:1px solid #999;
- /*opacity: 0.7;*/
- text-align: center;
- line-height: 200px;
- font-size: 60px;
- font-weight: 700;
- border-radius: 12%;
- }
- .box span:nth-child(1){
- background-color: deepskyblue;
- transform-origin: left;
- transform: rotatey(-90deg) translatex(-100px);//左
- }
- .box span:nth-child(2){
- background-color: red;
- transform-origin: rightright;
- transform: rotatey(90deg) translatex(100px) ;//右
- }
- .box span:nth-child(3){
- background-color: green;
- transform-origin: top;
- transform: rotatex(90deg) translatey(-100px) ;//上
- }
- .box span:nth-child(4){
- background-color: #6633FF;
- transform-origin: bottombottom;
- transform: rotatex(-90deg) translatey(100px);//下
- }
- .box span:nth-child(5){
- background-color: gold;
- transform: translatez(-100px);//后
- }
- .box span:nth-child(6){
- background-color: #122b40;
- transform: translatez(100px);//前
- }
- .box:hover span{
- opacity: 0.3;
- }
- .box:hover{
- animation-play-state:paused;//設(shè)置動畫暫停
- }
- </style>
JS代碼塊:
- <script>
- move();
- clickBox();
- //鼠標(biāo)按下且移動時觸發(fā),
- function move(){
- var body = document.querySelector("body");
- var box = document.querySelector(".box");
- var xNum =document.querySelector(".xNum");
- var yNum =document.querySelector(".yNum");
- var x = 0,y = 0,z = 0;
- var xx = 0,yy = 0;
- var xArr = [],yArr = [];
- window.onmousedown = function (e) {//鼠標(biāo)按下事件
- body.style.cursor = 'url("img/closedhand1.png"),auto';
- xArr[0] = e.clientX/2;//獲取鼠標(biāo)點(diǎn)擊屏幕時的坐標(biāo)
- yArr[0] = e.clientY/2;
- window.onmousemove = function (e) {//鼠標(biāo)移動事件————當(dāng)鼠標(biāo)按下且移動時觸發(fā)
- xArr[1] = e.clientX/2;//獲取鼠標(biāo)移動時第一個點(diǎn)的坐標(biāo)
- yArr[1] = e.clientY/2;
- yy += xArr[1] - xArr[0];//獲得鼠標(biāo)移動的距離
- xx += yArr[1] - yArr[0];
- xNum.value = xx+"°";//將所獲得距離數(shù)字賦值給input顯示旋轉(zhuǎn)角度
- yNum.value = yy+"°";
- //將旋轉(zhuǎn)角度寫入transform中
- box.style.transform = "rotatex("+xx+"deg) rotatey("+yy+"deg) rotatez(0deg)scale3d(0.7,0.7,0.7)";
- xArr[0] = e.clientX/2;
- yArr[0] = e.clientY/2;
- }
- };
- window.onmouseup = function () {//鼠標(biāo)抬起事件————用于清除鼠標(biāo)移動事件,
- body.style.cursor = 'url("img/openhand1.png"),auto';
- window.onmousemove = null;
- }
- }
- //點(diǎn)擊事件、負(fù)責(zé)立方體盒子的六面伸展
- function clickBox(){
- var btn = document.querySelector(".open");
- var box = document.querySelector(".box");
- var son = box.children;
- var value = 0;
- //存儲立方體散開時的transform參數(shù)
- var arr0 = ["rotatey(-90deg) translatex(-100px)","rotatey(90deg) translatex(100px)","rotatex(90deg) translatey(-100px)",<br>"rotatex(-90deg) translatey(100px)","translatez(-100px)","translatez(100px)"];
- //存儲立方體合并時的transform參數(shù)
- 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)"];
- btn.onclick = function(){
- if(value == 0){
- value = 1;
- btn.value = "點(diǎn)擊合并";
- for(var i=0;i<arr1.length;i++){
- son[i].style.transform = arr1[i];
- console.log(son[i])
- }
- }else if(value == 1){
- value = 0;
- btn.value = "點(diǎn)擊散開";
- for(var j=0;j<arr0.length;j++){
- son[j].style.transform = arr0[j];
- console.log(j);
- }
- }
- };
- }
- </script>
以上就是本文的全部內(nèi)容,希望對大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。
相關(guān)文章
純CSS3實現(xiàn)鼠標(biāo)滑過旋轉(zhuǎn)一定角度的導(dǎo)航菜單效果(無js)
純CSS3實現(xiàn)的旋轉(zhuǎn)導(dǎo)航菜單效果,無需任何JS,鼠標(biāo)滑過菜單項時,菜單旋轉(zhuǎn)一定的角度突出顯示2013-04-28CSS3實現(xiàn)的鼠標(biāo)懸停按鈕觸發(fā)式旋轉(zhuǎn)顯示提示語動畫特效源碼
是一段實現(xiàn)了將鼠標(biāo)懸停在按鈕上后,按鈕將立即進(jìn)行旋轉(zhuǎn)顯示更多提示語的效果代碼,本段代碼適應(yīng)于所有網(wǎng)頁使用,有需要的朋友們可以前來下載使用2016-05-05純CSS3實現(xiàn)的鼠標(biāo)懸停圖標(biāo)旋轉(zhuǎn)導(dǎo)航動畫特效源碼
是一款將鼠標(biāo)放在圖標(biāo)上時,該圖標(biāo)會立即產(chǎn)生旋轉(zhuǎn)動畫效果的代碼,本段代碼適應(yīng)于所有網(wǎng)頁使用,有興趣的朋友們可以前來下載使用2015-06-10一款基于jquery+css3實現(xiàn)的鼠標(biāo)懸停圖片旋轉(zhuǎn)變亮效果源碼
旋轉(zhuǎn)的效果主要靠css3來實現(xiàn),當(dāng)然還需要部分jquery代碼實現(xiàn)也就是當(dāng)鼠標(biāo)懸停后給li增加一class on即可并給on增加一個css3動畫效果即可,原理很簡單2015-02-04css實現(xiàn)的鼠標(biāo)懸停360度背景圖片做動畫旋轉(zhuǎn)效果
一款純css實現(xiàn)的一個鼠標(biāo)懸停背景圖片做動畫旋轉(zhuǎn)效果,摒棄掉煩人的js2014-04-03css3實現(xiàn)的鼠標(biāo)放上去旋轉(zhuǎn)效果按鈕
css3實現(xiàn)的鼠標(biāo)放上去旋轉(zhuǎn)效果按鈕,喜歡的朋友可以測試下2012-10-03純CSS3實現(xiàn)鼠標(biāo)滑過圓形圖片旋轉(zhuǎn)翻蓋動畫特效源碼
純CSS3實現(xiàn)鼠標(biāo)滑過圓形圖片旋轉(zhuǎn)翻蓋動畫特效源碼是一款效果非常炫酷的純CSS3圓形圖片鼠標(biāo)滑過旋轉(zhuǎn)翻蓋特效,本段代碼適應(yīng)于所有網(wǎng)頁使用,有需要的朋友可以直接下載使用2016-07-06