Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

how check if user is at the top of the html page?

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?

like image 235
Lir An Avatar asked Jun 20 '13 14:06

Lir An


People also ask

How do I know if my scroll is at the top?

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 .

How do you check if a user has scrolled to the bottom?

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.

How do I scroll to the top of the page in HTML?

window. scrollTo(0, 0); …is a sure bet to scroll the window (or any other element) back to the top.

How do you find the scroll top of a react?

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.


1 Answers

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();
   }
});
like image 121
Morgan T. Avatar answered Oct 13 '22 14:10

Morgan T.