百度360必应搜狗淘宝本站头条
当前位置:网站首页 > 技术文章 > 正文

基于css3新属性transform及原生js实现鼠标拖动3d立方体旋转

itomcoil 2025-01-05 18:05 23 浏览

基于css3新属性transform,实现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"/>
    <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: right;
 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: bottom;
 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 = 'url("img/closedhand1.png"),auto';
 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 = 'url("img/openhand1.png"),auto';
 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)",
"rotatex(-90deg) translatey(100px)","translatez(-100px)","translatez(100px)"]; //存储立方体合并时的transform参数 var arr1 = ["rotatey(-90deg) translatex(-100px)translatez(100px)","rotatey(90deg) translatex(100px)translatez(100px)",
"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>

相关推荐

selenium(WEB自动化工具)

定义解释Selenium是一个用于Web应用程序测试的工具。Selenium测试直接运行在浏览器中,就像真正的用户在操作一样。支持的浏览器包括IE(7,8,9,10,11),MozillaF...

开发利器丨如何使用ELK设计微服务中的日志收集方案?

【摘要】微服务各个组件的相关实践会涉及到工具,本文将会介绍微服务日常开发的一些利器,这些工具帮助我们构建更加健壮的微服务系统,并帮助排查解决微服务系统中的问题与性能瓶颈等。我们将重点介绍微服务架构中...

高并发系统设计:应对每秒数万QPS的架构策略

当面试官问及"如何应对每秒几万QPS(QueriesPerSecond)"时,大概率是想知道你对高并发系统设计的理解有多少。本文将深入探讨从基础设施到应用层面的解决方案。01、理解...

2025 年每个 JavaScript 开发者都应该了解的功能

大家好,很高兴又见面了,我是"高级前端进阶",由我带着大家一起关注前端前沿、深入前端底层技术,大家一起进步,也欢迎大家关注、点赞、收藏、转发。1.Iteratorhelpers开发者...

JavaScript Array 对象

Array对象Array对象用于在变量中存储多个值:varcars=["Saab","Volvo","BMW"];第一个数组元素的索引值为0,第二个索引值为1,以此类推。更多有...

Gemini 2.5编程全球霸榜,谷歌重回AI王座,神秘模型曝光,奥特曼迎战

刚刚,Gemini2.5Pro编程登顶,6美元性价比碾压Claude3.7Sonnet。不仅如此,谷歌还暗藏着更强的编程模型Dragontail,这次是要彻底翻盘了。谷歌,彻底打了一场漂亮的翻...

动力节点最新JavaScript教程(高级篇),深入学习JavaScript

JavaScript是一种运行在浏览器中的解释型编程语言,它的解释器被称为JavaScript引擎,是浏览器的一部分,JavaScript广泛用于浏览器客户端编程,通常JavaScript脚本是通过嵌...

一文看懂Kiro,其 Spec工作流秒杀Cursor,可移植至Claude Code

当Cursor的“即兴编程”开始拖累项目质量,AWS新晋IDEKiro以Spec工作流打出“先规范后编码”的系统工程思维:需求-设计-任务三件套一次生成,文档与代码同步落地,复杂项目不...

「晚安·好梦」努力只能及格,拼命才能优秀

欢迎光临,浏览之前点击上面的音乐放松一下心情吧!喜欢的话给小编一个关注呀!Effortscanonlypass,anddesperatelycanbeexcellent.努力只能及格...

JavaScript 中 some 与 every 方法的区别是什么?

大家好,很高兴又见面了,我是姜茶的编程笔记,我们一起学习前端相关领域技术,共同进步,也欢迎大家关注、点赞、收藏、转发,您的支持是我不断创作的动力在JavaScript中,Array.protot...

10个高效的Python爬虫框架,你用过几个?

小型爬虫需求,requests库+bs4库就能解决;大型爬虫数据,尤其涉及异步抓取、内容管理及后续扩展等功能时,就需要用到爬虫框架了。下面介绍了10个爬虫框架,大家可以学习使用!1.Scrapysc...

12个高效的Python爬虫框架,你用过几个?

实现爬虫技术的编程环境有很多种,Java、Python、C++等都可以用来爬虫。但很多人选择Python来写爬虫,为什么呢?因为Python确实很适合做爬虫,丰富的第三方库十分强大,简单几行代码便可实...

pip3 install pyspider报错问题解决

运行如下命令报错:>>>pip3installpyspider观察上面的报错问题,需要安装pycurl。是到这个网址:http://www.lfd.uci.edu/~gohlke...

PySpider框架的使用

PysiderPysider是一个国人用Python编写的、带有强大的WebUI的网络爬虫系统,它支持多种数据库、任务监控、项目管理、结果查看、URL去重等强大的功能。安装pip3inst...

「机器学习」神经网络的激活函数、并通过python实现激活函数

神经网络的激活函数、并通过python实现whatis激活函数感知机的网络结构如下:左图中,偏置b没有被画出来,如果要表示出b,可以像右图那样做。用数学式来表示感知机:上面这个数学式子可以被改写:...