实现一个骰子
难点
- 六个面的变形,组成一个正方体
- 每个面点位的绘制
答案
<!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 技术交流群。
上一篇: CSS 多行文字截断
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
更多
发布评论