Home Web Front-end CSS Tutorial Example introduction of css3 transform and native js to realize mouse dragging 3D cube rotation

Example introduction of css3 transform and native js to realize mouse dragging 3D cube rotation

Mar 08, 2017 pm 03:29 PM

This article uses native JS, click events, mouse press, mouse lift and mouse move events to realize the drag and rotation of the 3D cube, and reflect the rotation angle to the interface in real time.


Implementation principle: By obtaining the coordinates when the mouse clicks on the screen and the coordinates when the mouse moves, the distance moved by the mouse on the X-axis and Y-axis is obtained. The distance is assigned to the transform attribute in real time.

Thus, the effect of 3D cube rotation can be achieved by changing the transform:rotate attribute value:

HTML code block:

<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>
Copy after login

CSS code block:

<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>
Copy after login

JS code block:

<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>
Copy after login

The above is the entire content of this article. I hope it will be helpful to everyone’s learning and I hope you will support me a lot. PHP Chinese website.

The above is the detailed content of Example introduction of css3 transform and native js to realize mouse dragging 3D cube rotation. For more information, please follow other related articles on the PHP Chinese website!

Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

Video Face Swap

Video Face Swap

Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Tools

Notepad++7.3.1

Notepad++7.3.1

Easy-to-use and free code editor

SublimeText3 Chinese version

SublimeText3 Chinese version

Chinese version, very easy to use

Zend Studio 13.0.1

Zend Studio 13.0.1

Powerful PHP integrated development environment

Dreamweaver CS6

Dreamweaver CS6

Visual web development tools

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)

Vue 3 Vue 3 Apr 02, 2025 pm 06:32 PM

It&#039;s out! Congrats to the Vue team for getting it done, I know it was a massive effort and a long time coming. All new docs, as well.

Building an Ethereum app using Redwood.js and Fauna Building an Ethereum app using Redwood.js and Fauna Mar 28, 2025 am 09:18 AM

With the recent climb of Bitcoin’s price over 20k $USD, and to it recently breaking 30k, I thought it’s worth taking a deep dive back into creating Ethereum

Can you get valid CSS property values from the browser? Can you get valid CSS property values from the browser? Apr 02, 2025 pm 06:17 PM

I had someone write in with this very legit question. Lea just blogged about how you can get valid CSS properties themselves from the browser. That&#039;s like this.

Stacked Cards with Sticky Positioning and a Dash of Sass Stacked Cards with Sticky Positioning and a Dash of Sass Apr 03, 2025 am 10:30 AM

The other day, I spotted this particularly lovely bit from Corey Ginnivan’s website where a collection of cards stack on top of one another as you scroll.

A bit on ci/cd A bit on ci/cd Apr 02, 2025 pm 06:21 PM

I&#039;d say "website" fits better than "mobile app" but I like this framing from Max Lynch:

Comparing Browsers for Responsive Design Comparing Browsers for Responsive Design Apr 02, 2025 pm 06:25 PM

There are a number of these desktop apps where the goal is showing your site at different dimensions all at the same time. So you can, for example, be writing

Using Markdown and Localization in the WordPress Block Editor Using Markdown and Localization in the WordPress Block Editor Apr 02, 2025 am 04:27 AM

If we need to show documentation to the user directly in the WordPress editor, what is the best way to do it?

Why are the purple slashed areas in the Flex layout mistakenly considered 'overflow space'? Why are the purple slashed areas in the Flex layout mistakenly considered 'overflow space'? Apr 05, 2025 pm 05:51 PM

Questions about purple slash areas in Flex layouts When using Flex layouts, you may encounter some confusing phenomena, such as in the developer tools (d...

See all articles