栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 软件开发 > Web开发 > Html/CSS > CSS教程

css3 transform及原生js实现鼠标拖动3D立方体旋转

CSS教程 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

css3 transform及原生js实现鼠标拖动3D立方体旋转

本文通过原生JS,点击事件,鼠标按下、鼠标抬起和鼠标移动事件,实现3D立方体的拖动旋转,并将旋转角度实时的反应至界面上显示。
 
实现原理:通过获取鼠标点击屏幕时的坐标和鼠标移动时的坐标,来获得鼠标在X轴、Y轴移动的距离,将距离实时赋值给transform属性。
 
从而通过改变transform:rotate属性值来达到3D立方体旋转的效果:
 
HTML代码块:

XML/HTML Code复制内容到剪贴板
  1.   
  2.       
  3.     //X轴旋转角度   
  4.     //Y轴旋转角度   
  5.       
  6.       
  7.           
  8.             1  
  9.             2  
  10.             3  
  11.             4  
  12.             5  
  13.             6  
  14.         
  
  •     
  •   
  •    
  • CSS代码块:

    CSS Code复制内容到剪贴板
    1.   
    2.  body{cursor: url("img/openhand1.png"),auto;}   
    3.      .big_box{   
    4.             width: 500px;   
    5.             height: 500px;   
    6.             margin: 200px auto;   
    7.         }   
    8.   
    9.         .box{   
    10.             -webkit-transform-style: preserve-3d;   
    11.             -moz-transform-style: preserve-3d;   
    12.             -ms-transform-style: preserve-3d;   
    13.             transform-style: preserve-3d;   
    14.             transform-origin:100px 100px 00px;   
    15.             position: relative;   
    16.             transform: rotatex(0deg) rotatey(0deg) rotatez(0deg)scale3d(0.7,0.7,0.7);   
    17.         }   
    18.         .box span{   
    19.             transition: all 1s linear;   
    20.   
    21.         }   
    22.         span{   
    23.             display: block;   
    24.             position: absolute;   
    25.             width: 200px;   
    26.             height: 200px;   
    27.             box-sizing: border-box;   
    28.             border:1px solid #999;   
    29.               
    30.             text-align: center;   
    31.             line-height: 200px;   
    32.             font-size: 60px;   
    33.             font-weight: 700;   
    34.             border-radius: 12%;   
    35.   
    36.         }   
    37.         .box span:nth-child(1){   
    38.             background-color: deepskyblue;   
    39.             transform-origin: left;   
    40.             transform: rotatey(-90deg) translatex(-100px);//左   
    41.         }   
    42.         .box span:nth-child(2){   
    43.             background-color: red;   
    44.             transform-origin: rightright;   
    45.             transform: rotatey(90deg) translatex(100px) ;//右   
    46.   
    47.         }   
    48.   
    49.         .box span:nth-child(3){   
    50.             background-color: green;   
    51.             transform-origin: top;   
    52.             transform: rotatex(90deg) translatey(-100px) ;//上   
    53.   
    54.         }   
    55.         .box span:nth-child(4){   
    56.             background-color: #6633FF;   
    57.             transform-origin: bottombottom;   
    58.             transform: rotatex(-90deg) translatey(100px);//下   
    59.         }   
    60.         .box span:nth-child(5){   
    61.             background-color: gold;   
    62.             transform: translatez(-100px);//后   
    63.         }   
    64.         .box span:nth-child(6){   
    65.   
    66.             background-color: #122b40;   
    67.             transform: translatez(100px);//前   
    68.         }   
    69.         .box:hover span{   
    70.   
    71.             opacity: 0.3;   
    72.         }   
    73.         .box:hover{   
    74.             animation-play-state:paused;//设置动画暂停   
    75.         }   
    76.     

    JS代码块:

    Javascript Code复制内容到剪贴板
    1.     
    2.   

    以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持考高分网。

    转载请注明:文章转载自 www.mshxw.com
    本文地址:https://www.mshxw.com/it/215828.html

    CSS教程相关栏目本月热门文章

    我们一直用心在做
    关于我们 文章归档 网站地图 联系我们

    版权所有 (c)2021-2022 MSHXW.COM

    ICP备案号:晋ICP备2021003244-6号