我的网站需要视差效果。我通过关注 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/