js實現(xiàn)可旋轉(zhuǎn)的立方體模型
更新時間:2016年10月16日 12:52:08 投稿:hebedich
這里給大家分享的是通過js腳本來控制頁面中的正方體轉(zhuǎn)動特效,用戶可以點擊按鈕向右轉(zhuǎn)動,也可以向下轉(zhuǎn)動,結(jié)合自己的需求控制即可。效果非常棒,這里推薦給大家
這是一個簡單的立方體應用,他是很多立方旋轉(zhuǎn)變換的基礎,例如實現(xiàn)3D輪播圖的實現(xiàn)等。
<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8"> <title>可旋轉(zhuǎn)立方體效果</title> <style type="text/css"> *{padding: 0; margin: 0} img{border: 0} ul li{list-style: none} ul{width: 200px; height: 200px; margin: 100px auto; position: relative; -webkit-transform-style: preserve-3d ; /* perspective: 100px;*/ } /*繞中心旋轉(zhuǎn),坐標軸會隨旋轉(zhuǎn)而旋轉(zhuǎn)*/ li{width:200px;height:200px;position:absolute;text-align:center;line-height:200px;font-size:80px;font-weight:bold;color:#fff;} /*構(gòu)造六個面*/ li:nth-child(1){background:rgba(255,0,0,1);-webkit-transform:rotateX(90deg) translateZ(100px);} li:nth-child(2){background:rgba(0,255,255,1);-webkit-transform:rotateX(270deg) translateZ(100px);} li:nth-child(3){background:rgba(255,0,255,1);-webkit-transform:rotateY(90deg) translateZ(100px);} li:nth-child(4){background:rgba(0,255,0,1);-webkit-transform:rotateY(270deg) translateZ(100px);} li:nth-child(5){background:rgba(200,200,0,1);-webkit-transform:translateZ(-100px);} li:nth-child(6){background:rgba(0,0,255,1);-webkit-transform: translateZ(100px) ;} .button{ width: 200px; margin: 20px auto; position: relative; cursor: pointer; } input{ width: 50px; height: 30px; position: absolute; cursor: pointer; } /*按鈕的絕對定位*/ input:nth-child(1){left: 100px; top: 0} input:nth-child(2){left:200px;top:50px;} input:nth-child(3){left:0px;top:50px;} input:nth-child(4){left:100px;top:100px;} input:nth-child(5){left:100px;top:50px;} </style> <script type="text/javascript"> window.onload = function () { var x = 0, y = 0; var ul = document.getElementById('ul'); var inputs = document.getElementsByTagName('input'); for (var i = 0; i<inputs.length; i++){ inputs[i].onclick = run; } function run() { /*漸變*/ ul.style.webkitTransition = '-webkit-transform 3s linear'; ul.style.oTransition = '-o-transform 3s linear'; ul.style.transition = 'transform 3s linear'; /*旋轉(zhuǎn)的規(guī)則,就是x,y方向的deg改變*/ if(inputs[0]==this){x+=90;} if(inputs[1]==this){y+=90;} if(inputs[2]==this){y-=90;} if(inputs[3]==this){x-=90;} if (inputs[4] == this){ x = 0; y = 0; ul.style.webkitTransition = '-webkit-transform .1s linear'; ul.style.oTransition = '-o-transform .1s linear'; ul.style.transition = 'transform .1s linear'; } ul.style.webkitTransform = 'rotateX(' + x + 'deg) rotateY(' + y + 'deg)'; ul.style.oTransform = 'rotateX(' + x + 'deg) rotateY(' + y + 'deg)'; ul.style.transform = 'rotateX(' + x + 'deg) rotateY(' + y + 'deg)'; } document.addEventListener('keydown', function(e){ ul.style.webkitTransition='-webkit-transform 3s linear'; switch(e.keyCode){ case 37: y -= 90; //左箭頭 break; case 38: x += 90; //上箭頭 break; case 39: y += 90; //下箭頭 break; case 40: x -= 90; //右箭頭 break; case 13: x=0; y=0; //回車 (當回車時,迅速轉(zhuǎn)回初始狀態(tài)) ul.style.webkitTransition='-webkit-transform 0.1s linear'; break; } ul.style.webkitTransform = "rotateX("+x+"deg) rotateY("+y+"deg)"; //變換效果(沿X軸和Y軸旋轉(zhuǎn)) }, false);document.addEventListener("keydown", function (e) { ul.style.webkitTransition = '-webkit-transform 3s linear'; ul.style.oTransition = '-o-transform 3s linear'; ul.style.transition = 'transform 3s linear'; switch(e.keyCode){ case 37: } }) /* function run(){ ul.style.webkitTransition='-webkit-transform 3s linear'; //設置立方體變換的屬性、持續(xù)時間、動畫類型 if(inputs[0]==this){x+=90;} if(inputs[1]==this){y+=90;} if(inputs[2]==this){y-=90;} if(inputs[3]==this){x-=90;} if(inputs[4]==this){x=0;y=0; ul.style.webkitTransition='-webkit-transform 0.1s linear';} //當點擊重置按鈕時,迅速轉(zhuǎn)回到初始狀態(tài)。 ul.style.webkitTransform = "rotateX("+x+"deg) rotateY("+y+"deg)"; //變換效果(沿X軸和Y軸旋轉(zhuǎn)) }*/ } </script> </head> <body> <ul id="ul"> <li>1</li> <li>2</li> <li>3</li> <li>4</li> <li>5</li> <li>6</li> </ul> <div class="container"> </div> <div class="button"> <input type="button" value="上"> <input type="button" value="右"> <input type="button" value="左"> <input type="button" value="下"> <input type="button" value="重置"> </div> </body> </html>
相關文章
EasyUI的DataGrid綁定Json數(shù)據(jù)源的示例代碼
本篇文章主要介紹了EasyUI的DataGrid綁定Json數(shù)據(jù)源的示例代碼,小編覺得挺不錯的,現(xiàn)在分享給大家,也給大家做個參考。一起跟隨小編過來看看吧2017-12-12談談我對JavaScript原型和閉包系列理解(隨手筆記6)
這篇文章主要介紹我對JavaScript原型和閉包系列理解(隨手筆記6)的相關資料,需要的朋友可以參考下2015-12-12前端使用JavaScript結(jié)合CSS實現(xiàn)3D旋轉(zhuǎn)跟隨鼠標變化
這篇文章主要介紹了前端使用JavaScript結(jié)合CSS實現(xiàn)3D旋轉(zhuǎn)跟隨鼠標變化,文中通過示例代碼介紹的非常詳細,對大家的學習或者工作具有一定的參考學習價值,需要的朋友們下面隨著小編來一起學習吧2023-01-01