I have a dropdown menu which I like to close automaticly if the user is at the top of the html page,how this can be done with javascript/jquery?
You can check if window. scrollY (the number of pixels the window has scrolled vertically) is equal to 0 . If you want to check if the window has been scrolled to its leftermost, you can check if window. scrollX (the number of pixels the window has scrolled horizontally) is equal to 0 .
If you want to check whether the user has scrolled to the bottom of the page, you can use the scroll() jQuery event. The given code piece takes the top scroll of the window, so how much the page is scrolled down, it adds the height of the visible window and checks if it is equivalent to the height of the document.
window. scrollTo(0, 0); …is a sure bet to scroll the window (or any other element) back to the top.
To handle the onScroll event in React: Set the onScroll prop on an element or add an event listener on the window object. Provide an event handler function. Access relevant properties on the event or window objects.
You can easily do this with checking the scrollTop method from jQuery, on the window object:
$(window).scrollTop()
Just handle the scroll event and within the function, check $(window).scrollTop() === 0 and you will know if the user is scrolled to the top
$(document).scroll(function() {
if($(window).scrollTop() === 0) {
$(".menu").hide();
}
});
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With