微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

纯 css 视差透视不适用于 html 元素

如何解决纯 css 视差透视不适用于 html 元素

无法使 html 元素的透视属性工作。我想创建一个整页视差滚动效果,但它似乎只适用于产生很多问题的 body 元素。如果我使用 body 滚动页面,那么(认行为:Chrome 和 Firefox 将垂直滚动条放在 html 元素上)window.onscroll 事件停止触发。如果我尝试放置 html 元素,则视差效果将停止工作。

更正:此解决方案仅适用于基于 chrome 的浏览器,我写了这个问题,因为它似乎不适用于 Firefox。

window.onscroll = function() {console.log('onscroll')}
    html {
      perspective: 1000px;
      position: relative;
      overflow: hidden;
    }

    body {
      height: 100vh;
      overflow: auto;
      transform-style: preserve-3d
    }
    .section {
      position: relative;
      height: 100vh;
      display: block;
      color: white;
      transform-style: preserve-3d;
    }

    .wrapper {
      transform-style: preserve-3d
    }

    .parallax::after {
      content: " ";
      position: absolute;
      top: 0;
      right: 0;
      bottom: 0;
      left: 0;
      transform: translateZ(-500px) scale(2);
      background-size: 100%;
      z-index: -1;
    }

    .static {
      background: red;
    }

    .bg1::after {
      background-image: url('https://placekitten.com/g/900/700');
    }

    .bg2::after {
      background-image: url('https://placekitten.com/g/800/600');
    }
<!DOCTYPE html>
<html lang="en">

<head>
</head>

<body>
  <main class="wrapper">
    <section class="section parallax bg1">
      <h1>placekitten</h1>
    </section>
    <section class="section static">
      <h1>Static</h1>
    </section>
    <section class="section parallax bg2">
      <h1>placekitten</h1>
    </section>
  </main>

</body>

</html>

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。