我有一个元素在一个带有overflow: scroll
属性的容器内。这很好,但是我想要的是,当元素滚动时,一旦元素超出其“祖父”容器的顶部,整个元素就会隐藏,而不是像通常一样被裁剪。
之前,我已经让一个元素在滚动窗口时隐藏/显示过了,但实际上我想要的是,在我滚动它所在容器内的元素(以及当它溢出祖父容器时)时,隐藏/显示该元素,而不是在我滚动窗口时。
jQuery
$(document).scroll(function () {
if ($('.inner-container').scrollTop()) {
$('.scroll-content').css({'display': 'none'});
} else {
$('.scroll-content').css({'display:' : 'block'});
}
});
据我了解,在jQuery中,如果一个元素已经设置了overflow:scroll属性,那么尝试滚动该元素会有问题,而且scrollTop()方法通常只与窗口滚动有关。我无法找到一种可行的方法来使用element.scrollTop()。如有帮助,将不胜感激。
html
<div class="scroll-container">
<div class="inner-container">
<div class="scroll-content"> I want this to scroll as it is and then hide all of the orange block when it overflows outside of its container.. i.e the top of the orange block hits the top of the yellow block. The orange block should hide.
</div>
</div>
</div>
css
.scroll-container {
width: 200px;
height: 200px;
background-color: yellow;
overflow: scroll;
margin-left: 50px;
margin-top: 50px;
}
.inner-container {
width: 150px;
height: 400px;
}
.scroll-content {
margin-top: 30px;
width: 190px;
height: 150px;
background-color: orange;
}
在这里编辑代码 http://jsfiddle.net/3cdha2sy/29/
目前jQuery没有任何作用,因为我已经试过很多次,现在它甚至不能在窗口滚动时隐藏。非常感谢您的帮助。