Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to get the number of pixels a user has scrolled down the page?

Tags:

Suppose we have a html page with large height.So there will be a vertical scrollbar.

As the user scrolls down I want to know how much he has scrolled using javascript (jquery) I suppose... Is this possible?

like image 256
gadlol Avatar asked Nov 02 '10 18:11

gadlol


People also ask

How do I know how much scroll JavaScript?

scrollTop . It returns by how much the content within element was scrolled, not by how much element has moved off the screen...

How do I find the scroll length?

To get the height of the scroll bar the offsetHeight of div is subtracted from the clientHeight of div. OffsetHeight = Height of an element + Scrollbar Height. ClientHeight = Height of an element. Height of scrollbar = offsetHeight – clientHeight.

How do I know if my page is scrolled?

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.


1 Answers

In pure javascript you can simply do like that:

window.onscroll = function (e) {
    console.log(window.scrollY); // Value of scroll Y in px
};

More infos (The Mozilla Developer Network) :

  • onscroll
  • scrollY
like image 171
Fabien Sa Avatar answered Oct 10 '22 17:10

Fabien Sa