0

我一直在尝试创建一个可以在 x 轴上旋转的框,以在 4 面的每一面显示几个不同的图片。但是,我似乎无法让我的代码正常工作。这里是:

<style type="text/css">

#cube {
position:relative;
left:20px;
top:20px;
height:300px;
width:300px;
-webkit-transition: -webkit-transform 1s linear;
}

.side {
position:absolute;  
height:300px;
width:300px;
}

.one {
background-color:green;
-webkit-transform:rotateX(0deg) translateZ(150px); 
}

.two {
background-color:grey;
-webkit-transform:rotateX(90deg) translateZ(150px); 
}

.three {
background-color:red;
-webkit-transform:rotateX(180deg)  translateZ(150px); 
}

.four {
background-color:black;
-webkit-transform:rotateX(270deg) translateZ(150px); 
}

#turn {
height:30px;
width:30px;
background-color:green;
margin:30px;
}

</style>
<script type="text/javascript">

q = 1
deg = 0
function turn() {

var cube = document.getElementById('one');
deg = deg + 90

if( q == 1 ) {
cube.style.cssText = "-webkit-transform:rotateY(" + (deg) + "deg);";
q += 1;
console.log("left" + (deg));}
else if( q == 2 ) {
cube.style.cssText = "-webkit-transform:rotateY(" + (deg) + "deg);";
q += 1;
console.log("right" + (deg));}
else if( q == 3 ) {
cube.style.cssText = "-webkit-transform:rotateY(" + (deg) + "deg);";
q += 1;
console.log("back" + (deg));}
else if ( q == 4 ) {
cube.style.cssText = "-webkit-transform:rotateY(" + (deg) + "deg);";
q = 1;
console.log("front" + (deg));}

}

</script>
</head>
<body>

<div id="cube">
<div id ="one" class="side two"></div>
<div id="two" class="side three"></div>
<div id="three" class="side four"></div>
<div id="four" class="side one"></div>
</div>

<div id="turn" onclick="turn()"></div>

</body>

关于我如何无法让这个脚本工作的任何想法?

4

2 回答 2

0

您的代码中缺少几个概念。

  • CSS 类与 CSS ID
  • 透视 div 容器
  • 正确的“墙”div 定位
  • 适当的事件处理

我的 JSFiddle

  • 将事件处理程序移至 javascript 以使其更合规

http://jsfiddle.net/sw7aV/2/

我的 HTML

<div id="turn" onclick="turn()"></div>
<div id="container">
    <div id="cube" class="sprite">
        <div class="wall" id="front">front</div>
        <div class="wall" id="back">back</div>
        <div class="wall" id="top">top</div>
        <div class="wall" id="bottom">bottom</div>
        <div class="wall" id="left">left</div>
        <div class="wall" id="right">right</div>
    </div>
</div>

我的 CSS

#container {
    float: left;
    position: relative;
    width: 300px;
    height: 300px;
    overflow: hidden;
    border: 1px solid #CCCCCC;

    -webkit-perspective: 900;
    -moz-perspective: 900;
    -o-perspective: 900;
    perspective: 900;
}

.sprite {
    width: 100%;
    height: 100%;
    position: absolute;

    text-indent: 0px;

    -webkit-transform:  rotateY(0deg);
    -moz-transform:  rotateY(0deg);
    -o-transform:  rotateY(0deg);
    transform:  rotateY(0deg);

    -webkit-transform-style: preserve-3d;
    -moz-transform-style: preserve-3d;
    -o-transform-style: preserve-3d;
    transform-style: preserve-3d;
}

#cube {
    width: 300px;
    height: 300px;
    -webkit-transition: -webkit-transform 1s linear;
}

#cube .wall {
    display: block;
    position: absolute;
    height: 100%;
    width: 257px;
    -webkit-transform-style: preserve-3d;
    -moz-transform-style: preserve-3d;
    -o-transform-style: preserve-3d;
    transform-style: preserve-3d;
    -webkit-background-origin: content-box;
    -moz-background-origin: content-box;
    -o-background-origin: content-box;
    background-origin: content-box;
    border: 1px solid #DEDEDE;
    color: white;
}

#cube #front {
    -webkit-transform: rotateX(0deg) rotateY(-90deg) translateZ(150px);
    -moz-transform: rotateX(0deg) rotateY(-90deg) translateZ(150px);
    -ms-transform: rotateX(0deg) rotateY(-90deg) translateZ(150px);
    -o-transform: rotateX(0deg) rotateY(-90deg) translateZ(150px);
    transform: rotateX(0deg) rotateY(-90deg) translateZ(150px);
    width: 300px;
    height: 300px;
}

#cube #back {
    -webkit-transform: rotateX(0deg) rotateY(90deg) translateZ(150px);
    -moz-transform: rotateX(0deg) rotateY(90deg) translateZ(150px);
    -ms-transform: rotateX(0deg) rotateY(90deg) translateZ(150px);
    -o-transform: rotateX(0deg) rotateY(90deg) translateZ(150px);
    transform: rotateX(0deg) rotateY(90deg) translateZ(150px);
    width: 300px;
    height: 300px;
}

#cube #top {
    -webkit-transform: rotateX(90deg) rotateY(0deg) translateZ(150px);
    -moz-transform: rotateX(90deg) rotateY(0deg) translateZ(150px);
    -ms-transform: rotateX(90deg) rotateY(0deg) translateZ(150px);
    -o-transform: rotateX(90deg) rotateY(0deg) translateZ(150px);
    transform: rotateX(90deg) rotateY(0deg) translateZ(150px);
    width: 300px;
    height: 300px;
}

#cube #bottom {
    -webkit-transform: rotateX(-90deg) rotateY(0deg) translateZ(150px);
    -moz-transform: rotateX(-90deg) rotateY(0deg) translateZ(150px);
    -ms-transform: rotateX(-90deg) rotateY(0deg) translateZ(150px);
    -o-transform: rotateX(-90deg) rotateY(0deg) translateZ(150px);
    transform: rotateX(-90deg) rotateY(0deg) translateZ(150px);
    width: 300px;
    height: 300px;
}

#cube #left {
    -webkit-transform: rotateX(0deg) rotateY(180deg) translateZ(150px);
    -moz-transform: rotateX(0deg) rotateY(180deg) translateZ(150px);
    -ms-transform: rotateX(0deg) rotateY(180deg) translateZ(150px);
    -o-transform: rotateX(0deg) rotateY(180deg) translateZ(150px);
    transform: rotateX(0deg) rotateY(180deg) translateZ(150px);
    width: 300px;
    height: 300px;
}

#cube #right {
    -webkit-transform: rotateX(0deg) rotateY(0deg) translateZ(150px);
    -moz-transform: rotateX(0deg) rotateY(0deg) translateZ(150px);
    -ms-transform: rotateX(0deg) rotateY(0deg) translateZ(150px);
    -o-transform: rotateX(0deg) rotateY(0deg) translateZ(150px);
    transform: rotateX(0deg) rotateY(0deg) translateZ(150px);
    width: 300px;
    height: 300px;
}
于 2012-11-09T22:13:52.327 回答
0

试试这个:http ://css3.bradshawenterprises.com/demos/cubecarousel.php

您需要查看源代码以了解它是如何工作的,但它应该非常简单。

于 2012-05-21T08:12:15.290 回答