Reputation: 23
I have a div element #btns
that is hidden by default. It should be displayed on scrolling 200px
from top and again hidden after 500px
from top.
Here is my (non-working) code:
$(window).scroll(function() {
if ($(this).scrollTop()>200) {
$('#btns').fadeIn();
}
elseif ($(this).scrollTop()<500) {
$('#btns').fadeIn();
} else {
$('#btns').fadeOut();
}
});
Upvotes: 1
Views: 3344
Reputation: 107
You can add a class hide in button like this:
$(function() {
$(window).scroll(function() {
console.log('scrolling ', $(window).scrollTop(), $(document).height());
if($(window).scrollTop() >= 200 && $(window).scrollTop() <= ($(document).height() - 500)) {
$('#btns').removeClass('hide');
} else {
$('#btns').addClass('hide');
}
});
});
DEMO https://jsfiddle.net/1ks8at6r/5/
Upvotes: 1