首頁 >web前端 >js教程 >使用 html css 和 javascript 的立方體錯覺

使用 html css 和 javascript 的立方體錯覺

Mary-Kate Olsen
Mary-Kate Olsen原創
2024-10-23 00:02:30598瀏覽

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中文網其他相關文章!

陳述:
本文內容由網友自願投稿,版權歸原作者所有。本站不承擔相應的法律責任。如發現涉嫌抄襲或侵權的內容,請聯絡admin@php.cn