I'm trying to slowly hide a banner (within a fixed element) on scroll down, and then when the user scrolls back to the top of the page, bring the banner back. I'm using jQuery.
I've tried using:
- animate() with height and opacity - https://jsfiddle.net/kellyking/9ez2sp4r/46/
$(window).scroll(function() {
if ($(this).scrollTop() > 0) {
$(".callout-banner").animate({height: "0px",opacity: "0"});
} else {
$(".callout-banner").animate({height: "150px",opacity: "1"});
}
});
- fadeIn and fadeOut - https://jsfiddle.net/kellyking/ukgm39o6/133/
$(window).scroll(function() {
if ($(this).scrollTop() > 0) {
$('.callout-banner').fadeOut( "slow" );
} else {
$('.callout-banner').fadeIn( "slow" );
}
});
They both sort of work but both have problems.
- leaves a large blank spot when it disappears and then it never comes back
- works a little better, in that the banner both goes away and comes back, but really is jumpy
I've read A LOT of other posts, and then don't seem to work in this situation when the banner is inside a fixed element, and I only want it to come back when the user scrolls all the way back to the top of page.
Use boolean to stop repeating the animation