我可以在这里看到我正在寻找的效果,其中fixed_header_middle消失在fixed_header_bottom后面。
$(document).ready(function() {
$(window).bind("scroll", function(e) {
if (!$('#fixed_header_bottom').hasClass('fixed')) {
if ($(document).scrollTop() >= 100) {
$('#fixed_placeholder').show();
$('#fixed_header_bottom').addClass('fixed');
}
} else {
if ($(document).scrollTop() < 100) {
$('#fixed_placeholder').hide();
$('#fixed_header_bottom').removeClass('fixed');
}
}
});
});
&#13;
* {
margin: 0;
padding: 0;
}
#fixed_header_top {
display: block;
position: fixed;
width: 100%;
height: 50px;
background-color: #DD33DD;
top: 0;
}
#fixed_header_middle {
display: block;
width: 100%;
height: 100px;
background-color: #DDDD00;
top: 50px;
position: fixed;
z-index: -1;
}
#fixed_header_bottom,
#fixed_placeholder {
display: block;
width: 100%;
height: 50px;
background-color: #11DD55;
}
#fixed_header_bottom {
margin-top: 150px;
}
#fixed_placeholder {
display: none;
}
.fixed {
position: fixed;
top: 50px;
margin-top: 0!important;
}
#body_block {
background: #FFF;
}
&#13;
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<div id="fixed_header_top">fixed_header_top</div>
<div id="fixed_header_middle">fixed_header_middle</div>
<div id="fixed_header_bottom">fixed_header_bottom</div>
<div id="fixed_placeholder">Shouldn't see me</div>
<div id="body_block">BEGIN
<br />
<br />Bottom
<br />Bottom
<br />Bottom
<br />Bottom
<br />Bottom
<br />Bottom
<br />Bottom
<br />Bottom
<br />Bottom
<br />Bottom
<br />Bottom
<br />Bottom
<br />Bottom
<br />Bottom
<br />Bottom
<br />Bottom
<br />Bottom
<br />Bottom
<br />Bottom
<br />Bottom
<br />Bottom
<br />Bottom
<br />Bottom
<br />Bottom
<br />Bottom
<br />Bottom
<br />Bottom
<br />Bottom
<br />Bottom
<br />Bottom
<br />Bottom
<br />Bottom
<br />Bottom
<br />Bottom
<br />Bottom
<br />
<br />END</div>
&#13;
但是,在此示例中,还有其他效果,例如 fixed_header_bottom变得固定。
我只对在向下滚动时知道如何使固定元素在另一个元素后面消失感兴趣。
答案 0 :(得分:0)
如果我得到你想要的东西,z-index就是你要找的...... 它控制元素的叠加.. 见下面的代码
$(document).ready(function() {
$(window).bind("scroll", function(e) {
if (!$('#fixed_header_bottom').hasClass('fixed')) {
if ($(document).scrollTop() >= 100) {
$('#fixed_placeholder').show();
$('#fixed_header_bottom').addClass('fixed');
}
} else {
if ($(document).scrollTop() < 100) {
$('#fixed_placeholder').hide();
$('#fixed_header_bottom').removeClass('fixed');
$('#fixed_header_bottom').css('z-index', -1);
}
}
});
});
* {
margin: 0;
padding: 0;
}
#fixed_header_top {
display: block;
position: fixed;
width: 100%;
height: 50px;
background-color: #DD33DD;
top: 0;
}
#fixed_header_middle {
display: block;
width: 100%;
height: 100px;
background-color: #DDDD00;
top: 50px;
position: fixed;
z-index: -1;
}
#fixed_header_bottom,
#fixed_placeholder {
display: block;
width: 100%;
height: 50px;
background-color: #11DD55;
}
#fixed_header_bottom {
margin-top: 150px;
}
#fixed_placeholder {
display: none;
}
.fixed {
position: fixed;
top: 50px;
margin-top: 0!important;
}
#body_block {
background: #FFF;
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<div id="fixed_header_top">fixed_header_top</div>
<div id="fixed_header_middle">fixed_header_middle</div>
<div id="fixed_header_bottom">fixed_header_bottom</div>
<div id="fixed_placeholder">Shouldn't see me</div>
<div id="body_block">BEGIN
<br />
<br />Bottom
<br />Bottom
<br />Bottom
<br />Bottom
<br />Bottom
<br />Bottom
<br />Bottom
<br />Bottom
<br />Bottom
<br />Bottom
<br />Bottom
<br />Bottom
<br />Bottom
<br />Bottom
<br />Bottom
<br />Bottom
<br />Bottom
<br />Bottom
<br />Bottom
<br />Bottom
<br />Bottom
<br />Bottom
<br />Bottom
<br />Bottom
<br />Bottom
<br />Bottom
<br />Bottom
<br />Bottom
<br />Bottom
<br />Bottom
<br />Bottom
<br />Bottom
<br />Bottom
<br />Bottom
<br />Bottom
<br />
<br />END</div>