HTML 和 CSS 旋转和缩放 3D 立方体

HTML and CSS rotating and zoomable 3D cube

我用 html 和 css 做了一个立方体。此外,我在按下按钮时输入 jquery 以激活 css 类。但是,它们无法正常工作,而且,如果我想多次沿同一方向旋转立方体,我需要按两次按钮。我怎样才能解决这个问题?另外,当立方体旋转后,我希望黑色的一面留在一边。目前,每次我转动立方体时,背面都会变黑。和你好一样。另外,如何添加缩放功能,在我滚动时放大和缩小立方体? 我的代码 - http://pastebin.com/qZFfTsuG

你的代码有问题...

  • 你获取 jquery.min.js 两次,而你只需要一次。
  • 您有两个 $('document').ready 调用,而您只需要一个。您应该将两个按钮侦听器放在同一个中。
  • CSS animation 是实验性的,您应该添加一个额外的 css-条目,其中包含 Chrome 和 Safari 的供应商前缀(-webkit-animation: ).
  • 双击问​​题:您必须在动画完成后删除 rotate-left/right-css class(通过 JavaScript),或者创建一个第二次点击的动画,当 rotate-left/right-css class 被你当前的 toggleClass-call 自动删除时......最好的解决方案需要一些时间来解决,以决定。

总而言之,很多问题。

希望这段代码能达到你想要的效果。我通过使用变量 yAngle 来存储当前方向来使用不同的方法。这是此代码的 jsfiddle

$(document).ready(function(){
    var yAngle = 0;
    
    $("#button_left").click(function(){
        yAngle = yAngle-90;
        $("section").css("transform",'rotateY('+yAngle+'deg)');
    });
    
    $("#button_right").click(function(){
        yAngle = yAngle+90;
        $("section").css("transform","rotateY("+yAngle+"deg)");
    });
});
.wrap
{
    perspective: 800px;
    perspective-origin: 50% 100px;
}
.cube
{
    margin: 0 auto;
    position: relative;
    width: 200px;
    transform-style: preserve-3d;
    transition: transform 1s;
}
.cube div
{
    box-shadow: inset 0 0 20px rgba(125,125,125,0.9);
    position: absolute;
    width: 200px;
    height: 200px;
}
.back
{
    background: rgba(40,40,40,0.8);
    transform: translateZ(-100px) rotateY(180deg);
}
.right
{
    background: rgba(189,25,400,0.3);
    transform: rotateY(-270deg) translateX(100px);
    transform-origin: top right;
}
.left
{
    background: rgba(189,25,400,0.3);
    transform: rotateY(270deg) translateX(-100px);
    transform-origin: center left;
}
.top
{
    background: rgba(189,25,400,0.3);
    transform: rotateX(-90deg) translateY(-100px);
    transform-origin: top center;
}
.bottom
{
    background: rgba(189,25,400,0.3);
    transform: rotateX(90deg) translateY(100px);
    transform-origin: bottom center;
}
.front
{
    background: rgba(189,25,400,0.3);
    transform: translateZ(100px);
}
/*
@keyframes spin_left {
        from { transform: rotateY(0); }
        to { transform: rotateY(-90deg); }
}
.rotate-left {
        animation: spin_left 1s 1 linear;
}
@keyframes spin_right {
        from { transform: rotateY(0); }
        to { transform: rotateY(90deg); }
}
.rotate-right {
        animation: spin_right 1s 1 linear;
}
*/
<br><br><br><br><br><br><br><br><br><br>
<div class="wrap">
        <section class="cube">
                <div class="front">Hello</div>
                <div class="back"></div>
                <div class="top"></div>
                <div class="bottom"></div>
                <div class="left"></div>
                <div class="right"></div>
        </section>
</div>    
<input type="button" value="<--" id="button_left">
<input type="button" value="-->" id="button_right">
    
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>