N
N
Nohaga2020-05-08 11:02:41
JavaScript
Nohaga, 2020-05-08 11:02:41

How to track scroll down some distance?

Here is the code, at 1 pixel the scroll down already works and the menu disappears. And in safari, for example, there is a swaying effect, like when you scroll the site sways up and down, this is taken into account by the scroll. There, the menu twitches, it appears, it disappears, if you make it work only when you scroll 50 pixels, for example, so that it does not react to these staggers and small finger movements.

var lastScrollTop = 0;
$(window).scroll(function(event){
   var st = $(window).scrollTop();
   if (st > 100 && st > lastScrollTop){
      $('.header_top').addClass('hide');
   } else {
      $('.header_top').removeClass('hide');
   }
   lastScrollTop = st;
});

Answer the question

In order to leave comments, you need to log in

1 answer(s)
E
Evgeny Neverov, 2020-05-08
@deMone

Well, this part of the condition seems to interfere with you: st > lastScrollTopbecause with any scroll down, your current state will always be greater than the previous one.
If you remove this part of the condition and leave only , you will be able to take into account that the page has scrolled exactly the right distance down. And you don't need lastScrollTop at all. if (st > 100)

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question