jquery - 'Card' 沿Y轴旋转180度

标签 jquery css 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>

jQuery:

$(document).ready(function () {
  $('.box_holder').click(function () {
    $(this).toggleClass('show_info')
  });
});

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 {
}

最佳答案

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

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

$(document).ready(function() {
  $('.box_holder').click(function() {
    $(this).toggleClass('show_info')
  });
});
.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);
}
<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>

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

关于jquery - 'Card' 沿Y轴旋转180度,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33386764/

相关文章:

html - 如何让两个标题与 css 在同一行?

css - 对缩放对象执行旋转过渡 CSS 3

css - Webkit:CSS3 2D 变换 Scale + cubic bezier 问题(当参数 > 1 时)

html - 重新调整立方体的大小

javascript - 强制用户在文本框输入中输入特定详细信息

javascript - 如果 ruby​​ on rails 中的 text_field 为空,如何禁用提交按钮?

php - 刚刚用 html 和 css 创建了一个搜索栏,如何对 php 搜索表单使用相同的样式?

php - apache 重写规则不起作用...尝试结合 css 和 javascript

jquery - 将相同的函数绑定(bind)到jquery中的不同元素

javascript - 如何从异步调用返回响应?