有没有办法根据垂直滚动时传递的元素在显示和隐藏固定元素之间切换?
这是我想要实现的目标的视觉参考:
我更喜欢这个功能来检测固定元素在页面上的位置,而不是窗口的滚动位置。
希望照片足够清晰;否则,请查看以下代码段:
$(document).ready(function() {
var $window = $(window);
var div2 = $('#div2');
var div1 = $('#div1');
var div1_top = div1.offset().top;
var div1_height = div1.height();
var div1_bottom = div1_top + div1_height;
console.log(div1_bottom);
$window.on('scroll', function() {
var scrollTop = document.documentElement.scrollTop;
var viewport_height = $window.height();
var scrollTop_bottom = scrollTop + viewport_height;
div2.toggleClass('show', scrollTop > div1_top && (scrollTop + window.innerHeight) < div1_bottom);
});
});
body {
background: #ccffcc;
padding: 0;
margin: 0;
border: 0;
text-align: center;
}
#div1 {
background: #0099ff;
height: 1500px;
color: #fff;
}
#div2 {
width: 100px;
height: 100px;
text-align: center;
position: fixed;
margin: auto;
top: 0;
left: 0;
bottom: 0;
right: 0;
background: #ffff00;
color: #000;
display: none;
}
#div2.show {
display: block;
}
#div3 {
height: 1500px;
color: #000;
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<br>
<br>
<br>
<br>
<br> Scroll area before <b>div1</b> appears
<br>
<br>
<br>
<br>
<br>
<br>
<div id="div1">
<div id="div2">This is <b>div2</b></div>
This is <b>div1</b>
<br>
<i>(Toggle show/hide <b>div2</b> when the top of <b>div2</b> passes the top of <b>div1</b>)</i>
</div>
<div id="div3">
This is <b>div3</b>
<br>
<i>(Toggle show/hide <b>div2</b> when the bottom of <b>div2</b> reaches the top of <b>div3</b>)</i>
</div>
答案 0 :(得分:1)
这是你想要的吗?
如果是,解决方案是你应该考虑窗口顶部到#div2(中间部分)的距离
<强>被修改强>
添加评论提及的功能
$(document).ready(function() {
var $window = $(window);
var div2 = $('#div2');
var div1 = $('#div1');
$window.on('scroll', function() {
var scrollTop = document.documentElement.scrollTop;
var viewport_height = $window.height();
var scrollTop_bottom = scrollTop + viewport_height;
var window_top_to_div2 = ($window.height()-div2.height())/2;
var div1_top = div1.offset().top;
var div1_height = div1.height();
var div1_bottom = div1_top + div1_height;
div2.toggleClass('show', scrollTop >= (div1_top-window_top_to_div2) && (scrollTop + window.innerHeight) <= (div1_bottom+window_top_to_div2));
});
});
&#13;
body {
background: #ccffcc;
padding: 0;
margin: 0;
border: 0;
text-align: center;
}
#div1 {
background: #0099ff;
height: 1500px;
color: #fff;
}
#div2 {
width: 100px;
height: 100px;
text-align: center;
position: fixed;
margin: auto;
top: 0;
left: 0;
bottom: 0;
right: 0;
background: #ffff00;
color: #000;
display: none;
}
#div2.show {
display: block;
}
#div3 {
height: 1500px;
color: #000;
}
&#13;
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<br>
<br>
<br>
<br>
<br> Scroll area before <b>div1</b> appears
<br>
<br>
<br>
<br>
<br>
<br>
<div id="div1">
<div id="div2">This is <b>div2</b></div>
This is <b>div1</b>
<br>
<i>(Toggle show/hide <b>div2</b> when the top of <b>div2</b> passes the top of <b>div1</b>)</i>
</div>
<div id="div3">
This is <b>div3</b>
<br>
<i>(Toggle show/hide <b>div2</b> when the bottom of <b>div2</b> reaches the top of <b>div3</b>)</i>
</div>
&#13;