如何用HTML5写一个旋转立方体

``### 当鼠标放在上面旋转
在这里插入图片描述

代码如下

<!DOCTYPE HTML>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8">
<title>立方体</title>
<style type="text/css">
* {margin:0; padding:0;}
ul li {list-style:none;}
.box {width:100%; height:100%;  margin:200px auto; position:relative; perspective:800px;}
	.box ul {width:300px; height:300px; position:absolute;left:0; top:300px; bottom:0; right:0; margin:auto; transform-style:preserve-3d; transition:all 10s; }
	.box ul:hover {transform:rotateX(360deg) rotateY(360deg);}
	.box ul li {width:300px; height:300px; text-align:center; line-height:300px; position:absolute; font-size:48px; backface-visibility:hidden;}
	.box ul li:nth-child(1) {background:rgba(255,0,0,.5); transform:translateY(-150px) rotateX(90deg);}
	.box ul li:nth-child(2) {background:rgba(0,255,0,.5); transform:translateY(150px) rotateX(-90deg);}
	.box ul li:nth-child(3) {background:rgba(0,0,255,.5); transform:translateX(-150px) rotateY(-90deg);}
	.box ul li:nth-child(4) {background:rgba(0,155,255,.5); transform:translateX(150px) rotateY(90deg);}
	.box ul li:nth-child(5) {background:rgba(255,0,255,.5);transform: 
	 translateZ(150px);}
	 .box ul li:nth-child(6) {background:rgba(255,255,0,.5); transform:translateZ(-150px) rotateY(180deg);}
</style>
</head>

<body>
<div class="box">
    <ul>
        <li>上</li>
        <li>下</li>
        <li>左</li>
        <li>右</li>
        <li>前</li>
        <li>后</li>
    </ul>
</div>
</body>
</html>
  • 3
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
可以使用CSS3的3D变换来实现一个旋转立方体。下面是一个简单的例子: HTML代码: ```html <div class="cube"> <div class="face front">1</div> <div class="face back">2</div> <div class="face left">3</div> <div class="face right">4</div> <div class="face top">5</div> <div class="face bottom">6</div> </div> ``` CSS代码: ```css .cube { width: 200px; height: 200px; position: relative; transform-style: preserve-3d; animation: rotate 3s linear infinite; } .face { position: absolute; width: 200px; height: 200px; text-align: center; line-height: 200px; font-size: 100px; } .front { transform: translateZ(100px); background-color: red; } .back { transform: translateZ(-100px) rotateY(180deg); background-color: blue; } .left { transform: translateX(-100px) rotateY(-90deg); background-color: green; } .right { transform: translateX(100px) rotateY(90deg); background-color: yellow; } .top { transform: translateY(-100px) rotateX(90deg); background-color: purple; } .bottom { transform: translateY(100px) rotateX(-90deg); background-color: orange; } @keyframes rotate { 0% { transform: rotateY(0deg) rotateX(0deg) rotateZ(0deg); } 100% { transform: rotateY(360deg) rotateX(360deg) rotateZ(360deg); } } ``` 解释: - `.cube`定义了立方体的大小和3D变换,同时添加了一个旋转动画。 - `.face`定义了每个面的大小和样式。 - `.front`、`.back`、`.left`、`.right`、`.top`和`.bottom`分别表示立方体的前、后、左、右、上、下面,使用不同的3D变换来定位每个面,并且设置了不同的颜色。 - `@keyframes rotate`定义了旋转动画,从0%到100%逐渐旋转立方体。 你可以在浏览器中查看这个例子的效果。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值