简体   繁体   English

在固定的 div 上滚动 div

[英]scroll div over a fixed div

My question is how can I overlap a div over a fixed div on scroll without using a background image.我的问题是如何在不使用背景图像的情况下将 div 重叠在滚动的固定 div 上。

I want the bottom of the div to match the bottom of the screen to fix that div and make the content (the other divs) below to scroll up (like a parallax effect).我希望 div 的底部与屏幕底部匹配以修复该 div 并使下面的内容(其他 div)向上滚动(如视差效果)。 Same with if the fixed div is bigger than the screen, as in bigger height (so position sticky won't work).如果固定 div 比屏幕大,则相同,因为高度更大(因此 position 粘滞将不起作用)。

Here is what I've got so far.这是我到目前为止所得到的。 I checked if the user scrolled to the div, that I want the scroll effect at and fixed at the bottom.我检查了用户是否滚动到 div,我希望滚动效果固定在底部。 When I'm at that point the content acts like the fixed div doesn't exist and skips it abruptly instead of scrolling in from below.当我在那个时候,内容就像固定的 div 不存在一样,突然跳过它而不是从下面滚动。 The effect works though when I scroll back up it just skips the fixed div.虽然当我向上滚动时它只是跳过固定的 div,但效果有效。

I'm trying to achieve something like this: https://www.w3schools.com/howto/tryit.asp?filename=tryhow_css_parallax_percent我正在尝试实现这样的目标: https://www.w3schools.com/howto/tryit.asp?filename=tryhow_css_parallax_percent

But without a background image.但是没有背景图像。

.background-beige {
  width: 100%;
  background-color: #f8f8f8;
  padding-bottom: 200px !important;
}

.contact-banner-wrapper {
  height: 436px;
  background-color: #c22a40;
  position: relative;
}


  $(window).scroll(function (e) {
    var el = $(".contact-banner-wrapper");
    var elem = $(".background-beige");
    if (
      $(window).scrollTop() >=
      elem.offset().top  + elem.outerHeight() - window.innerHeight 
    ) {
      elem.css({ position: "fixed", bottom: "0px"});

    }
    if ($(this).scrollTop() < 200) {
      elem.css({ position: "static"});

      console.log("reset");
    }
  }); 

Do you have improvements or any other solutions?您有改进或任何其他解决方案吗?

I did a work around, to my problem and now it works as intended.我解决了我的问题,现在它按预期工作了。 position: fixed; takes the div out of the flow of the DOM so it gets ignored and skipped by the other divs.将 div 从 DOM 流中取出,因此它会被其他 div 忽略和跳过。 I just gave the content (the other divs that scroll from below over the fixed div a top margin of the height of the fixed div to work around the position: fixed; change.我只是给内容(从固定 div 下方滚动的其他 div 固定 div 高度的顶部边距,以解决position: fixed;更改。

$(window).scroll(function (e) {
    var scrollOverContent = $(".contact-banner-wrapper");
    var fixedDiv = $(".contact-banner-wrapper").prev();

    var heightFixedDiv = fixedDiv.outerHeight();

    if (
      $(window).scrollTop() >=
      fixedDiv.offset().top + fixedDiv.outerHeight() - window.innerHeight
    ) {
      fixedDiv.css({ position: "fixed", bottom: "0px", zIndex: "0" });
      scrollOverContent.css("margin-top", heightFixedDiv);
    }
    if (
      $(window).scrollTop() <
      scrollOverContent.offset().top - $(window).height()
    ) {
      fixedDiv.css({ position: "static" });
      scrollOverContent.css({ marginTop: "0" });
    }
});

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

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