css多棱柱立体旋转

css多棱柱立体旋转

css代码:

    *{
	  margin:0;
	  padding:0
	}
	html,body{
	  height:100%;
	  overflow:hidden
	}
	#box{
	  width:450px;
	  height:450px;
	  border:1px solid;
	  position:absolute;
	  left:0;
	  top:0;
	  right:0;
	  bottom:0;
	  margin:auto;
	  perspective:300px;
	}
	#cube{
	  width:150px;
	  height:150px;
	  position:absolute;
	  left:0;
	  top:0;
	  right:0;
	  bottom:0;
	  margin:auto;
	  transition:2s transform;
	  transform-style:preserve-3d;
	}
	#cube > div{
	  width:150px;
	  height:150px;
	  border:1px solid blue;
	  box-sizing:border-box;
	  background:rgba(255,0,0,.3);
	  text-align:center;
	  font:60px/150px "微软雅黑";
	  position:absolute;
	  /* backface-visibility:hidden; */
	}
	#box:hover #cube{
	  transform:rotateY(360deg)
	}

HTML代码:

  <div id="box">
	<div id="cube">
	</div>
  </div>

JS代码:

	window.onload = function(){
		//棱数自己设置
		prism(6);
		function prism(n){
			var cube = document.getElementById('cube');
			var style = document.createElement('style');
			document.head.appendChild(style);
			var cssText = '';
			var div = '';
			for (var i = 0; i < n; i++) {
				div += '<div>'+(i+1)+'</div>';
				cssText += '#cube > div:nth-child(' + (i + 1) + '){transform:rotateY(' + (i * (360 / n)) + 'deg)}';
			}
			cube.innerHTML = div;
			var divNode = document.querySelector('#box > #cube > div');
			//棱长
			var cubeLength = divNode.offsetWidth;
			//外角度数
			var outDeg = 360 / n;
			//内角度数
			var inDeg = 180 - outDeg;
			cssText += '#cube{transform-origin:center center -' + ((cubeLength / 2) * Math.tan((inDeg / 2) * Math.PI / 180)) + 'px}';
			cssText += '#cube > div{transform-origin:center center -' + ((cubeLength / 2) * Math.tan((inDeg / 2) * Math.PI / 180)) + 'px}';
			style.innerHTML = cssText;
		}
	}

页面效果:
多棱柱

  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
以下是一个使用纯 CSS3 实现的圆柱立体图模板: HTML 代码: ```html <div class="cylinder"> <div class="top"></div> <div class="bottom"></div> <div class="body"></div> </div> ``` CSS 代码: ```css .cylinder { position: relative; width: 100px; height: 150px; transform-style: preserve-3d; transform: rotateX(-30deg) rotateY(45deg); } .top, .bottom { position: absolute; width: 100px; height: 100px; border-radius: 50%; background-color: #f00; transform-style: preserve-3d; transform: translateZ(50px); } .top { top: 0; } .bottom { bottom: 0; } .body { position: absolute; top: 50px; width: 100px; height: 50px; background-color: #f00; transform-style: preserve-3d; } .body:before, .body:after { content: ""; position: absolute; width: 100px; height: 50px; background-color: #f00; } .body:before { transform: rotateY(90deg) translateZ(50px) translateX(25px); } .body:after { transform: rotateY(90deg) translateZ(50px) translateX(-25px); } ``` 解释: - `.cylinder` 容器设置 `transform-style: preserve-3d`,表示其子元素可以在三维空间中变换,并且自身也能够被变换。 - `.top` 和 `.bottom` 是圆柱的两个圆面,使用 `border-radius: 50%` 实现圆形,设置 `transform: translateZ(50px)` 向上移动 50px,使其固定在圆柱的顶部和底部。 - `.body` 是圆柱的侧面,设置 `transform-style: preserve-3d` 表示其可以在三维空间中变换。使用 `:before` 和 `:after` 伪元素实现圆柱的左右两侧,通过 `transform: rotateY(90deg) translateZ(50px) translateX(25px)` 和 `transform: rotateY(90deg) translateZ(50px) translateX(-25px)` 分别使其相对于 `.body` 元素旋转90度,向左和向右移动25px,与 `.body` 元素组成完整的圆柱形状。

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值