透视效果仅适用于未封闭的img标签。为什么?以及如何解决?

时间:2019-01-31 23:17:17

标签: javascript jquery html css

我正在尝试在下面创建确切的效果(但在弹出模式下)。到目前为止,我可以使用它,但是html损坏了(img标签未关闭)。如果我修复了img标签,透视效果就会消失。有人可以解释原因,然后告诉我如何解决吗?我的观点是:https://css-tricks.com/almanac/properties/p/perspective/,主包装上的像素为100px。

var lFollowX = 0,
    lFollowY = 0,
    x = 0,
    y = 0,
    friction = 1 / 30;

function moveBackground() {
  x += (lFollowX - x) * friction;
  y += (lFollowY - y) * friction;
  
  translate = 'translate(' + x + 'px, ' + y + 'px) scale(1.1)';

  $('.bg').css({
    '-webit-transform': translate,
    '-moz-transform': translate,
    'transform': translate
  });

  window.requestAnimationFrame(moveBackground);
}

$(window).on('mousemove click', function(e) {

  var lMouseX = Math.max(-100, Math.min(100, $(window).width() / 2 - e.clientX));
  var lMouseY = Math.max(-100, Math.min(100, $(window).height() / 2 - e.clientY));
  lFollowX = (20 * lMouseX) / 100; // 100 : 12 = lMouxeX : lFollow
  lFollowY = (10 * lMouseY) / 100;

});

moveBackground();
body {
  height: 100vh;
}

.wrap {
  background-color: #0F2044;
  -webkit-perspective: 100px;
          perspective: 100px;
  height: 100%;
  position: relative;
  overflow: hidden;
}
.wrap .bg {
  width: 100%;
  height: 100%;
  position: absolute;
  top: 0;
  left: 0;
  z-index: -1;
  -webkit-transform: scale(1.1);
          transform: scale(1.1);
}
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<div class="wrap">
  
  <div class="bg">
        <img class="front" src="https://shannels.com/fg.svg"
  </div>
    <div class="bg">
        <img class="front" src="https://shannels.com/mg.svg"
  </div>
      <div class="bg">
        <img class="front" src="https://shannels.com/bg.svg" 
  </div>
  
</div>

1 个答案:

答案 0 :(得分:3)

无效代码将创建img的嵌套元素,您可以如下创建:

<div class="bg">
    <img class="front" src="https://shannels.com/fg.svg">
    <div class="bg">
      <img class="front" src="https://shannels.com/mg.svg">
      <div class="bg">
        <img class="front" src="https://shannels.com/bg.svg">
      </div>
    </div>
  </div>

var lFollowX = 0,
  lFollowY = 0,
  x = 0,
  y = 0,
  friction = 1 / 30;

function moveBackground() {
  x += (lFollowX - x) * friction;
  y += (lFollowY - y) * friction;

  translate = 'translate(' + x + 'px, ' + y + 'px) scale(1.1)';

  $('.bg').css({
    '-webit-transform': translate,
    '-moz-transform': translate,
    'transform': translate
  });

  window.requestAnimationFrame(moveBackground);
}

$(window).on('mousemove click', function(e) {

  var lMouseX = Math.max(-100, Math.min(100, $(window).width() / 2 - e.clientX));
  var lMouseY = Math.max(-100, Math.min(100, $(window).height() / 2 - e.clientY));
  lFollowX = (20 * lMouseX) / 100; // 100 : 12 = lMouxeX : lFollow
  lFollowY = (10 * lMouseY) / 100;

});

moveBackground();
body {
  height: 100vh;
}

.wrap {
  background-color: #0F2044;
  -webkit-perspective: 100px;
  perspective: 100px;
  height: 100%;
  position: relative;
  overflow: hidden;
}

.wrap .bg {
  width: 100%;
  height: 100%;
  position: absolute;
  top: 0;
  left: 0;
  z-index: -1;
  -webkit-transform: scale(1.1);
  transform: scale(1.1);
}
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<div class="wrap">

  <div class="bg">
    <img class="front" src="https://shannels.com/fg.svg">
    <div class="bg">
      <img class="front" src="https://shannels.com/mg.svg">
      <div class="bg">
        <img class="front" src="https://shannels.com/bg.svg">
      </div>
    </div>
  </div>

</div>

我不完全知道浏览器如何转换代码,但我认为我们不应该依赖无效代码。

似乎这部分

 <img class="front" src="https://shannels.com/fg.svg"
 </div>

被浏览器视为<img class="front" src="https://shannels.com/fg.svg"</div><img .. >),因此您将缺少一个关闭的div,浏览器稍后将尝试关闭所有这些。

此技巧使代码易于复制粘贴以添加新图层,但无法保证在所有浏览器中行为相同。