【问题标题】:HTML5 rotate animation - How to show "the other side of the coin"?HTML5 旋转动画 - 如何显示“硬币的另一面”?
【发布时间】:2016-02-25 00:32:04
【问题描述】:

考虑this JSFiddle rotation example

HTML:

<div class="container">
  <div class="coin">
    <div class="face heads">
      Hey!
    </div>
    <div class="face tails">
      Ho!
    </div>
  </div>
</div>

CSS:

@keyframes rotation {
  0% {
    transform: rotate3d(0, 1, 0, 0deg);
  }
  50% {
    transform: rotate3d(0, 1, 0, 180deg);
  }
  100% {
    transform: rotate3d(0, 1, 0, 360deg);
  }
}

.container {
  background-color: blue;
  width: 100px;
  height: 100px;
}

.coin {
  position: relative;
  top: 25px;
  left: 25px;
  width: 50px;
  height: 50px;
  text-align: center;
  line-height: 50px;
  animation-name: rotation;
  animation-iteration-count: infinite;
  animation-timing-function: linear;
  animation-duration: 2.5s;
}

.face {
  position: absolute;
  top: 0;
  left: 0;
  width: 100%;
  height: 100%;
  border-radius: 50%;
}

.heads {
  background-color: green;
  z-index: 2;
}

.tails {
  background-color: gray;
  z-index: 1;
}

如您所见,众所周知的.coin 由两个.faces 组成,由两个重叠的divs 和不同的z-index 表示。

当整个.coin 围绕Y 轴旋转时,动画是否应该在达到关键帧的50% 时渲染.tails 面? 还是我对 HTML5 期望过高?还是我只是做错了什么?

提前感谢您的澄清!

【问题讨论】:

  • @Paulie_D 好点,谢谢。我编辑了问题。

标签: css html animation css-animations


【解决方案1】:

David Walsh- CSS Flip的帮助下,我不得不做一些研究

@keyframes rotation {
  0% {
    transform: rotate3d(0, 1, 0, 0deg);
  }
  50% {
    transform: rotate3d(0, 1, 0, 180deg);
  }
  100% {
    transform: rotate3d(0, 1, 0, 360deg);
  }
}
.container {
  background-color: blue;
  width: 100px;
  height: 100px;
  perspective: 1000;
  margin: 1em auto;
}
.coin {
  position: relative;
  top: 25px;
  left: 25px;
  width: 50px;
  height: 50px;
  text-align: center;
  line-height: 50px;
  animation-name: rotation;
  animation-iteration-count: infinite;
  animation-timing-function: linear;
  animation-duration: 2.5s;
  transform: rotateY(0deg);
  transform-style: preserve-3d;
}
.face {
  position: absolute;
  top: 0;
  left: 0;
  width: 100%;
  height: 100%;
  border-radius: 50%;
  backface-visibility: hidden;
}
.heads {
  background-color: green;
  z-index: 2;
  transform: rotateY(0deg);
}
.tails {
  background-color: gray;
  z-index: 1;
  transform: rotateY(180deg);
}
<div class="container">
  <div class="coin">
    <div class="face heads">
      Hey!
    </div>
    <div class="face tails">
      Ho!
    </div>
  </div>
</div>

【讨论】:

  • 是的。这些是我一直在寻找的属性。非常感谢。 :)
  • 有没有办法获得相同的效果,但实际上使用图像?我按照相同的教程,但是如果我用图像替换文本,我没有得到效果,只显示一侧
猜你喜欢
  • 2011-09-02
  • 2019-04-16
  • 1970-01-01
  • 2015-05-25
  • 1970-01-01
  • 2013-08-26
  • 2016-12-27
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多