5

我试图将 div 从正面垂直翻转到背面,然后恢复它。但是当我悬停而不是翻转时,它会折叠成一条细条。有什么问题?[水平翻转工作正常。当我将其更改为垂直翻转时出现问题]

<html>
<head>
<style type="text/css">

    /* entire container, keeps perspective */
.flip-container {
    perspective: 1000;
}
    /* flip the pane when hovered */
    .flip-container:hover .flipper, .flip-container.hover .flipper {
        transform: rotateX(180deg);
    }

.flip-container, .front, .back {
    width: 320px;
    height: 480px;
}

/* flip speed goes here */
.flipper {
    transition: 0.6s;
    transform-style: preserve-3d;

    position: relative;
}

/* hide back of pane during swap */
.front, .back {
    backface-visibility: hidden;

    position: absolute;
    top: 0;
    left: 0;
}

/* front pane, placed above back */
.front {
    z-index: 2;
    background-color:red;
}

/* back, initially hidden pane */
.back {
    transform: rotateY(180deg);
    background-color:green; 
}
.vertical.flip-container {
    position: relative;
}

    .vertical .back {
        transform: rotateX(180deg);
    }

    .vertical.flip-container .flipper {
        transform-origin: 100% 213.5px; /* half of height */
    }

    .vertical.flip-container:hover .flipper {
        transform: rotateX(-180deg);
    }
</style>

</head>
<body>
<div class="flip-container" ontouchstart="this.classList.toggle('hover');">
    <div class="flipper">
        <div class="front">
            Front Side
        </div>
        <div class="back">
            Back Side
        </div>
</div>
</div>
</body>
</html>
4

1 回答 1

5

只需将所有rotateYs 设为rotateXs 并确保将类添加vertical到容器中(因为您的 CSS 中有它,但 HTML 中没有)

工作演示

更改的 HTML

<div class="vertical flip-container" ontouchstart="this.classList.toggle('hover');">

更新的 CSS

/* entire container, keeps perspective */
.flip-container {
    perspective: 1000px;
}

.flip-container, .front, .back {
    width: 320px;
    height: 480px;
}

/* flip speed goes here */
.flipper {
    transition: 0.6s;
    transform-style: preserve-3d;
    position: relative;
}

/* hide back of pane during swap */
.front, .back {
    position: absolute;
    top: 0;
    left: 0;
    backface-visibility:hidden;
    -webkit-backface-visibility:hidden;
}

/* front pane, placed above back */
.front {
    z-index: 1;
    background-color:red;
}

/* back, initially hidden pane */
.back {
    transform: rotateX(-180deg);
    background-color:green; 
    animation: toFront 0.3s linear normal forwards;
}
.vertical.flip-container {
    position: relative;
}
.vertical.flip-container:hover .back {
    animation-delay: 0.3s;
    animation: toBack 0.3s linear normal  forwards;
}
@keyframes toBack {  
  0% {z-index:0;}
  100% {z-index:1;}
}
@keyframes toFront {
  0% {z-index:1;}
  100% {z-index:0;}
}
.vertical.flip-container .flipper {
    transform-origin: 100% 240px; /* half of height */
}

.vertical.flip-container:hover .flipper {
    transform: rotateX(-180deg);
}

我还使用 CSS3 动画添加了全部功能

于 2013-08-01T15:31:04.763 回答