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;
}