CSS3起步 09-------3D补充demo【多棱柱3D轮播旋转】及其原生js封装

html

<!DOCTYPE html>
<html>
	<head>
		<meta charset="utf-8">
		<title>多棱柱3D轮播旋转</title>
		<script type="text/javascript" src="js/多棱柱3D轮播旋转封装.js"></script>
		<style type="text/css">
			* {
				margin: 0;
				padding: 0;
			}

			html,
			body {
				height: 100%;
				overflow: hidden;
			}

			#wrap {
				position: absolute;
				left: 0;
				top: 0;
				right: 0;
				bottom: 0;
				margin: auto;
				width: 300px;
				height: 300px;
				box-sizing: border-box;
				border: 1px solid black;
				perspective: 150px;
			}

			#box>div {
				text-align: center;
				font: 100px/200px "宋体";
				background-color: pink;
			}

			#wrap:hover #box {
				transform: rotateY(360deg);
			}
		</style>
	</head>
	<body>
		<div id="wrap">
			<div id="box">
			</div>
		</div>
	</body>
</html>

js

onload = function() {
	create3Dwheel(200, 200, 13, 5);
}

function create3Dwheel(Width, Height, num, time) {
	let wrap, box, cssText, htmlText, cssNode, timer, rotateDeg, i;
	cssText = "";
	htmlText = "";
	rotateDeg = 0;
	wrap = document.querySelector('#wrap');
	box = document.querySelector('#box');
	//先自己创建个style节点
	cssNode = document.createElement("style");
	//设置盒子样式,定位基点
	cssText += "#box {" +
		"position: absolute;" +
		"left: 0;" +
		"top: 0;" +
		"right: 0;" +
		"bottom: 0;" +
		"margin: auto;"
		//后接单位的话,js不能存在空格
		+
		"width:" + (Width) + "px;" +
		"height:" + (Height) + "px;" +
		"transition:transform " + (time) + "s;" +
		"transform-style:preserve-3d;" +
		"transform-origin:center center -" + (Math.tan((num - 2) / (2 * num) * Math.PI) * Width / 2) + "px;" +
		"}"
	//设置面样式,定位基点
	cssText += "#box>div {" +
		"position: absolute;" +
		"width: " + (Width) + "px;" +
		"height: " + (Height) + "px;" +
		"transform-origin:center center -" + (Math.tan((num - 2) / (2 * num) * Math.PI) * Width / 2) + "px;" +
		"}"
	//创建面及设置每个面各自的旋转角度
	for (i = 0; i < num; i++) {
		htmlText += "<div>" + (i + 1) + "</div>";
		cssText += "#box>div:nth-child(" + (i + 1) + "){transform:rotateY(" + ((180 - 180 * (num - 2) / num) * i) +
			"deg);}"
	}
	cssNode.innerHTML = cssText;
	document.head.appendChild(cssNode);
	box.innerHTML = htmlText;
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值