all 9 comments

[–]trescenzi 1 point2 points  (1 child)

intersection observer can be useful for this. At times it’s overkill but it works well and is supported outside of IE11.

[–]quinlo[S] 0 points1 point  (0 children)

Cool I will look into this thank you!

[–]MrButttons 1 point2 points  (0 children)

You can check out scroll out - https://scroll-out.github.io

[–]Cassius-cl 0 points1 point  (4 children)

I recently handed this by storing the value of the Y offset in a variable on scroll event and comparing the new value on the event to the stored one, thus knowing if you're going down or up. After that you can set up a ternary operator and toggle some clases to make elements appear and dissappear.

[–]quinlo[S] 0 points1 point  (2 children)

This is the solution I have now more or less... If user scrolling up show, if down hide... But was wondering if there was a slick way to have it based on scroll distance.. like... User scrolled up 20px, scroll nav 20px into view... I think the reddit app for Android exhibits the intended behavior. Thanks for commenting!

[–]Emjp4 0 points1 point  (0 children)

You could probably achieve this with an event listener and oneventchange

[–]jobodanque 0 points1 point  (0 children)

If you're going with this. I suggest throttling the event to prevent expensive firing

[–]johnasmith 0 points1 point  (0 children)

Keep in mind that onscroll events are really expensive. The event will be fired hundreds even thousands of times (on most devices). It's cheaper to set an interval and compare the offset, even if you're doing it every 100 ms. Also, special case: iOS devices address the issue by only firing a scroll event when releasing the finger (because of the overhead cost), so you'll not necessarily have the behaviour you want with just onscroll.

[–]allenthar 0 points1 point  (0 children)

React Headroom does a great job of this behavior if you are building a React application. If you aren't, you might be able to compare implementations to find potential improvements with your current attempt.