Dima Vleskov Dima Vleskov - 1 month ago 4
jQuery Question

how to stop menu when you stop scrolling

I can't figure out how to stop menu when you stop scrolling. When you scroll down menu has to hide and show only when stop scrolling or scrolling up. My code work only when you scroll up, I would appreciate if you help me.

js:

$(window).scroll(
{
previousTop: 0
},
function () {
var currentTop = $(window).scrollTop();
if (currentTop < this.previousTop) {
$("header").css("display", "none");
} else {
$("header").css("display", "block");
}
this.previousTop = currentTop;
});

Answer

The problem is you're only checking for when the currentTop is less than the previousTop. That would be only when going up.

Really, if you want to trigger it any time you're scrolling, you need to just do currentTop != previousTop, which will work in both directions.