首页 > web前端 > js教程 > 正文

使用 html css 和 javascript 的立方体错觉

Mary-Kate Olsen
发布: 2024-10-23 00:02:30
原创
575 人浏览过

cube illusion using html css and javascript

关注我们:https://www.instagram.com/webstreet_code/

代码

<!DOCTYPE html>
<html lang="en">
<head>
  <meta charset="UTF-8">
  <meta name="viewport" content="width=device-width, initial-scale=1.0">
  <title>3D Interactive Rubik's Cube</title>
  <style>
    * {
      margin: 0;
      padding: 0;
      box-sizing: border-box;
    }
    body {
      display: flex;
      justify-content: center;
      align-items: center;
      height: 100vh;
      background: #121212;
    }
    .scene {
      width: 300px;
      height: 300px;
      perspective: 1000px;
    }
    .cube {
      width: 100%;
      height: 100%;
      position: relative;
      transform-style: preserve-3d;
      transition: transform 1s ease;
    }
    .cube-face {
      position: absolute;
      display: grid;
      grid-template-columns: repeat(3, 1fr);
      grid-template-rows: repeat(3, 1fr);
      width: 300px;
      height: 300px;
      background: #fff;
      border: 2px solid #333;
      transform: translateZ(150px);
    }
    .cube-face div {
      border: 2px solid #d3caca;
    }
    /* Colors for each face */
    .front div { background: red; }
    .back div { background: orange; }
    .left div { background: blue; }
    .right div { background: green; }
    .top div { background: yellow; }
    .bottom div { background: white; }

    .cube-face.front { transform: rotateY(0deg) translateZ(150px); }
    .cube-face.back { transform: rotateY(180deg) translateZ(150px); }
    .cube-face.left { transform: rotateY(-90deg) translateZ(150px); }
    .cube-face.right { transform: rotateY(90deg) translateZ(150px); }
    .cube-face.top { transform: rotateX(90deg) translateZ(150px); }
    .cube-face.bottom { transform: rotateX(-90deg) translateZ(150px); }

    /* Cube rotation on hover */
    .scene:hover .cube {
      transform: rotateX(360deg) rotateY(360deg);
    }
  </style>
</head>
<body>
  <div class="scene">
    <div class="cube">
      <!-- 6 faces of the Rubik's Cube -->
      <div class="cube-face front">
        <div></div><div></div><div></div>
        <div></div><div></div><div></div>
        <div></div><div></div><div></div>
      </div>
      <div class="cube-face back">
        <div></div><div></div><div></div>
        <div></div><div></div><div></div>
        <div></div><div></div><div></div>
      </div>
      <div class="cube-face left">
        <div></div><div></div><div></div>
        <div></div><div></div><div></div>
        <div></div><div></div><div></div>
      </div>
      <div class="cube-face right">
        <div></div><div></div><div></div>
        <div></div><div></div><div></div>
        <div></div><div></div><div></div>
      </div>
      <div class="cube-face top">
        <div></div><div></div><div></div>
        <div></div><div></div><div></div>
        <div></div><div></div><div></div>
      </div>
      <div class="cube-face bottom">
        <div></div><div></div><div></div>
        <div></div><div></div><div></div>
        <div></div><div></div><div></div>
      </div>
    </div>
  </div>
</body>
</html>

登录后复制

以上是使用 html css 和 javascript 的立方体错觉的详细内容。更多信息请关注PHP中文网其他相关文章!

来源:dev.to
本站声明
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系admin@php.cn
作者最新文章
热门教程
更多>
最新下载
更多>
网站特效
网站源码
网站素材
前端模板