沿Y轴旋转'卡'180度

Nic*_*ick 3 css jquery css3 css-transforms

我有这个主要工作(请参阅https://jsfiddle.net/90ycrope/1/)但是第二个div中带有"后面"一词的内容在整个过程中都是可见的.

我想要实现的两个目标是:

  1. 正确的功能(背后可见内容,前面可见前面的内容)

  2. 效率 - 我认为代码有一种更简单的方法吗?

HTML:

<div class="box_holder">
<div class="front">Some content here</div>
<div class="back">Behind</div>
</div>
Run Code Online (Sandbox Code Playgroud)

jQuery的:

$(document).ready(function () {
  $('.box_holder').click(function () {
    $(this).toggleClass('show_info')
  });
});
Run Code Online (Sandbox Code Playgroud)

CSS:

.box_holder {
    display: block;
    position: relative;
    float: left;
    left: 8px;
    top: 8px;
    width: 240px;
    height: 335px;
    text-align: center;
    font-family:'ProximaNova', 'Helvetica Neue', Helvetica, Arial, sans-serif;
    font-weight: normal;
    background: #C3C3C3;
    color: #3b3b3b;
    font-size: 1em;
    line-height: 1.32;
    margin-right: 16px;
    margin-bottom: 32px;
    transition: 1s;
}
.box_holder.show_info {
    -ms-transform: rotateY(180deg);
    -webkit-transform: rotateY(180deg);
    transform: rotateY(180deg);
}
.box_holder.show_info .front {
    -ms-transform: rotateY(180deg);
    -webkit-transform: rotateY(180deg);
    transform: rotateY(180deg);
}
.box_holder.show_info .back {
    -ms-transform: rotateY(180deg);
    -webkit-transform: rotateY(180deg);
    transform: rotateY(180deg);
}
.front {
    backface-visibility:hidden;
    transition:1s;
}
.back {
    transition:1s;
}
.show_info .back {
}
Run Code Online (Sandbox Code Playgroud)

Har*_*rry 8

这是翻转动画的简约和固定版本.以下是我所做的:

  • 仅应用容器的高度,宽度并添加position: relative到容器中.
  • 前后元素相对于box_holder容器绝对定位,背景颜色应用于这些子元素.
  • backface-visibility子元素被设置为隐藏.这是关键属性,因为它可以防止元素的背面出现.
  • 最初,前部元件不旋转,但后部元件在Y轴上旋转180度.这会将后面的元素发送回去,并且由于上面提到的设置而变得隐藏.
  • 单击元素并应用show_info类时,后部元素上的旋转无效(旋转回0度),而前部反向旋转180度.这两个一起使它看起来好像正在翻转容器.

$(document).ready(function() {
  $('.box_holder').click(function() {
    $(this).toggleClass('show_info')
  });
});
Run Code Online (Sandbox Code Playgroud)
.box_holder {
  position: relative;
  width: 240px;
  height: 335px;
}
.box_holder .front, .box_holder .back {
  position: absolute;
  top: 0px;
  left: 0px;
  height: 100%;
  width: 100%;
  text-align: center;
  background: #C3C3C3;
  color: #3b3b3b;
  transition: 1s;
  backface-visibility: hidden;
}
.box_holder .back {
  transform: rotateY(180deg);
}
.box_holder.show_info .back {
  transform: rotateY(0deg);
}
.box_holder.show_info .front {
  transform: rotateY(-180deg);
}
Run Code Online (Sandbox Code Playgroud)
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/prefixfree/1.0.7/prefixfree.min.js"></script>
<div class="box_holder">
  <div class="front">Some content here</div>
  <div class="back">Behind</div>
</div>
Run Code Online (Sandbox Code Playgroud)

注意:使用前缀免费库只是为了避免添加使代码膨胀的前缀版本.