html - 固定位置透视

标签 html css parallax

这是我的 JS FIDDLE

我想要我的 div id="text"固定。但它不适用于我的代码...问题是我需要将所有内容保留在我的 div id="wrapper" 中.我能怎么做?如果您有其他解决方案可以对我的图像产生视差效果(仅使用 css),请告诉我。

body {
  overflow-x: hidden;
  margin:0;
}

#text {
  font-size: 70px;
  height: 100%;
  width: 100%;
  position: fixed;
}
#intro {
  font-size: 30px;
  width: 100%;
  border:3px solid yellow;
  position: absolute;
}

#wrapper {

  transform-style: preserve-3d;
  height: 100%;
  perspective: 1px;
  overflow-x: hidden;
  border:3px solid cyan;
  position: fixed;
  top: 0;
  left: 0;
  width: 100%;

}

#wrapper section {

    position: relative;
    height: 200px;
    width: 300px;
    background:pink;
    box-sizing: border-box;
    border:3px solid red;
    transform-style: inherit;
    overflow: hidden; 
    margin: 100px;

  }

#wrapper section .back {
      width: 300px;

      border:3px solid green;
      transition: transform 1s ease-out;
      height:200px;
      background-position: center center;
      transform: translateZ(-0.6px) scale(1.5); }

.back {
    background-image: url(http://wallpaper-gallery.net/images/background-image/background-image-18.jpg); }





#wrapper section .back:hover {

  opacity: 0.5;
  transform:translateZ(-0.4px) scale(2.6); }

最佳答案

这里是:

body {
  overflow-x: hidden;
/*    position:static;*/
    margin:0;

}

#text {
  font-size: 70px;
  height: 100%;
  width: 100%;
  position: fixed;
  pointer-events: none;
}
#intro {
  font-size: 30px;
  width: 100%;
  border:3px solid yellow;
  position: absolute;
}

#wrapper {

  transform-style: preserve-3d;
  height: 100%;
  perspective: 1px;
  overflow-x: hidden;
  border:3px solid cyan;
  position: fixed;
  top: 0;
  left: 0;
  width: 100%;

}

#wrapper section {

    position: relative;
    height: 200px;
    width: 300px;
    background:pink;
    box-sizing: border-box;
    border:3px solid red;
    transform-style: inherit;
    overflow: hidden; 
    margin: 100px;

  }

#wrapper section .back {
      width: 300px;

      border:3px solid green;
      transition: transform 1s ease-out;
      height:200px;
      background-position: center center;
      transform: translateZ(-0.6px) scale(1.5); }

.back {
    background-image: url("http://wallpaper-gallery.net/images/background-image/background-image-18.jpg"); }





#wrapper section .back:hover {

  opacity: 0.5;
  transform:translateZ(-0.4px) scale(2.6); }
<div id="wrapper">
  <div id="intro">
    <p>Sed ut perspiciatis unde omnis iste natus error sit voluptatem accusantium doloremque laudantium, totam rem aperiam, eaque ipsa quae ab illo inventore veritatis et quasi architecto beatae vitae dicta sunt explicabo. Nemo enim ipsam voluptatem quia voluptas sit aspernatur aut odit aut fugit, sed quia consequuntur magni dolores eos qui ratione voluptatem sequi nesciunt. Neque porro quisquam est, qui dolorem ipsum quia dolor sit amet, consectetur, adipisci velit, sed quia non numquam eius modi tempora incidunt ut labore et dolore magnam aliquam quaerat voluptatem. Ut enim ad minima veniam, quis nostrum exercitationem ullam corporis suscipit laboriosam, nisi ut aliquid ex ea commodi consequatur? Quis autem vel eum iure reprehenderit qui in ea voluptate velit esse quam nihil molestiae consequatur, vel illum qui dolorem eum fugiat quo voluptas nulla pariatur?</p>
  </div>
  <section id="frame1"><div class="back"></div></section>
  <section id="frame3"><div class="back"></div></section>
  <section id="frame5"><div class="back"></div></section>
  <section id="frame7"><div class="back"></div></section>
</div>
<div id="text">
  <h1>Hello</h1>
</div>

您需要将 real fixed 元素放在 DOM 的最后(作为 #wrapper 的兄弟)并给它 pointer -events:none 所以它允许 pointer-events(包括 scroll)传递给另一个 fixed 定位元素(#wrapper,它包含其余内容)需要滚动才能使视差效果发生。

关于html - 固定位置透视,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/41539001/

相关文章:

javascript - 在移动设备上禁用视差

android - 如何样式化 (CSS) 由 Android 应用程序 (Intent) 创建的电子邮件的 HTML 正文?

html - 按照第一行的宽度换行

php - 多选选项

javascript - 链接上的 e.preventDefault() 仍会加载页面

javascript - 如何在html5中制作数据列表显示选项

javascript - 基于操作系统的条件 CSS

javascript - javascript 函数上的 HTML 按钮调用不适用于 sqlite 数据库

javascript - 未捕获的类型错误 : Cannot read property 'setAttribute' of null (console error)

javascript - Chrome 中的视差错误