简体   繁体   English

如何将多个div相互叠加,并随着用户滚动,每个div向上移动以显示其背后的一个div

[英]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.

相关问题 如何获得一个按钮来显示每次单击时不同的隐藏div? - How can I get one button to reveal different hidden divs with each click? 如何在点击时翻转 DIV,并在彼此之间进行交互(一个翻转打开,另一个关闭)? - How can I flip DIVs on click, with interaction between each other (one flips open, the other closes)? 两个div相互堆叠-底部div上的链接不可点击 - two divs stacked on top of each other — link on bottom div not clickable 当用户向下滚动时显示Div - Reveal Div as user scrolls down 如何修复每次用户滚动时弹出的横幅? - How to fix banner popping up each time the user scrolls? 当用户滚动到页面顶部时,如何才能使此代码仅显示div标签? - How can I go about making this code only show the div tag when the user scrolls to the top of the page? 如何将多个层叠在一起的 IMG 保存为一张图像? - How can I save multiple IMGs layered on top of each other as one image? 每次有人滚动到特定部分时,如何触发此 Count up 事件? - How can I trigger this Count up event each time someone scrolls to a specific section? 在活动 div 上(使用 JQuery)时,如何将其他 div 彼此分开? - How can you toggle other divs separate from each other while on an active div (with JQuery)? 如何在每次鼠标上下滚动时移动到每个div标签来替换默认的鼠标滚动行为? - How to replace the default mouse scroll behavior by moving to each div tag each time the mouse scrolls up or down?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM