简体   繁体   English

将 100vh 高度页面中的元素向上滚动并移出视图

[英]Scroll elements in a 100vh height page up and out of view

I have a simple HTML document with a height of 100vh .我有一个height100vh的简单 HTML 文档。 The element contains a title.该元素包含一个标题。 Now when I scroll nothing happens as expected.现在,当我滚动时,什么都没有按预期发生。 What I need is an effect that the page stays the same and my title is scrolling up and out of the viewport.我需要的是页面保持不变并且我的标题向上滚动并移出视口的效果。 How can I achieve that?我怎样才能做到这一点?

My code simplified:我的代码简化:

 html, body { margin: 0; padding: 0; } #wrapper { background-color: red; height: 100vh; width: 100%; } h1 { color: white; position: absolute; top: 50%; left: 50%; transform: translate(-50%, -50%); }
 <div id="wrapper"> <h1>Title</h1> </div>

You can do it the opposite way - not fixing the to-be-scrolled element, but the contents element which you put into the background by using a negative z-index:你可以用相反的方式来做 - 不是固定要滚动的元素,而是你通过使用负 z-index 放入背景的内容元素:

Apply position:fixed to the wrapper and move it to the background with z-index: -1 .position:fixed应用到包装器并使用z-index: -1将其移动到背景。 Erase position: absolute from the h1 to reset its position to static to allow it to scroll.擦除position: absoluteh1将其位置重置为static以允许其滚动。

For the scrolling to be possible, you either need enough height for the title element (at least 150% or more), or another (possibly invisible) element that follows afterwards, like in my example below.为了使滚动成为可能,您需要足够的标题元素高度(至少 150% 或更多),或者之后的另一个(可能不可见)元素,如下面的示例所示。

The text centering inside h1 can be done via flex (see below).文本居中h1可以通过 flex 完成(见下文)。

 html, body { margin: 0; padding: 0; height: 100%; } #wrapper { position: fixed; z-index: -1; background-color: red; height: 100vh; width: 100%; } h1 { color: white; display: flex; justify-content: center; align-items: center; height: 100%; margin: 0; } .something_else { height: 100%; }
 <div id="wrapper"> wrapper contents here... </div> <h1>Title</h1> <div class="something_else"></div>

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM