CSS视差效果会导致垂直滚动条和行为异常

时间:2018-09-11 10:01:01

标签: html css parallax

我的网站需要视差效果。我通过遵循this教程来创建了此视差。它在教程中表现完美,就您所见,就我而言,它正在生成垂直滚动条,并且行为异常。

  

它滚动内部滚动条并保持主体不变。当我   从overflow-y: auto;中删除#section-1.sec_wrapper,即视差   不管用。我该如何运作?

#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>

1 个答案:

答案 0 :(得分:0)

import numpy as np
import pandas as pd

x = np.arange(0, 100, 0.1)
y = np.sin(x) + 0.5*np.sin(0.5*x)
z = np.random.rand(x.size)

df = pd.DataFrame(data=np.stack((x,y), axis=-1), index=x, columns=['y','z'])

# Fetch all x segments for which y is larger than 0.5. Discard others.
# Calculate segment length in units of x.
# Compute mean of z per segment.

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;
}