实现一个骰子

发布于 2023-05-03 20:15:33 字数 4115 浏览 57 评论 0

难点

  1. 六个面的变形,组成一个正方体
  2. 每个面点位的绘制

答案

<!DOCTYPE html>
<html lang="en">

<head>
  <meta charset="UTF-8" />
  <meta http-equiv="X-UA-Compatible" content="IE=edge" />
  <meta name="viewport" content="width=\, initial-scale=1.0" />
  <title>Document</title>
</head>
<style>
  .dice {
    position: relative;
    width: 100px;
    height: 100px;
    margin-top: 100px;
    margin-left: 100px;
    transform-style: preserve-3d;
    animation: roll 3s linear infinite;

  }

  .face {
    position: absolute;
    width: 100px;
    height: 100px;
    background: white;
    border: 1px solid black;
    box-sizing: border-box;
    transform-style: preserve-3d;
    padding: 10px;
  }

  .front {
    transform: translateZ(50px);
    display: flex;
    justify-content: center;
    align-items: center;
  }

  .back {
    transform: translateZ(-50px) rotateY(180deg);
  }

  .top {
    transform: rotateX(-90deg) translateZ(50px);
    display: flex;
    justify-content: space-between;
    align-items: center;
  }

  .bottom {
    transform: rotateX(90deg) translateZ(50px);
    display: flex;
    justify-content: space-between;

  }

  .left {
    transform: rotateY(-90deg) translateZ(50px);
    display: flex;
    justify-content: space-between;
  }

  .right {
    transform: rotateY(90deg) translateZ(50px);
    display: flex;
    justify-content: space-between;
  }

  .point {
    width: 20px;
    height: 20px;
    border-radius: 50%;
    background: #111;

  }

  .three-top {
    position: absolute;
    top: 10px;
    left: 10px;
  }

  .three-center {
    position: absolute;
    top: 50%;
    left: 50%;
    transform: translate(-50%, -50%);
  }

  .three-bottom {
    position: absolute;
    bottom: 10px;
    right: 10px;
  }

  .space-around {
    display: flex;
    flex-direction: column;
    justify-content: space-around;
  }

  .space-between {
    display: flex;
    flex-direction: column;
    justify-content: space-between;
  }

  .five-center {
    position: absolute;
    top: 50%;
    left: 50%;
    transform: translate(-50%, -50%);
  }


  @keyframes roll {
    0% {
      transform: rotateX(0deg) rotateY(0deg) rotateZ(0deg);
    }

    100% {
      transform: rotateX(360deg) rotateY(360deg) rotateZ(360deg);
    }
  }
</style>

<body>
  <div class="dice">
    <div class="face front">
      <div class="point"></div>
    </div>
    <div class="face back">
      <div class="point three-top"></div>
      <div class="point three-center"></div>
      <div class="point three-bottom"></div>
    </div>
    <div class="face top">
      <div class="point"></div>
      <div class="point"></div>
    </div>
    <div class="face bottom">
      <div class="space-around">
        <div class="point"></div>
        <div class="point"></div>
      </div>
      <div class="space-around">
        <div class="point"></div>
        <div class="point"></div>
      </div>

    </div>
    <div class="face left">
      <div class="space-between">
        <div class="point"></div>
        <div class="point"></div>
      </div>
      <div class="space-between">
        <div class="point"></div>
        <div class="point"></div>
      </div>
      <div class="point five-center"></div>

    </div>
    <div class="face right">
      <div class="space-around">
        <div class="point"></div>
        <div class="point"></div>
        <div class="point"></div>
      </div>
      <div class="space-around">
        <div class="point"></div>
        <div class="point"></div>
        <div class="point"></div>
      </div>

    </div>
  </div>
</body>

</html>

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。
列表为空,暂无数据

关于作者

0 文章
0 评论
23 人气
更多

推荐作者

qq_eQNo9e

文章 0 评论 0

内心旳酸楚

文章 0 评论 0

mb_BlPo2I8v

文章 0 评论 0

alipaysp_ZRaVhH1Dn

文章 0 评论 0

alipaysp_VP2a8Q4rgx

文章 0 评论 0

    我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
    原文