I have a simple script inside my html file that changes the opacity of a div depending on the result of the $(window).scrollTop() function. So if I scroll over a certain point the div appears and if I scroll back behind that point it should disappear again.
This works fine:
$(window).scroll(function () {
if ($(window).scrollTop() > $("#m-product-sweater").offset().top) {
$("#m-nav").animate({opacity: "1"}, 500);
};
});
The logic for changing the opactiy back to 0 is missing. So I simply changed the code to:
$(window).scroll(function () {
if ($(window).scrollTop() > $("#m-product-sweater").offset().top) {
$("#m-nav").animate({opacity: "1"}, 500);
};
if ($(window).scrollTop() < $("#m-product-sweater").offset().top) {
$("#m-nav").animate({opacity: "0"}, 500);
};
});
All of the sudden the whole script won't work anymore! Why?
I don't see any mistakes. I also tried it with else like this:
$(window).scroll(function () {
if ($(window).scrollTop() > $("#m-product-sweater").offset().top) {
$("#m-nav").animate({opacity: "1"}, 500);
} else {
$("#m-nav").animate({opacity: "0"}, 500);
};
});
Won't work either. I am stuck. :(
console.log();below theanimatecall to see if the if statement itself is even working. Of course it is. For some reason the script just won't change the css attribute...$(window).scrillTop() == $("#m-product-sweater").offset().top?