In my understanding, the purpose of the throttled function was to keep track of the direction so that it could hide the button when scrolling down (positive scrollAmount) and show it when scrolling up (negative scrollAmount). I'm not sure if the latter is the desired behavior, but if so, I think scrollY needs to be constantly updated, hence throttle. Otherwise, debounce, which is called at the beginning / end of the scroll, doesn't have enough info to know the direction and whether it is the beginning or end of the movement. (this may illustrate this point a bit better: https://cl.ly/846f6e51d4a7).
Are you suggesting to distinguish between the leading and trailing debounce calls and then hide it on leading (the beginning of the movement) and show on trailing? I haven't considered that but I guess it should be possible. Will look into how this and how can this be simplified in general tomorrow morning
However, there is a limitation compared to the previous approach with throttle:
if the user scrolls up a little and then immediately continues to scroll down, it will still consider this as a "scroll-up" event and won't hide the button.
So, with the debounce-only approach, we can either accept this as a limitation or make it hide the button even on scroll-up.
why isn't the entire thing debounced?
In my understanding, the purpose of the throttled function was to keep track of the direction so that it could hide the button when scrolling down (positive
scrollAmount
) and show it when scrolling up (negativescrollAmount
). I'm not sure if the latter is the desired behavior, but if so, I think scrollY needs to be constantly updated, hence throttle. Otherwise,debounce
, which is called at the beginning / end of the scroll, doesn't have enough info to know the direction and whether it is the beginning or end of the movement. (this may illustrate this point a bit better: https://cl.ly/846f6e51d4a7).Are you suggesting to distinguish between the leading and trailing debounce calls and then hide it on leading (the beginning of the movement) and show on trailing? I haven't considered that but I guess it should be possible. Will look into how this and how can this be simplified in general tomorrow morning
Update: the debounce-only approach works overall:
However, there is a limitation compared to the previous approach with throttle:
if the user scrolls up a little and then immediately continues to scroll down, it will still consider this as a "scroll-up" event and won't hide the button.
So, with the debounce-only approach, we can either accept this as a limitation or make it hide the button even on scroll-up.
i'd leave it entirely debounced.
changed this line in version 13 of the diff
Done