CSS3 realizes draggable Rubik's Cube 3D effect

CSS3 realizes draggable Rubik's Cube 3D effect

Mainly used knowledge points:

•css3 3d transformations •native js mouse drag events

•display:grid layout

Functions implemented

• 3D Rubik's Cube can be clicked and dragged

• See the effect directly

html:

<div class="container">
        <div class="box default">
            <div class="pic"><img src="./img/cat.jpg" alt=""></div>
            <div class="pic"><img src="./img/dog.jpg" alt=""></div>
            <div class="pic"><img src="./img/elephant.jpg" alt=""></div>
            <div class="pic"><img src="./img/lion.jpg" alt=""></div>
            <div class="pic"><img src="./img/rabbit.jpg" alt=""></div>
            <div class="pic"><img src="./img/monkey.jpg" alt=""></div>
        </div>
    </div>
    <h1>Click the image button below to switch</h1>
    <div class="btn">
        <input type="image" class="1" src="./img/cat.jpg">
        <input type="image" class="2" src="./img/dog.jpg">
        <input type="image" class="3" src="./img/elephant.jpg">
        <input type="image" class="4" src="./img/lion.jpg">
        <input type="image" class="5" src="./img/rabbit.jpg">
        <input type="image" class="6" src="./img/monkey.jpg">
    </div>

CSS:

   * {
        margin: 0;
        padding: 0;
    }
    html,
    body {
        width: 100%;
        height: 100%;
        background: #66677c;
        text-align: center;
    }
    .container {
        width: 300px;
        height: 300px;
        margin: 50px auto 150px;
        perspective: 1200px;
    }
    .container .box {
        width: 300px;
        height: 300px;
        position: relative;
        transform-style: preserve-3d;
        transition: transform 0.5s;
    }
    .container .box .pic {
        position: absolute;
        left: 0;
        top: 0;
        width: 300px;
        height: 300px;
        box-shadow: 0px 0px 5px #fff;
    }
    .container .box .pic img {
        width: 100%;
        height: 100%;
        cursor: pointer;
    }
    .container .box .pic:nth-child(1) {
        transform: translateZ(150px);
    }
    .container .box .pic:nth-child(2) {
        transform: rotateY(-180deg) translateZ(150px);
    }
    .container .box .pic:nth-child(3) {
        transform: rotateY(90deg) translateZ(150px);
    }
    .container .box .pic:nth-child(4) {
        transform: rotateY(-90deg) translateZ(150px);
    }
    .container .box .pic:nth-child(5) {
        transform: rotateX(90deg) translateZ(150px);
    }
    .container .box .pic:nth-child(6) {
        transform: rotateX(-90deg) translateZ(150px);
    }
    h1 {
        color: #fff;
        font-size: 30px;
        margin-bottom: 30px;
    }
    .btn {
        display: grid;
        justify-content: center;
        grid-template-columns: 100px 100px 100px;
        grid-template-rows: 100px 100px;
        grid-gap: 15px;
    }
    .btn input {
        width: 100px;
        height: 100px;
        outline: none;
        border: 2px solid #fff;
    }
    .btn input:focus {
        border: 2px solid #e70;
    }
    .default {
        transform: translateZ(-150px) rotateX(-10deg) rotateY(15deg);
    }
    .image1 {
        transform: translateZ(-150px) rotateX(0deg) rotateY(0deg);
    }
    .image2 {
        transform: translateZ(-150px) rotateY(-180deg);
    }
    .image3 {
        transform: translateZ(-150px) rotateY(-90deg);
    }
    .image4 {
        transform: translateZ(-150px) rotateY(90deg);
    }
    .image5 {
        transform: translateZ(-150px) rotateX(-90deg);
    }
    .image6 {
        transform: translateZ(-150px) rotateX(90deg);
    }

js:

 (function(){
        var btn = document.getElementsByClassName('btn')[0];
        var box = document.getElementsByClassName('box')[0];
        btn.addEventListener('click',function(e){
            var className = e.target.className;
            if(className !== 'btn'){
                box.style = '';
                box.classList.replace(box.classList[1],'image'+className);
            }
        })
        //Mouse drag effect var xN = 10, yN = 15;
        document.addEventListener('mousedown',function(e){
            e.preventDefault();
            e.stopPropagation();
            var x = e.clientX;
            var y = e.clientY;
            document.addEventListener('mousemove',move);
            document.addEventListener('mouseup', up);
            function move(e){
                e.preventDefault();
                e.stopPropagation();
                var x1 = e.clientX;
                var y1 = e.clientY;
                xN += (x1 - x)*0.04;
                yN += (y1 - y)*0.04;
                box.style.transform = 'translateZ(-150px) rotateY(' + xN + 'deg) rotateX(' + -yN + 'deg)';
            }
            function up(){
                document.removeEventListener('mousemove', move);
            }
        })
    })()

Summarize

The above is the introduction of the draggable Rubik's Cube 3D effect based on CSS3. I hope it will be helpful to you. If you have any questions, please leave me a message and I will reply to you in time. I would also like to thank everyone for their support of the 123WORDPRESS.COM website! If you find this article helpful, please feel free to reprint it and please indicate the source. Thank you!

<<:  Design Theory: A Method to Understand People's Hearts

>>:  Detailed steps to build the TypeScript environment and deploy it to VSCode

Recommend

20 JS abbreviation skills to improve work efficiency

Table of contents When declaring multiple variabl...

HTML5+CSS3 header creation example and update

Last time, we came up with two header layouts, on...

Tutorial on installing Android Studio on Ubuntu 19 and below

Based on past experience, taking notes after comp...

JS code to achieve page switching effect

This article example shares the specific code of ...

Basic operations of mysql learning notes table

Create Table create table table name create table...

Detailed explanation of Vue life cycle functions

Table of contents Lifecycle Functions Common life...

How to change the encoding to utf-8 in mysql version 5.7 under windows

Preface I just started learning MySQL and downloa...

Tutorial on installing Ubuntu 20.04 and NVIDIA drivers

Install Ubuntu 20.04 Install NVIDIA drivers Confi...

CentOS 7.9 installation and configuration process of zabbix5.0.14

Table of contents 1. Basic environment configurat...

In-depth understanding of MySQL global locks and table locks

Preface According to the scope of locking, locks ...