I have a fixed header. i want to change the opacity when i scroll down and restore the opacity when i scroll up (at top of the page) i write down this simple script:
$(window).scroll(function () {
if(scrollY == 0){
$("#header").animate({
opacity: 1
}, 1000);
}
if(scrollY > 0){
$("#header").animate({
opacity: 0.5
}, 1000);
}
});
actually the header take the opacity when i scroll down but when i scroll up at the top of the page he NEVER going back to opacity:1. why?
This might be a better way to go. It checks to see if #header is animated before animating the opacity to .5.
Also, it caches the #header in a variable outside of the scroll handler. Better for performance.
var $header = $('#header');
$(window).scroll(function () {
if(scrollY <= 0){
$header.animate({
opacity: 1
}, 1000);
}
if(scrollY > 0 && $header.is(':not(:animated)')){
$header.animate({
opacity: .5
}, 1000);
}
});
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With