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

最佳答案

我希望这段代码能达到您想要的效果。我通过使用变量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>

关于jquery - HTML和CSS旋转和可缩放3D立方体,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/31445358/

10-12 07:12