转换原点:100%不适用于转换旋转

时间:2019-12-02 19:10:49

标签: css css-transforms

我想创建一个小册子,当有人将鼠标悬停在该小册子上时,该小册子向左右打开。 我在小册子的左侧位置使用transform-origin: 0%transform: rotateY(-180deg),打开左侧的位置(卡红色和蓝色)。对于右侧,我当然使用transform-origin: 100%transform: rotateY(180deg)打开右侧的站点(绿色卡)。

但是右侧的实际行为是,它应用transform-origin: 0%并在小册子的左侧位置后面旋转(这就是为什么看不到它,但在那里看到的原因)。

我不明白为什么transform-origin: 100%不起作用。

下面是代码:https://jsfiddle.net/eL6q3hp4/2/

.book-container {
  height: 350px;
  width: 250px;
  position: absolute;
  top: 50%;
  left: 50%;
  transform: translate(-50%, -50%);
  -webkit-perspective: 1000px;
  -moz-perspective: 1000px;
  perspective: 1000px;
}

.book-part {
  height: auto;
  width: auto;
  position: absolute;
  transform-style: preserve-3d;
  top: 0;
  left: 0;
}

.book-part > .card {
  height: 350px;
  width: 250px;
  position: absolute;
  transform-style: preserve-3d;
  backface-visibility: hidden;
}

/* RIGHT SITE */
.right-part {
  transform-origin: 0%;
}
.right-card {
  transform: rotateY(-180deg);
}
.book-container:hover .right-part {
  transform: rotateY(180deg);
  transition: 0.7s;
  transition-delay: 0.3s;
}

/* LEFT SITE */
.left-part {
  transform-origin: 100%;
}
.left-card {
  transform: rotateY(180deg);
}
.book-container:hover .left-part {
  transform: rotateY(-180deg);
  transition: 0.7s;
}


.middle-card {
  background-color: blue;
}

.right-card {
  background-color: green;
}

.left-card {
  background-color: red;
}

.front-card {
  background-color: yellow;
}
    <div class="book-container">
        <div class="book-part middle-part">
            <div class="card middle-card"></div>
        </div>
        <div class="book-part right-part">
            <div class="card right-card"></div>
        </div>
        <div class="book-part left-part">
            <div class="card left-card"></div>
            <div class="card front-card"></div>
        </div>
    </div>

2 个答案:

答案 0 :(得分:0)

.book-part中删除绝对位置并反转旋转值。您也可以在右边的方块中添加正面卡片

我还添加了过渡效果:

.book-container {
  height: 350px;
  width: 250px;
  position: absolute;
  top: 50%;
  left: 50%;
  transform: translate(-50%, -50%);
  perspective: 1000px;
}

.book-part {
  transform-style: preserve-3d;
}

.book-part>.card {
  height: 350px;
  width: 250px;
  position: absolute;
  transform-style: preserve-3d;
  backface-visibility: hidden;
}


/* RIGHT SITE */

.right-part {
  transform-origin: 0%;
  transition: 0.7s;
}

.right-card {
  transform: rotateY(180deg);
}

.book-container:hover .right-part {
  transform: rotateY(-180deg);
  transition-delay: 0.3s;
}


/* LEFT SITE */

.left-part {
  transform-origin: 100%;
  transition: 0.7s;
  transition-delay: 0.3s;
}

.left-card {
  transform: rotateY(-180deg);
}

.book-container:hover .left-part {
  transform: rotateY(180deg);
  transition-delay: 0s;
}
<div class="book-container">
  <div class="book-part middle-part">
    <div class="card middle-card" style="background:blue"></div>
  </div>
  <div class="book-part right-part">
    <img class="card right-card" style="background:green">
    <img class="card front-card" style="background:pink;">
  </div>
  <div class="book-part left-part">
    <img class="card left-card" style="background:red"> 
    <img class="card front-card" style="background:yellow">
  </div>
</div>

您还可以像下面那样简化代码:

.book-container {
  height: 350px;
  width: 250px;
  position: absolute;
  top: 50%;
  left: 50%;
  transform: translate(-50%, -50%);
  perspective: 1000px;
  background:blue;
}
.book-container > div{
  transform-style: preserve-3d;
}

.book-container > div:before,
.book-container > div:after{
  content:"";
  height: 350px;
  width:100%;
  position: absolute;
  transform-style: preserve-3d;
  backface-visibility: hidden;
  background:yellow;
}


/* RIGHT SITE */
.right-part {
  transform-origin: 0%;
  transition: 0.7s;
}

.book-container .right-part:before {
  transform: rotateY(180deg);
  background:green;
}

.book-container:hover .right-part {
  transform: rotateY(-180deg);
  transition-delay: 0.3s;
}


/* LEFT SITE */
.left-part {
  transform-origin: 100%;
  transition: 0.7s;
  transition-delay: 0.3s;
}

.book-container .left-part:before {
  transform: rotateY(180deg);
  background:red;
}

.book-container:hover .left-part {
  transform: rotateY(180deg);
  transition-delay: 0s;
}
<div class="book-container">
  <div class="right-part">
  </div>
  <div class="left-part">
  </div>
</div>

答案 1 :(得分:0)

我不知道在最初的情况下您是否使用动态尺寸(在这种情况下该解决方案将不适用),但是您可以对transform-origin使用像素值来使其起作用:

.book-container {
  height: 350px;
  width: 250px;
  position: absolute;
  top: 50%;
  left: 50%;
  transform: translate(-50%, -50%);
  -webkit-perspective: 1000px;
  -moz-perspective: 1000px;
  perspective: 1000px;
}

.book-part {
  height: auto;
  width: auto;
  position: absolute;
  transform-style: preserve-3d;
  top: 0;
  left: 0;
}

.book-part > .card {
  height: 350px;
  width: 250px;
  position: absolute;
  transform-style: preserve-3d;
  backface-visibility: hidden;
}

/* RIGHT SITE */
.right-part {
  transform-origin: 250px;
}
.right-card {
  transform: rotateY(180deg);
}
.book-container:hover .right-part {
  transform: rotateY(180deg);
  transition: 0.7s;
  transition-delay: 0.3s;
}

/* LEFT SITE */
.left-part {
  transform-origin: 0%;
}
.left-card {
  transform: rotateY(-180deg);
}
.book-container:hover .left-part {
  transform: rotateY(-180deg);
  transition: 0.7s;
}


.middle-card {
  background-color: blue;
}

.right-card {
  background-color: green;
}

.left-card {
  background-color: red;
}

.front-card {
  background-color: yellow;
}
<div class="book-container">
        <div class="book-part middle-part">
            <div class="card middle-card"></div>
        </div>
        <div class="book-part right-part">
            <div class="card right-card"></div>
        </div>
        <div class="book-part left-part">
            <div class="card left-card"></div>
            <div class="card front-card"></div>
        </div>
    </div>