当用户位于页面顶部时,会显示div。当他们向下滚动页面时,div将被隐藏,直到它们到达页面底部,从而再次显示div。 div将是一个固定的导航菜单
下面是另一个成员发布的一些代码,它成功显示了页面底部的div,但始终将其隐藏在顶部。
原帖可在此处找到:How to show div when user reach bottom of the page?
谢谢!
<script>
$(document).ready(function() {
$(window).scroll(function() {
if ($("body").height() <= ($(window).height() + $(window).scrollTop())) {
$("#hi").css("display","block");
}else {
$("#hi").css("display","none");
}
});
});
</script>
答案 0 :(得分:2)
以下内容适合您:
已编辑使用fadeIn()
和fadeOut()
$(document).ready(function() {
$(window).scroll(function() {
//get the height of your menu
var menuHeight = $('#hi').height();
//get offset from top and bottom
var top = $(this).scrollTop();
var bottom = $(document).height() - $(this).height() - $(this).scrollTop();
//check to see if we are at the top, bottom, or in between
if (top < menuHeight) {
//at top set classes to show menu at top
$('#hi').removeClass( 'bottom' );
$('#hi').addClass( 'top' );
// use `show()` to show the div imediately
//$('#hi').show();
//or use `fadeIn()` to fade the div in gradually
//The strings 'fast' and 'slow' can be supplied to
//indicate durations of 200 and 600 milliseconds, respectively
$('#hi').fadeIn( 'slow' );
}
else if (bottom < menuHeight) {
//at bottom set classes to show menu at bottom
$('#hi').removeClass( 'top' );
$('#hi').addClass( 'bottom' );
//$('#hi').show();
$('#hi').fadeIn( 'slow' );
}
else {
//somewhere in between, hide menu
//$('#hi').hide();
$('#hi').fadeOut( 'slow' );
}
});
});
&#13;
#hi{
width: 100%;
height: 60px;
background-color: #cccccc;
vertical-align: middle;
text-align: center;
font-size:3em;
}
.top {
position: fixed;
top: 0;
left: 0;
z-index: 999;
}
.bottom{
position: fixed;
bottom: 0;
left: 0;
z-index: 999;
}
&#13;
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.0.3/jquery.min.js"></script>
<div id="hi" class="top"> This is my cool 'hi' div!</div>
<br>
<br>
<br>
<br>
<br>
<br>
<br>
<br>
<br>
<br>
<br>
<br>
<br>
<br>
<br>
<br>
<br>
<br>
<br>
<br>
<br>
<br>
<br>
<br>
<br>
<br>
<br>
<br>
<br>
<br>
<br>
<br>
<br>
<br>
<br>
<br>
<br>
<br>
<br>
<br>
<br>
<br>
<br>
<br>
<br>
<br>
<br>
<br>
<br>
<br>
<br>
<br>
<br>
<br>
<br>
<br>
<br>
<br>
<br>
&#13;
答案 1 :(得分:0)
试试这个:
<script>
$(document).ready(function() {
$(window).scroll(function() {
if ($("body").height() <= ($(window).height() + $(window).scrollTop()) || $(window).scrollTop() <= 50) {
$("#hi").css("display","block");
}else {
$("#hi").css("display","none");
}
});
});
</script>