首页 > 代码库 > CSS实现正方体旋转
CSS实现正方体旋转
代码如下:
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>CSS3 星空旋转正方体</title>
<style type="text/css">
body{
background-image:linear-gradient(to top,blue,#000);
}
*{
padding: 0;
margin:0;
}
#Mypage{
/*-webkit-perspective:600px;
-webkit-perspective-origin:50% 50%;*/
-webkit-transform-style:preserve-3d;
position: relative;
width: 400px;
height: 400px;
margin:100px auto;
}
#Mypage li{
list-style:none;
}
.pages{
position: absolute;
width:400px;
height:400px;
}
#page1{
background-color: #FF0088;
-webkit-transform:translateZ(200px);
}
#page2{
background-color: #FFFF00;
-webkit-transform:rotateX(90deg) translateZ(200px);
}
#page3{
background-color: #00FF00;
-webkit-transform:rotateX(270deg) translateZ(200px);
}
#page4{
background-color: #00FFFF;
-webkit-transform:rotateY(90deg) translateZ(200px);
}
#page5{
background-color: #7700FF;
-webkit-transform:rotateY(270deg) translateZ(200px);
}
#page6{
background-color: #FF00FF;
-webkit-transform:translateZ(-200px);
}
#closth{
margin: 0 auto;
font-size: 18px;
text-align: center;
padding-top: 100px;
float: left;
}
#closth div{
padding-top: 20px;
margin-bottom: 15px;
}
#closth div a{
color: #fff;
margin-right: 40px;
}
#heard{
width: 80%;
margin: 0 auto;
margin-bottom: 300px;
}
#Opage{
margin-top: 20px;
}
.wrap:first-child{
margin-left: 400px;
}
.wrap{
float: left;
list-style: none outside none;
padding-right: 40px;
}
.wrap a{
color: #fff;
}
</style>
</head>
<body>
<div id="heard">
<div id="closth">
<div><a href="javascript:tops()">上翻</a></div>
<div><a href="javascript:lefts()">左翻</a><a href="javascript:cleanth()">重置</a><a href="javascript:rights()">右翻</a></div>
<div><a href="javascript:bottoms()">下翻</a></div>
</div>
<ul id="Mypage">
<li class="pages" id="page1"><img width="400px" height="400px"></li>
<li class="pages" id="page2"><img width="400px" height="400px"></li>
<li class="pages" id="page3"><img width="400px" height="400px"></li>
<li class="pages" id="page4"><img width="400px" height="400px"></li>
<li class="pages" id="page5"><img width="400px" height="400px"></li>
<li class="pages" id="page6"><img width="400px" height="400px"></li>
</ul>
<ul id="Opage">
<li class="wrap"><a href="javascript:ones()">1</a></li>
<li class="wrap"><a href="javascript:twos()">2</a></li>
<li class="wrap"><a href="javascript:threes()">3</a></li>
<li class="wrap"><a href="javascript:fours()">4</a></li>
<li class="wrap"><a href="javascript:fives()">5</a></li>
<li class="wrap"><a href="javascript:sixs()">6</a></li>
</ul>
</div>
<script type="text/javascript">
var x=0;
var y=0;
var n,m=0;
function cleanth(){
x=0;
y=0;
n=0;
m=0;
var curpage=document.getElementById(‘Mypage‘);
curpage.style.transition=‘-webkit-transform 20ms linear‘;
curpage.style.transform="rotateX("+x+"deg) rotateY("+y+"deg)";
}
function count(){
if(x%360==0){
n=x/360;
}
if(y%360==0){
m=y/360;
}
}
function tops()
{
count();
x+=90;
var curpage=document.getElementById(‘Mypage‘);
curpage.style.transition=‘-webkit-transform 3s linear‘;
curpage.style.transform="rotateX("+x+"deg) rotateY("+y+"deg)";
}
function bottoms(){
count();
x-=90;
var curpage=document.getElementById(‘Mypage‘);
curpage.style.transition=‘-webkit-transform 3s linear‘;
curpage.style.transform="rotateX("+x+"deg) rotateY("+y+"deg)";
}
function lefts(){
count();
y+=90;
var curpage=document.getElementById(‘Mypage‘);
curpage.style.transition=‘-webkit-transform 3s linear‘;
curpage.style.transform="rotateX("+x+"deg) rotateY("+y+"deg)";
}
function rights(){
count();
y-=90;
var curpage=document.getElementById(‘Mypage‘);
curpage.style.transition=‘-webkit-transform 3s linear‘;
curpage.style.transform="rotateX("+x+"deg) rotateY("+y+"deg)";
}
function ones(){
count();
x=0+n*360;
y=0+m*360;
var curpage=document.getElementById(‘Mypage‘);
curpage.style.transition=‘-webkit-transform 3s linear‘;
curpage.style.transform="rotateX("+x+"deg) rotateY("+y+"deg)";
}
function twos(){
count();
x=-90+n*360;
y=m*360+0;
var curpage=document.getElementById(‘Mypage‘);
curpage.style.transition=‘-webkit-transform 3s linear‘;
curpage.style.transform="rotateX("+x+"deg) rotateY("+y+"deg)";
}
function threes(){
count();
x=90+n*360;
y=0+m*360;
var curpage=document.getElementById(‘Mypage‘);
curpage.style.transition=‘-webkit-transform 3s linear‘;
curpage.style.transform="rotateX("+x+"deg) rotateY("+y+"deg)";
}
function fours(){
count();
x=0+n*360;
y=m*360-90;
var curpage=document.getElementById(‘Mypage‘);
curpage.style.transition=‘-webkit-transform 3s linear‘;
curpage.style.transform="rotateX("+x+"deg) rotateY("+y+"deg)";
}
function fives(){
count();
x=0+n*360;
y=90+m*360;
var curpage=document.getElementById(‘Mypage‘);
curpage.style.transition=‘-webkit-transform 3s linear‘;
curpage.style.transform="rotateX("+x+"deg) rotateY("+y+"deg)";
}
function sixs(){
count();
x=180+n*360;
y=0+m*360;
var curpage=document.getElementById(‘Mypage‘);
curpage.style.transition=‘-webkit-transform 3s linear‘;
curpage.style.transform="rotateX("+x+"deg) rotateY("+y+"deg)";
}
</script>
</body>
</html>
CSS实现正方体旋转