CSS3 3D旋转:透视如何工作?

时间:2016-10-13 16:56:51

标签: css3 3d rotation transform perspective

我在鼠标移动后处理3D bloc。我正在更新CSS值以创建3D效果。 基地看起来像:



document.addEventListener('DOMContentLoaded', function() {
  let mouseMove = function (e) {
    let el = e.currentTarget;
    let delta_x = parseFloat(e.offsetX / el.offsetWidth).toFixed(3)
    let delta_y = parseFloat(e.offsetY / el.offsetHeight).toFixed(3)

    var transform = "rotateY(" + ((delta_x - 0.5) * 50) + "deg) " +
        "rotateX(" + (-(delta_y - 0.5) * 50) + "deg)"
    var boxShadow = parseInt(-(delta_x - 0.5) * 8) +"px " + 
        parseInt(-((delta_y - 0.5) * 8) + 2) + 
        "px 4px rgba(34, 25, 25, 0.4);"

    el.setAttribute('style', 
                    "transform: " + transform + "; " + 
                    "box-shadow: " + boxShadow);
  }

  let els = document.getElementsByClassName("el")
  let len = els.length
  for(let i=0; i<len; i++) {
    let el = els[i]
    el.addEventListener("mousemove", mouseMove)
  }
}, false);
&#13;
html, body, #wrapper { 
  height: 100%; 
  margin: 0; 
  padding: 0; 
}
#wrapper {
  background: #a4d24b;
  /*font-family: 'Open Sans', sans-serif; */
  font-weight: 400;
  font-size: 14px;
  display: flex;
  flex-wrap: wrap;
  perspective: 500px;
}
.container {
  position: relative;
  margin: auto;
  width: 20%;
  height: 40%;
  transition: all .25s;
  transform-origin: 50% 50%;
}
.container:hover {
  transform: translateZ(25px);
}
.el {
  height: 100%;
  background: #FFF; color: #000;
  box-shadow: 0 2px 4px rgba(34, 25, 25, 0.4);
}
.el:not(:hover) {
  transform: rotateY(0deg) rotateX(0deg) !important;
  box-shadow: 0 2px 4px rgba(34, 25, 25, 0.4) !important;
}
&#13;
<div id="wrapper">
  <div class="container">
    <div class="el"></div>
  </div>
</div>
&#13;
&#13;
&#13;

JS概念很简单:当鼠标在集团上移动时,我得到鼠标在 x y

  • 对于 x (0 - &gt; 1),我将transform属性从rotate: rotateY(-12deg)移至rotate: rotateY(12deg)
  • y (0 - &gt; 1)从rotate: rotateX(-12deg)rotate: rotateX(12deg)
  • 的情况相同
  • 我还移动了盒子阴影(带有x和y)来帮助实现可视化。

结果看起来不错,但是如果我在这个bloc中添加一个背景,结果看起来很奇怪。

(与背景相同的代码)

&#13;
&#13;
document.addEventListener('DOMContentLoaded', function() {
  let mouseMove = function (e) {
    let el = e.currentTarget;
    let delta_x = parseFloat(e.offsetX / el.offsetWidth).toFixed(3)
    let delta_y = parseFloat(e.offsetY / el.offsetHeight).toFixed(3)

    var transform = "rotateY(" + ((delta_x - 0.5) * 50) + "deg) " +
        "rotateX(" + (-(delta_y - 0.5) * 50) + "deg)"
    var boxShadow = parseInt(-(delta_x - 0.5) * 8) +"px " + 
        parseInt(-((delta_y - 0.5) * 8) + 2) + 
        "px 4px rgba(34, 25, 25, 0.4);"

    el.setAttribute('style', 
                    "transform: " + transform + "; " + 
                    "box-shadow: " + boxShadow);
  }

  let els = document.getElementsByClassName("el")
  let len = els.length
  for(let i=0; i<len; i++) {
    let el = els[i]
    el.addEventListener("mousemove", mouseMove)
  }
}, false);
&#13;
html, body, #wrapper { 
  height: 100%; 
  margin: 0; 
  padding: 0; 
}
#wrapper {
  background: #a4d24b;
  /*font-family: 'Open Sans', sans-serif; */
  font-weight: 400;
  font-size: 14px;
  display: flex;
  flex-wrap: wrap;
  perspective: 500px;
}
.container {
  position: relative;
  margin: auto;
  width: 20%;
  height: 40%;
  transition: all .25s;
  transform-origin: 50% 50%;
}
.container:hover {
  transform: translateZ(25px);
}
.el {
  height: 100%;
  background: #FFF url(https://images-na.ssl-images-amazon.com/images/M/MV5BMjQyODg5Njc4N15BMl5BanBnXkFtZTgwMzExMjE3NzE@._V1_SY1000_SX686_AL_.jpg);
  background-size: cover;
  background-position: 50% 50%; 
  color: #000;
  box-shadow: 0 2px 4px rgba(34, 25, 25, 0.4);
}
.el:not(:hover) {
  transform: rotateY(0deg) rotateX(0deg) !important;
  box-shadow: 0 2px 4px rgba(34, 25, 25, 0.4) !important;
}
&#13;
<div id="wrapper">
  <div class="container">
    <div class="el"></div>
  </div>
</div>
&#13;
&#13;
&#13;

现在,它看起来只有顶部工作,而底部并没有向好的方向发展。 (当鼠标从左上角移动到右下角时,最佳再现。)

我犯了错误吗?或者它是一个透视问题?

更新有些浏览器并不喜欢CSS,如果白色框在你移动鼠标时没有移动,只需删除它即可。

.el:not(:hover) { transform: rotateY(0deg) rotateX(0deg) !important; box-shadow: 0 2px 4px rgba(34, 25, 25, 0.4) !important; }

1 个答案:

答案 0 :(得分:1)

您需要在已转换元素的直接父级上应用透视。

我解决了使用perspective:inherit

&#13;
&#13;
document.addEventListener('DOMContentLoaded', function() {
  let mouseMove = function (e) {
    let el = e.currentTarget;
    let delta_x = parseFloat(e.offsetX / el.offsetWidth).toFixed(3)
    let delta_y = parseFloat(e.offsetY / el.offsetHeight).toFixed(3)

    var transform = "rotateY(" + ((delta_x - 0.5) * 50) + "deg) " +
        "rotateX(" + (-(delta_y - 0.5) * 50) + "deg)"
    var boxShadow = parseInt(-(delta_x - 0.5) * 8) +"px " + 
        parseInt(-((delta_y - 0.5) * 8) + 2) + 
        "px 4px rgba(34, 25, 25, 0.4);"

    el.setAttribute('style', 
                    "transform: " + transform + "; " + 
                    "box-shadow: " + boxShadow);
  }

  let els = document.getElementsByClassName("el")
  let len = els.length
  for(let i=0; i<len; i++) {
    let el = els[i]
    el.addEventListener("mousemove", mouseMove)
  }
}, false);
&#13;
html, body, #wrapper { 
  height: 100%; 
  margin: 0; 
  padding: 0; 
}
#wrapper {
  background: #a4d24b;
  /*font-family: 'Open Sans', sans-serif; */
  font-weight: 400;
  font-size: 14px;
  display: flex;
  flex-wrap: wrap;
  perspective: 500px;
}
.container {
  position: relative;
  margin: auto;
  width: 20%;
  height: 40%;
  transition: all .25s;
  transform-origin: 50% 50%;
  perspective: inherit;
}
.container:hover {
  transform: translateZ(25px);
}
.el {
  height: 100%;
  background: #FFF url(https://images-na.ssl-images-amazon.com/images/M/MV5BMjQyODg5Njc4N15BMl5BanBnXkFtZTgwMzExMjE3NzE@._V1_SY1000_SX686_AL_.jpg);
  background-size: cover;
  background-position: 50% 50%; 
  color: #000;
  box-shadow: 0 2px 4px rgba(34, 25, 25, 0.4);
}
.el:not(:hover) {
  transform: rotateY(0deg) rotateX(0deg) !important;
  box-shadow: 0 2px 4px rgba(34, 25, 25, 0.4) !important;
}
&#13;
<div id="wrapper">
  <div class="container">
    <div class="el"></div>
  </div>
</div>
&#13;
&#13;
&#13;