如何使用CSS3创建3D透视图?

时间:2013-11-14 21:45:21

标签: html css3

我一直在尝试为我一直在研究的项目创建一个3D外观卡片翻转动画类型。不幸的是,我的动画看起来并不完全是3D。

我一直在使用这个guide。在第一个例子中,这个人设法使它看起来像窗户背景翻转。但是,当我尝试在JSFiddle上使用相同的代码时,结果与他的结果不同。

他的演示代码在下面产生了效果。当卡被翻转时,它会使一侧变小,给人以透视的印象: enter image description here

在我的JSFiddle上使用他的代码(除了不同的背景),两边看起来在整个时间内保持相同的大小: enter image description here

有人可以向我解释我错过了什么,或者如何在他的网站上获得相同的透视效果?提前致谢。

他的HTML代码:

<div id="f1_container">
<div id="f1_card" class="shadow">
  <div class="front face">
    <img src="/images/Windows%20Logo.jpg"/>
  </div>
  <div class="back face center">
    <p>This is nice for exposing more information about an image.</p>
    <p>Any content can go here.</p>
  </div>
</div>
</div>

他的CSS代码:

#f1_container {
  position: relative;
  margin: 10px auto;
  width: 450px;
  height: 281px;
  z-index: 1;
}
#f1_container {
  perspective: 1000;
}
#f1_card {
  width: 100%;
  height: 100%;
  transform-style: preserve-3d;
  transition: all 1.0s linear;
}
#f1_container:hover #f1_card {
  transform: rotateY(180deg);
  box-shadow: -5px 5px 5px #aaa;
}
.face {
  position: absolute;
  width: 100%;
  height: 100%;
  backface-visibility: hidden;
}
.face.back {
  display: block;
  transform: rotateY(180deg);
  box-sizing: border-box;
  padding: 10px;
  color: white;
  text-align: center;
  background-color: #aaa;
}

2 个答案:

答案 0 :(得分:3)

看看他说他从他的CSS中删除了供应商前缀以保持清洁?我打赌这是你的问题。其中一些CSS属性不是完全标准的,但是在具有不同供应商前缀的不同浏览器中实现。我实际上不确定哪些,但谷歌可以提供帮助。

编辑:嗯。好吧,CSS无论如何都是罪魁祸首,但我实际上并没有看到很多厂商的前缀。我将他的实际CSS从页面中删除,并将其粘贴到您使用的“干净”CSS的位置,这使得小提琴起作用。他真正的CSS是:

#f1_container {
    height: 281px;
    margin: 10px auto;
    position: relative;
    width: 450px;
    z-index: 1;
}
#f1_container {
    perspective: 1000px;
}
#f1_card {
    height: 100%;
    transform-style: preserve-3d;
    transition: all 1s linear 0s;
    width: 100%;
}
#f1_container:hover #f1_card, #f1_container.hover_effect #f1_card {
    box-shadow: -5px 5px 5px #AAAAAA;
    transform: rotateY(180deg);
}
.face {
    backface-visibility: hidden;
    height: 100%;
    position: absolute;
    width: 100%;
}
.face.back {
    -moz-box-sizing: border-box;
    background-color: #AAAAAA;
    color: #FFFFFF;
    display: block;
    padding: 10px;
    text-align: center;
    transform: rotateY(180deg);
}

答案 1 :(得分:2)

他们有一些CSS不正确......在这个例子中,他有一个.back.face课,应该是.face.back(不是为什么它不起作用,正如所指出的那样。只是把它清理干净了)。其他问题是其他海报指出的罪魁祸首。我创造了一个新的jsFiddle。虽然我会使用jQuery翻转插件,因为IE很难呈现这样的效果。

http://jsfiddle.net/JJrHD/1/

相关问题