【问题标题】:DIV that is scrollable to reveal DIV underneath可滚动以在下面显示 DIV 的 DIV
【发布时间】:2017-08-24 23:31:39
【问题描述】:

我有许多 DIV,我将它们放在另一个之上。

我想要做到的是能够滚动顶部的 DIV,然后在 DIV 的底部,显示下面的 DIV。

到目前为止,这是我必须要做的:

https://codepen.io/dadofgage/pen/yMqZaj

<style>
    #container {
    position: relative;
}

.overlay, .overlay2, .overlay3, 
.content{
    display:block;
    position: absolute;
    top: 0;
    left: 0;
}

.overlay3{
    z-index: 8;
    background: #000;
    color:#fff
}

.overlay2{
    z-index: 9;
    background: #c00;
}

.overlay{
    z-index: 10;
    background: #ccc;
}
</style>


<div id="container">
<div class="overlay3">This is the top div of content.  This is the top div of content.  This is the top div of content.  This is the top div of content.  This is the top div of content.  This is the top div of content.  This is the top div of content.  This is the top div of content.  This is the top div of content.  This is the top div of content.  This is the top div of content.  This is the top div of content.  This is the top div of content.  This is the top div of content.  This is the top div of content.  This is the top div of content.  This is the top div of content.  This is the top div of content.  This is the top div of content.  This is the top div of content.  This is the top div of content.  This is the top div of content.  This is the top div of content.  This is the top div of content.  This is the top div of content.  This is the top div of content.  This is the top div of content.  This is the top div of content.  This is the top div of content.  This is the top div of content.  This is the top div of content.  This is the top div of content.  </div>
<div class="overlay2">This is the SECOND div of content.  This is the SECOND div of content.  This is the SECOND div of content.  This is the SECOND div of content.  This is the SECOND div of content.  This is the SECOND div of content.  This is the SECOND div of content.  This is the SECOND div of content.  This is the SECOND div of content.  This is the SECOND div of content.  This is the SECOND div of content.  This is the SECOND div of content.  This is the SECOND div of content.  This is the SECOND div of content.  This is the SECOND div of content.  This is the SECOND div of content.  This is the SECOND div of content.  This is the SECOND div of content.  This is the SECOND div of content.  This is the SECOND div of content.  </div>
<div class="overlay">This is the THIRD div of content.  This is the THIRD div of content.  This is the THIRD div of content.  This is the THIRD div of content.  This is the THIRD div of content.  This is the THIRD div of content.  This is the THIRD div of content.  This is the THIRD div of content.  This is the THIRD div of content.  This is the THIRD div of content.  This is the THIRD div of content.  This is the THIRD div of content.  This is the THIRD div of content.  This is the THIRD div of content.  This is the THIRD div of content.  This is the THIRD div of content.  This is the THIRD div of content.  This is the THIRD div of content.  This is the THIRD div of content.  This is the THIRD div of content.  This is the THIRD div of content.  This is the THIRD div of content.  This is the THIRD div of content.  This is the THIRD div of content.  This is the THIRD div of content.  This is the THIRD div of content.  This is the THIRD div of content.  This is the THIRD div of content.  This is the THIRD div of content.  This is the THIRD div of content.  </div>
<div class="content">This is the BOTTOM div of content.  This is the BOTTOM div of content.  This is the BOTTOM div of content.  This is the BOTTOM div of content.  This is the BOTTOM div of content.  This is the BOTTOM div of content.  This is the BOTTOM div of content.  This is the BOTTOM div of content.  This is the BOTTOM div of content.  This is the BOTTOM div of content.  This is the BOTTOM div of content.  This is the BOTTOM div of content.  This is the BOTTOM div of content.  This is the BOTTOM div of content.  This is the BOTTOM div of content.  This is the BOTTOM div of content.  This is the BOTTOM div of content.  This is the BOTTOM div of content.  This is the BOTTOM div of content.  </div>

【问题讨论】:

  • 您需要在此处显示显示问题的代码的最小示例,而不是明天可能会消失的第三方网站。向我们展示 jQuery。
  • 我没有任何其他代码 - 这就是我想要弄清楚的
  • @Mike 你试过什么?这只是 CSS 和 HTML。你的 JavaScript 或 jQuery 在哪里?

标签: jquery html css scroll effect


【解决方案1】:

我建议使用.scrollTop(),然后基于此有条件地显示它们。例如:

https://jsfiddle.net/Twisty/89at6t77/

HTML

<div id="container">
  <div id="o-3" class="overlay">This is contents #3.</div>
  <div id="o-2" class="overlay hidden">This is contents #2.</div>
  <div id="o-1" class="overlay hidden">This is contents #1.</div>
  <div id="o-0" class="content hidden">This is content.</div>
</div>

CSS

#container {
  position: relative;
  height: 1500px;
}

.overlay,
.content {
  display: block;
  position: absolute;
  top: 0;
  left: 0;
  width: 100%;
  height: 150px;
  text-align: center;
  padding-top: 150px;
}

#o-3 {
  z-index: 8;
  background: #000;
  color: #fff
}

#o-2 {
  z-index: 9;
  background: #c00;
  top: 300px;
}

#o-1 {
  z-index: 10;
  background: #ccc;
  top: 600px;
}

#o-0 {
  top: 900px;
}

.hidden {
  display: none;
}

JavaScript

$(function() {
  var boxHeight = $("#o-3").height();
  $(window).scroll(function(e) {
    currentPos = $(window).scrollTop() + boxHeight;
    if (currentPos > 320) {
      $("#o-2").fadeIn();
    }
    if (currentPos > 620) {
      $("#o-1").fadeIn();
    }
    if (currentPos > 920) {
      $("#o-0").fadeIn();
    }
  });
});

【讨论】:

  • 感谢您的帮助!如果我想将 DIV 放在另一个上面,以便滚动显示下面的 DIV 怎么办?如果我使用 position:absolute 和 top:0 是这样做的,但是由于您正在滚动,您在技术上已经将父 div 向上移动了
  • 我已经稍微更新了你的小提琴,试图更好地展示我想要的最终效果 - 我的下一个障碍是在这些 DIV 中获得可变高度jsfiddle.net/dadofgage/0k7az7mL
  • @Mike 可以做到。如果高度是可变的,您只需要获取每个div 的高度,可能存储在一个数组中,并在显示和隐藏不同部分时进行相应调整。
猜你喜欢
  • 1970-01-01
  • 2021-06-04
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2016-03-14
  • 1970-01-01
  • 1970-01-01
  • 2021-02-12
相关资源
最近更新 更多