搜尋
首頁web前端css教學css3 transform及原生js實作滑鼠拖曳3D立方體旋轉的範例介紹

本文透過原生JS,點擊事件,滑鼠按下、滑鼠抬起和滑鼠移動事件,實現3D立方體的拖曳旋轉,並將旋轉角度即時的反應至介面上顯示。

 
實作原理:透過取得滑鼠點擊螢幕時的座標和滑鼠移動時的座標,來獲得滑鼠在X軸、Y軸移動的距離,將距離即時賦值給transform屬性。
 
從而透過改變transform:rotate屬性值來達到3D立方體旋轉的效果:
 
HTML程式碼區塊:

<body>
    <input type="button" class="open" value="点击散开"/>
    <input type="text" class="xNum" value="0"/>//X轴旋转角度   
    <input type="text" class="yNum" value="0"/>//Y轴旋转角度   
    <input type="text" class="zNum"/>
    <p class="big_box">
        <p class="box">
            <span>1</span>
            <span>2</span>
            <span>3</span>
            <span>4</span>
            <span>5</span>
            <span>6</span>
        </p>
    </p>
</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;//设置动画暂停   
        }   
</style>

JS程式碼區塊:

<script>   
    move();   

    clickBox();   

    //鼠标按下且移动时触发,

    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) {//鼠标按下事件
            body.style.cursor = &#39;url("img/closedhand1.png"),auto&#39;;   
            xArr[0] = e.clientX/2;//获取鼠标点击屏幕时的坐标
            yArr[0] = e.clientY/2;   
            window.onmousemove = function (e) {//鼠标移动事件————当鼠标按下且移动时触发
                xArr[1] = e.clientX/2;//获取鼠标移动时第一个点的坐标
                yArr[1] = e.clientY/2;   
                yy += xArr[1] - xArr[0];//获得鼠标移动的距离
                xx += yArr[1] - yArr[0];   
                xNum.value = xx+"°";//将所获得距离数字赋值给input显示旋转角度
                yNum.value = yy+"°";   
                //将旋转角度写入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 () {//鼠标抬起事件————用于清除鼠标移动事件,
            body.style.cursor = &#39;url("img/openhand1.png"),auto&#39;;   
            window.onmousemove = null;   
        }   
    }   
    //点击事件、负责立方体盒子的六面伸展
    function clickBox(){   
        var btn = document.querySelector(".open");   
        var box = document.querySelector(".box");   
        var son = box.children;   
        var value = 0;   
        //存储立方体散开时的transform参数
        var arr0 = ["rotatey(-90deg) translatex(-100px)","rotatey(90deg) translatex(100px)","rotatex(90deg) translatey(-100px)",<br>"rotatex(-90deg) translatey(100px)","translatez(-100px)","translatez(100px)"];   
        //存储立方体合并时的transform参数
        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 = "点击合并";   
                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 = "点击散开";   
                for(var j=0;j<arr0.length;j++){   
                    son[j].style.transform = arr0[j];   
                    console.log(j);   

                }   
            }   
        };   
    }   
</script>

#以上就是本文的全部內容,希望對大家的學習有所幫助,也希望大家多多支持PHP中文網。


以上是css3 transform及原生js實作滑鼠拖曳3D立方體旋轉的範例介紹的詳細內容。更多資訊請關注PHP中文網其他相關文章!

陳述
本文內容由網友自願投稿,版權歸原作者所有。本站不承擔相應的法律責任。如發現涉嫌抄襲或侵權的內容,請聯絡admin@php.cn
動畫粉絲的製作動畫粉絲的製作Apr 18, 2025 am 10:35 AM

切換選項卡時,這是您的眼睛首先要尋找的東西。

將您的域與Netlify託管網站一起使用將您的域與Netlify託管網站一起使用Apr 18, 2025 am 10:34 AM

Netlify有自己的自定義領域文檔,因此,如果您在此內容上尋找馬的技術文檔,則應將其視為

虛擬程式碼虛擬程式碼Apr 18, 2025 am 10:33 AM

Yonatan Doron不久前就媒體上寫了一篇文章,稱為“代碼藝術 - 為什麼要編寫更多偽代碼”。喜歡這個標題,作為偽代碼的粉絲

讓瑪麗·昆多組織的治療付出艱鉅的任務讓瑪麗·昆多組織的治療付出艱鉅的任務Apr 18, 2025 am 10:31 AM

我們生活在WebPack和NPM腳本的時代。好是壞,他們帶領捆綁和任務運行,以及一點點滾動,JSPM和GULP。但

從大鴻溝中分支從大鴻溝中分支Apr 18, 2025 am 10:27 AM

我喜歡一詞前端開發人員。如果您的擔憂是:

啟動網站的初學者旅程啟動網站的初學者旅程Apr 18, 2025 am 10:20 AM

在2018年9月,我學習Web開發的旅程僅幾個月了。正如我確定的許多新開發人員一樣,這是一項艱鉅的任務

所有新的ES2019技巧和技巧所有新的ES2019技巧和技巧Apr 18, 2025 am 10:19 AM

ES2019中添加了新功能,該標準再次更新了。現在在Node,Chrome,Firefox和Safari中正式提供

用線圈貨幣化(並刪除支持者的廣告)用線圈貨幣化(並刪除支持者的廣告)Apr 18, 2025 am 10:13 AM

過去,我過去嘗試過一些基於“小費的微付款”網站。他們來來去去。那很好。從發布者的角度來看,它

See all articles

熱AI工具

Undresser.AI Undress

Undresser.AI Undress

人工智慧驅動的應用程序,用於創建逼真的裸體照片

AI Clothes Remover

AI Clothes Remover

用於從照片中去除衣服的線上人工智慧工具。

Undress AI Tool

Undress AI Tool

免費脫衣圖片

Clothoff.io

Clothoff.io

AI脫衣器

AI Hentai Generator

AI Hentai Generator

免費產生 AI 無盡。

熱門文章

R.E.P.O.能量晶體解釋及其做什麼(黃色晶體)
1 個月前By尊渡假赌尊渡假赌尊渡假赌
R.E.P.O.最佳圖形設置
1 個月前By尊渡假赌尊渡假赌尊渡假赌
威爾R.E.P.O.有交叉遊戲嗎?
1 個月前By尊渡假赌尊渡假赌尊渡假赌

熱工具

MinGW - Minimalist GNU for Windows

MinGW - Minimalist GNU for Windows

這個專案正在遷移到osdn.net/projects/mingw的過程中,你可以繼續在那裡關注我們。 MinGW:GNU編譯器集合(GCC)的本機Windows移植版本,可自由分發的導入函式庫和用於建置本機Windows應用程式的頭檔;包括對MSVC執行時間的擴展,以支援C99功能。 MinGW的所有軟體都可以在64位元Windows平台上運作。

SublimeText3 英文版

SublimeText3 英文版

推薦:為Win版本,支援程式碼提示!

SublimeText3漢化版

SublimeText3漢化版

中文版,非常好用

SAP NetWeaver Server Adapter for Eclipse

SAP NetWeaver Server Adapter for Eclipse

將Eclipse與SAP NetWeaver應用伺服器整合。

PhpStorm Mac 版本

PhpStorm Mac 版本

最新(2018.2.1 )專業的PHP整合開發工具