html - CSS视差效果获得垂直滚动条和行为不端

标签 html css parallax

我的网站需要视差效果。我通过关注 this 创建了这个视差教程。它在教程中表现完美,在我的例子中,如您所见,它生成了一个垂直滚动条并且行为异常。

It scroll the inner scrollbar and keep the body as it is. When I remove overflow-y: auto; from #section-1.sec_wrapper, the parallax is not working. How can I make it work?

#section-1.sec_wrapper {
  height: 100vh;
  overflow-x: hidden;
  overflow-y: auto;
  perspective: 2px;
}

#section-1.sec_wrapper .top-content {
  position: relative;
  height: 100vh;
  display: flex;
  align-items: center;
  justify-content: center;
  color: white;
  text-shadow: 0 0 5px #000;
}

#section-1.sec_wrapper .top-content:after {
  background-image: url('https://placekitten.com/g/900/700');
  background-repeat: no-repeat;
}

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

#section-2 {
  background: red;
  height: 400px;
}
<div class="">
  <!-- section 1 -->
  <section id="section-1" class="sec_wrapper">
    <div class="top-content parallax">
      <div class="welcome-txt">
        <p style="text-align: center;">hello! how are you?<br>we are</p>
      </div>
    </div>
  </section>

  <section id="section-2" class="sec_wrapper">

  </section>
</div>

最佳答案

Please this now, I missed the example last time.
https://codepen.io/pixel-lab/pen/oPqNLx

body,
html {
  margin: 0;
  padding: 0;
}

.wrapper {
  /* The height needs to be set to a fixed value for the effect to work.
   * 100vh is the full height of the viewport. */
  height: 100vh;
  /* The scaling of the images would add a horizontal scrollbar, so disable x overflow. */
  overflow-x: hidden;
  /* Enable scrolling on the page. */
  overflow-y: auto;
  /* Set the perspective to 2px. This is essentailly the simulated distance from the viewport to transformed objects.*/
  perspective: 2px;
}




/*
#section-1.sec_wrapper {
  height: 100vh;
  overflow-x: hidden;
  overflow-y: auto;
  perspective: 2px;
}*/

#section-1.sec_wrapper .top-content {
  position: relative;
  height: 100vh;
  display: flex;
  align-items: center;
  justify-content: center;
  color: white;
  text-shadow: 0 0 5px #000;
}

#section-1.sec_wrapper .top-content:after {
  background-image: url('https://placekitten.com/g/900/700');
  background-repeat: no-repeat;
}

.parallax::after {
  /* Display and position the pseudo-element */
  content: " ";
  position: absolute;
  top: 0;
  right: 0;
  bottom: 0;
  left: 0;
  /* Move the pseudo-element back away from the camera,
   * then scale it back up to fill the viewport.
   * Because the pseudo-element is further away, it appears to move more slowly, like in real life. */
  transform: translateZ(-1px) scale(1.5);
  /* Force the background image to fill the whole element. */
  background-size: 100%;
  /* Keep the image from overlapping sibling elements. */
  z-index: -1;
}

#section-2 {
  background: red;
  height: 400px;
}
<div class="wrapper">
  <!-- section 1 -->
  <section id="section-1" class="sec_wrapper parallax">
    <div class="top-content parallax">
      <div class="welcome-txt">
        <p style="text-align: center;">hello! how are you?<br>we are</p>
      </div>
    </div>
  </section>

  <section id="section-2" class="sec_wrapper ">

  </section>
</div>

关于html - CSS视差效果获得垂直滚动条和行为不端,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/52273395/

相关文章:

javascript - 在相同高度对齐来自不同父 div 的 div

html - 如何使用 css 设置 xml 标签属性的样式

css - 在我的 css 中应用 light open sans 字体的问题

html - TranslateZ 在 html 容器元素的底部添加额外的空白

jquery - 滑动 slider 和视差滚动

javascript - 如何让经典的jQuery视差更流畅

JavaScript 更新失败

javascript - 单击单选按钮时更改表格中的边框颜色

javascript - magento 中的顶部菜单栏问题

html - 元素仅在 Safari 上悬停时移动