我已经看到当用户滚动到页面末尾时显示 div 的页面。当用户开始向上滚动时,div 消失。我怎样才能在 jQuery 中做到这一点。我正在使用 v1.8
到目前为止,我已经尝试过了
if($(window).scrollTop() + $(window).height() == $(document).height()) {
alert('bottom')
}
我已经看到当用户滚动到页面末尾时显示 div 的页面。当用户开始向上滚动时,div 消失。我怎样才能在 jQuery 中做到这一点。我正在使用 v1.8
到目前为止,我已经尝试过了
if($(window).scrollTop() + $(window).height() == $(document).height()) {
alert('bottom')
}
您必须在滚动时检查:
var $win = $(window),
$doc = $(document),
$target = $('#target');
// save relevant elements so they don't have to be selected on each scroll call
$win.scroll(function() {
$win.scrollTop() + $win.height() == $doc.height()
? $target.show()
: $target.hide();
});
示例:http: //jsfiddle.net/7EzUf/
您将代码放在元素的.scroll
处理程序中。window
每当用户滚动时,都会触发该函数。
$(window).scroll(function(){
if ($(window).scrollTop() + $(window).height() == $(document).height()) {
$('#bottom').fadeIn();
} else {
$('#bottom').fadeOut();
}
});
看到它在这里工作jsFiddle
当您击中底部时会出现警报 div,否则会隐藏