简体   繁体   English

滚动到下一个父级Div时隐藏粘滞Div

[英]Hide Sticky Div Once Scrolling Past Next Parent Div

I'm trying to hide a "sticky" div once it scrolls past the next parent div. 一旦滚动到下一个父div,我就试图隐藏“粘性” div。 I've currently successfully have it so it appears after scrolling "y > 100" but I'm having a lot of trouble getting the "Sticky Note" to disappear after scrolling past #break. 我目前已经成功安装了它,因此在滚动“ y> 100”后会显示出来,但是在滚动#break之后使“粘滞音符”消失后,我遇到了很多麻烦。

Example below. 下面的例子。

http://codepen.io/anon/pen/BojKBx http://codepen.io/anon/pen/BojKBx

 $(document).scroll(function() { var y = $(this).scrollTop(); if (y > 100) { $('.bottomMenu').fadeIn(); } else { $('.bottomMenu').fadeOut(); } }); 
 .bottomMenu { display: none; position: fixed; bottom: 0; width: 50%; height: 60px; border-bottom: 1px solid #000; z-index: 1; margin: 0 auto; left: 50%; margin-left: -500px; text-align: center; } #header { font-size: 50px; text-align: center; height: 60px; width: 100%; background-color: red; } #container { height: 2500px; } #break { text-align: center; font-size: 30px; margin-bottom: 300px; width: 100%; background-color: yellow; } #footer { height: 60px; background-color: red; width: 100%; bottom: 0; } 
 <div id="header">Home</div> <div class="bottomMenu"> <h2>Sticky Note</h2> </div> <div id="container"></div> <div id="break">Should Not Be Seen After This Point</div> <div id="footer"></div> 

You can get Y position of a div (its vertical offset starting from the top of the page), and then add condition to show sticky note only when you're below the required "Y" coordinate, and above the required div. 您可以获取div的Y位置(从页面顶部开始的垂直偏移),然后添加条件以仅在低于所需的“ Y”坐标且高于所需的div时显示便签。 Example: http://codepen.io/anon/pen/EVPKyP 示例: http//codepen.io/anon/pen/EVPKyP

Javascript code: JavaScript代码:

$(document).scroll(function () {
var bodyRect = document.body.getBoundingClientRect(),
    elemRect = document.getElementById("break").getBoundingClientRect(),
    offset   = elemRect.top - bodyRect.top - window.innerHeight;
    var y = $(this).scrollTop();
    if (y > 100 && y < offset) {
        $('.bottomMenu').fadeIn();
    } else {
        $('.bottomMenu').fadeOut();
    }

});

Sources: 资料来源:

Retrieve the position (X,Y) of an HTML element 检索HTML元素的位置(X,Y)

screen width vs visible portion 屏幕宽度与可见部分

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

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