RotateX在隐藏溢出的情况下停止

时间:2019-02-24 16:18:22

标签: css css-animations css-transforms

我有一个使用关键帧和变换的简单动画,特别是rotateXrotateZ。动画效果符合我的预期(至少在Chrome中,还没有尝试过其他动画)。

现在,我可以正常工作了,如果动画元素超出其父元素,我想隐藏它们。我将普通的overflow: hidden;放在父级上。

然后rotateX就会停止渲染。 rotateZ继续按预期进行。奇怪的是,看起来元素在检查时仍在继续转换,但是子元素仅遵守rotateZ

@keyframes stir {
  0% {
    transform: rotateX(0deg) rotateZ(-25deg);
  }
  25% {
    transform: rotateX(3deg) rotateZ(0deg);
  }
  50% {
    transform: rotateX(0deg) rotateZ(25deg);
  }
  75% {
    transform: rotateX(-3deg) rotateZ(0deg);
  }
  100% {
    transform: rotateX(0deg) rotateZ(-25deg);
  }
}
* {
  box-sizing: border-box;
}
.mixing {
  position: relative;
  width: 80px;
  padding-top: 50px;
  margin: 50px auto;
}
.mixing .rim,
.mixing .rimb {
  width: 80px;
  height: 20px;
  border: 2px solid #000;
  border-radius: 40px / 10px;
  background: #fff;
  position: relative;
  z-index: 5;
}
.mixing .rimb {
  z-index: 3;
}
.mixing .sides {
  position: relative;
  background: #fff;
  height: 8px;
  width: 100%;
  margin: -18px 0 0;
  border-left: 2px solid #000;
  border-right: 2px solid #000;
  top: 10px;
  z-index: 4;
}
.mixing .bowl {
  width: 70px;
  height: 40px;
  margin: -16px auto 0;
  border: 2px solid #000;
  background: #fff;
  border-radius: 0 0 30px 30px / 0 0 40px 40px;
}
.mixing .spoon-box {
  width: 120px;
  height: 70px;
  border-radius: 0 0 60px 60px / 0 0 20px 20px;
  position: absolute;
  top: -2px;
  left: -20px;
  z-index: 10;
  perspective: 25px;
  transform-style: preserve-3d;
}
.mixing .spoon {
  position: absolute;
  left: 50%;
  margin-left: -9px;
  bottom: -50px;
  transform-origin: center bottom;
  animation: stir 2s infinite;
  animation-timing-function: linear;
}
.mixing .spoon .stick {
  height: 55px;
  width: 8px;
  border: 2px solid #000;
  border-radius: 3px 3px 0 0;
  border-width: 2px 2px 0px;
  background: #fff;
  margin: 8px 5px -4px;
  position: relative;
  z-index: 2;
}
.mixing .spoon .cup {
  position: relative;
  height: 22px;
  width: 18px;
  margin-bottom: 40px;
  border: 2px solid #000;
  background: #fff;
  border-radius: 9px / 11px;
}
.broken .spoon-box {
  overflow: hidden;
}
<div class="mixing">
  <div class="rim"></div>
  <div class="sides"></div>
  <div class="rimb"></div>
  <div class="bowl"></div>
  <div class="spoon-box">
    <div class="spoon">
      <div class="stick"></div>
      <div class="cup"></div>
    </div>
  </div>
</div>
<div class="mixing broken">
  <div class="rim"></div>
  <div class="sides"></div>
  <div class="rimb"></div>
  <div class="bowl"></div>
  <div class="spoon-box">
    <div class="spoon">
      <div class="stick"></div>
      <div class="cup"></div>
    </div>
  </div>
</div>

https://codepen.io/freer4/pen/jJNEpP

1 个答案:

答案 0 :(得分:1)

您是否考虑过使用clip-path CSS规则而不是overflow: hidden?在最新的Firefox和Chrome上进行简短测试后,看来您的rotateZ未被应用的问题已修复。作为一个不错的好处,您可以对剪切进行更具体的说明,因此可以使用clip-path: ellipse(...)来匹配碗的形状,如下图所示:

bowl clipping

不过,请注意,IE and Edge不支持clip-path

修改后的示例可以在此forked codepen或以下代码段中找到:

@keyframes stir {
  0% {
    transform: rotateX(0deg) rotateZ(-25deg);
  }
  25% {
    transform: rotateX(3deg) rotateZ(0deg);
  }
  50% {
    transform: rotateX(0deg) rotateZ(25deg);
  }
  75% {
    transform: rotateX(-3deg) rotateZ(0deg);
  }
  100% {
    transform: rotateX(0deg) rotateZ(-25deg);
  }
}
* {
  box-sizing: border-box;
}
.mixing {
  position: relative;
  width: 80px;
  padding-top: 50px;
  margin: 50px auto;
}
.mixing .rim,
.mixing .rimb {
  width: 80px;
  height: 20px;
  border: 2px solid #000;
  border-radius: 40px / 10px;
  background: #fff;
  position: relative;
  z-index: 5;
}
.mixing .rimb {
  z-index: 3;
}
.mixing .sides {
  position: relative;
  background: #fff;
  height: 8px;
  width: 100%;
  margin: -18px 0 0;
  border-left: 2px solid #000;
  border-right: 2px solid #000;
  top: 10px;
  z-index: 4;
}
.mixing .bowl {
  width: 70px;
  height: 40px;
  margin: -16px auto 0;
  border: 2px solid #000;
  background: #fff;
  border-radius: 0 0 30px 30px / 0 0 40px 40px;
}
.mixing .spoon-box {
  width: 120px;
  height: 70px;
  border-radius: 0 0 60px 60px / 0 0 20px 20px;
  position: absolute;
  top: -2px;
  left: -20px;
  z-index: 10;
  perspective: 25px;
  transform-style: preserve-3d;
}
.mixing .spoon {
  position: absolute;
  left: 50%;
  margin-left: -9px;
  bottom: -50px;
  transform-origin: center bottom;
  animation: stir 2s infinite;
  animation-timing-function: linear;
}
.mixing .spoon .stick {
  height: 55px;
  width: 8px;
  border: 2px solid #000;
  border-radius: 3px 3px 0 0;
  border-width: 2px 2px 0px;
  background: #fff;
  margin: 8px 5px -4px;
  position: relative;
  z-index: 2;
}
.mixing .spoon .cup {
  position: relative;
  height: 22px;
  width: 18px;
  margin-bottom: 40px;
  border: 2px solid #000;
  background: #fff;
  border-radius: 9px / 11px;
}
.broken .spoon-box {
  transform-style: preserve-3d;
  clip-path: ellipse(80px 49px at 60px 22px);
}
<div class="mixing">
  <div class="rim"></div>
  <div class="sides"></div>
  <div class="rimb"></div>
  <div class="bowl"></div>
  <div class="spoon-box">
    <div class="spoon">
      <div class="stick"></div>
      <div class="cup"></div>
    </div>
  </div>
</div>
<div class="mixing broken">
  <div class="rim"></div>
  <div class="sides"></div>
  <div class="rimb"></div>
  <div class="bowl"></div>
  <div class="spoon-box">
    <div class="spoon">
      <div class="stick"></div>
      <div class="cup"></div>
    </div>
  </div>
</div>