0

我已经看到当用户滚动到页面末尾时显示 div 的页面。当用户开始向上滚动时,div 消失。我怎样才能在 jQuery 中做到这一点。我正在使用 v1.8

到目前为止,我已经尝试过了

if($(window).scrollTop() + $(window).height() == $(document).height()) {
       alert('bottom')
}
4

3 回答 3

2

您必须在滚动时检查:

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();
});

jsfiddle

于 2012-09-19T10:57:41.667 回答
2

示例:http: //jsfiddle.net/7EzUf/

您将代码放在元素的.scroll处理程序中。window每当用户滚动时,都会触发该函数。

$(window).scroll(function(){
    if ($(window).scrollTop() + $(window).height() == $(document).height()) {
        $('#bottom').fadeIn();
    } else {
        $('#bottom').fadeOut();
    }
});
于 2012-09-19T11:00:43.957 回答
0

看到它在这里工作jsFiddle

当您击中底部时会出现警报 div,否则会隐藏

于 2012-09-19T10:57:17.020 回答