Reputation: 1275
I have seen pages which display div's when the user scrolls to the end of the page. When the user starts scrolling up, the div disappears. How can I do this in jQuery. I am using v1.8
So far I have tried this
if($(window).scrollTop() + $(window).height() == $(document).height()) {
alert('bottom')
}
Upvotes: 0
Views: 104
Reputation: 7250
You have to check that on scrolling:
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();
});
Upvotes: 2
Reputation: 12753
Example: http://jsfiddle.net/7EzUf/
You put your code in a .scroll
handler for the window
element. The function will be fired whenever the user scrolls.
$(window).scroll(function(){
if ($(window).scrollTop() + $(window).height() == $(document).height()) {
$('#bottom').fadeIn();
} else {
$('#bottom').fadeOut();
}
});
Upvotes: 2