To detect the scroll direction:
scrollYvalue with the current
In the code above:
divelement to indicate the direction of the scroll. We set the element at a fixed position on the top right of the page using the
divelements with some line break elements (
br) so that the page becomes scrollable.
oldScrollYand store the
scrollYvalue of the window object.
windowobject. This is executed when the scroll on the page happens. Inside this function, we get the current
scrollYvalue of the window object and compare it with the
oldScrollY. If the new value is greater than
oldScrollYthen the scroll direction is downwards. Otherwise, the scroll direction is upwards.
oldScrollYvariable with the latest
View all Courses