Collapsed header

Hi,

I’m trying to implement a collapsed header
that will be collapsed on scrolling down and expand on scrolling up
all examples I found are expanding only when the user reaches the top of the page
while I want it to expand when the user scrolls up without a connection to the location of the page

thanks