[英]How can I layer several divs on top of each other and as the user scrolls each div moves up to reveal the one behind it
I want to layer several divs on top of each other and as the user scrolls the div on top will scroll-up to reveal the dive behind it. 我想将多个div彼此叠加,当用户滚动div时,其顶部将向上滚动以显示其背后的潜水。 Here's an image of I mean:
这是我的意思的图像:
I know I need to change the natural flow of the document, so I most likely need to use absolute position, but how can I can scroll each div up? 我知道我需要更改文档的自然流,因此很可能需要使用绝对位置,但是如何滚动每个div? I tried using absolute positioning on divs each with a lower z-index, but I don't know how to scroll each one up.
我尝试对每个Z-index较低的div使用绝对定位,但是我不知道如何向上滚动每个。 Any guidance on a jQuery/JavaScript library or CSS trick to achieve this, or a point in the right direction is much appreciated.
任何有关实现此目标的jQuery / JavaScript库或CSS技巧的指南,或朝着正确方向的观点,均受到赞赏。
I'm not sure exactly what effect you're looking for, but the following might help you out. 我不确定您要寻找的是什么效果,但是以下内容可能会对您有所帮助。 You can listen to the window's scroll position using the
$(window).scroll
event and the $(window).scrollTop()
function. 您可以使用
$(window).scroll
事件和$(window).scrollTop()
函数收听窗口的滚动位置。
$(window).scroll(function (e) { var scroll = $(window).scrollTop(); if (scroll > 120) { $(".div-1").slideUp(); } if (scroll > 240) { $(".div-2").slideUp(); } });
section { height: 900px; } div { position: absolute; left: 0; top: 0; display: flex; align-items: center; justify-content: center; padding: 1rem; width: 10rem; height: 900px; font-size: 2rem; } .div-1 { background: #faa; } .div-2 { background: #aff; } .div-3 { background: #ffa; }
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script> <section> <div class="div-3">3</div> <div class="div-2">2</div> <div class="div-1">1</div> </section>
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.